From 8137437e3e9219f0ad0ee436b6a121dc26c11bc7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 7 Apr 2020 12:15:57 -0700 Subject: [PATCH] BUG: is_categorical shouldnt recognize Dtype objects --- pandas/core/dtypes/common.py | 11 ++++++----- pandas/core/indexes/base.py | 2 +- pandas/tests/dtypes/test_common.py | 1 + pandas/tests/dtypes/test_dtypes.py | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 16373bd697c1f..efb27db26345e 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -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, @@ -68,9 +68,6 @@ ensure_float64 = algos.ensure_float64 ensure_float32 = algos.ensure_float32 -_ensure_datetime64ns = conversion.ensure_datetime64ns -_ensure_timedelta64ns = conversion.ensure_timedelta64ns - def ensure_float(arr): """ @@ -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: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d0319e9181bad..9f147a6e6c8fd 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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 values = ensure_categorical(values) result = values._reverse_indexer() diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 66bf696cbe912..7dc8984f6d9b7 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -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(): diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index d0831ea514a64..d9b0473d6c3a3 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -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)