Skip to content

Commit 7bc3b08

Browse files
committed
Add check againts dtype of string in Categorical (#63045)
1 parent f4851e5 commit 7bc3b08

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,7 @@ def _repr_categories(self) -> list[str]:
22802280
from pandas.io.formats import format as fmt
22812281

22822282
formatter = None
2283-
if self.categories.dtype == "str":
2283+
if self.categories.dtype == "str" or self.categories.dtype == "string":
22842284
# the extension array formatter defaults to boxed=True in format_array
22852285
# override here to boxed=False to be consistent with QUOTE_NONNUMERIC
22862286
formatter = cast(ExtensionArray, self.categories._values)._formatter(

pandas/tests/arrays/categorical/test_repr.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,10 @@ def test_categorical_str_repr(self):
545545
result = repr(Categorical([1, "2", 3, 4]))
546546
expected = "[1, '2', 3, 4]\nCategories (4, object): [1, 3, 4, '2']"
547547
assert result == expected
548+
549+
def test_categorical_with_pandas_series(self):
550+
# GH 63045
551+
s = Series(["apple", "banana", "cherry", "cherry"], dtype="string")
552+
result =repr(Categorical(s))
553+
expected = "['apple', 'banana', 'cherry', 'cherry']\nCategories (3, string): ['apple', 'banana', 'cherry']"
554+
assert result == expected

0 commit comments

Comments
 (0)