Skip to content

BUG: is_categorical shouldnt recognize Dtype objects #33377

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
PeriodDtype,
registry,
)
from pandas.core.dtypes.generic import ABCCategorical, ABCIndexClass
from pandas.core.dtypes.generic import ABCCategorical, ABCIndexClass, ABCSeries
from pandas.core.dtypes.inference import ( # noqa:F401
is_array_like,
is_bool,
Expand Down Expand Up @@ -68,9 +68,6 @@
ensure_float64 = algos.ensure_float64
ensure_float32 = algos.ensure_float32

_ensure_datetime64ns = conversion.ensure_datetime64ns
_ensure_timedelta64ns = conversion.ensure_timedelta64ns
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated edit before i figured out what this branch was about; these are never used



def ensure_float(arr):
"""
Expand Down Expand Up @@ -359,8 +356,12 @@ def is_categorical(arr) -> bool:
True
>>> is_categorical(pd.CategoricalIndex([1, 2, 3]))
True
>>> is_categorical(cat.dtype)
False
"""
return isinstance(arr, ABCCategorical) or is_categorical_dtype(arr)
return isinstance(arr, ABCCategorical) or (
isinstance(arr, (ABCIndexClass, ABCSeries)) and is_categorical_dtype(arr.dtype)
)


def is_datetime64_dtype(arr_or_dtype) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4716,7 +4716,7 @@ def groupby(self, values) -> PrettyDict[Hashable, np.ndarray]:
# TODO: if we are a MultiIndex, we can do better
# that converting to tuples
if isinstance(values, ABCMultiIndex):
values = values.values
values = values._values
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated edit from before i figured out what this branch was about

values = ensure_categorical(values)
result = values._reverse_indexer()

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def test_is_categorical():
assert com.is_categorical(pd.CategoricalIndex([1, 2, 3]))

assert not com.is_categorical([1, 2, 3])
assert not com.is_categorical(cat.dtype) # Categorical obj, not dtype


def test_is_datetime64_dtype():
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_basic(self, dtype):
assert is_categorical_dtype(s)
assert not is_categorical_dtype(np.dtype("float64"))

assert is_categorical(s.dtype)
assert not is_categorical(s.dtype)
assert is_categorical(s)
assert not is_categorical(np.dtype("float64"))
assert not is_categorical(1.0)
Expand Down