Skip to content

BUG: Fix is_categorical_dtype for Sparse[category] #35797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Bug fixes
Categorical
^^^^^^^^^^^

-
- Bug in :func:`pandas.core.dtypes.common.is_categorical_dtype` where sparse categorical dtypes would return ``False`` (:issue:`35793`)
-

Datetimelike
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ def is_dtype(cls, dtype: object) -> bool:
return False
elif isinstance(dtype, cls):
return True
elif hasattr(dtype, "subtype") and isinstance(dtype.subtype, cls):
return True
if isinstance(dtype, str):
try:
return cls.construct_from_string(dtype) is not None
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,7 @@ def is_categorical_dtype(arr_or_dtype) -> bool:
"""
if isinstance(arr_or_dtype, ExtensionDtype):
# GH#33400 fastpath for dtype object
return arr_or_dtype.name == "category"

return ("category" == arr_or_dtype.name) or ("category" in arr_or_dtype.name)
if arr_or_dtype is None:
return False
return CategoricalDtype.is_dtype(arr_or_dtype)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ def test_is_categorical_deprecation():
com.is_categorical([1, 2, 3])


def test_sparse_categorical_is_categorical():
# https://github.com/pandas-dev/pandas/issues/35793
s = pd.Series(
["a", "b", "c"], dtype=pd.SparseDtype(CategoricalDtype(["a", "b", "c"]))
)
assert com.is_categorical_dtype(s)
assert com.is_categorical_dtype(s.dtype)


def test_is_datetime64_dtype():
assert not com.is_datetime64_dtype(object)
assert not com.is_datetime64_dtype([1, 2, 3])
Expand Down