Skip to content

BUG: String categories are not quoted as expected #63045

@thanhlecongg

Description

@thanhlecongg

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
s = pd.Series(["apple", "banana", "cherry", "cherry"], dtype="string")
cat = pd.Categorical(s)
print(cat)
# Expected: Categories (3, string): ['apple', 'banana', 'cherry']
# Output: Categories (3, string): [apple, banana, cherry]

Issue Description

When Categorical receives a pandas Series with dtype of “string”, representation of string elements in the list of unique categories will not include quotes. During my search against existing Issues, I think this relates to Issues #61890. The proposed fix in PR #61891 only checks dtype against “str”. Therefore, it still fails to check pandas Series with dtype of “string”. This PR fixes this problem by adding a check against “string”, like this:

- if self.categories.dtype == "str":
+ if self.categories.dtype == "str" or self.categories.dtype == "string":

If this fix is good to you, I can open a PR for your review.

Expected Behavior

String categories are expected to be quoted, but the current output is not.

Expected: Categories (3, string): ['apple', 'banana', 'cherry']
Output: Categories (3, string): [apple, banana, cherry]

Installed Versions

commit : 9c8bc3e
python : 3.13.9
python-bits : 64
OS : Darwin
OS-release : 24.6.0
Version : Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:29 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.3.3
numpy : 2.3.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 22.0.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions