diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index b2301ab0190c7..cf00535385c24 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -111,27 +111,6 @@ def ensure_str(value: Union[bytes, Any]) -> str: return value -def ensure_categorical(arr): - """ - Ensure that an array-like object is a Categorical (if not already). - - Parameters - ---------- - arr : array-like - The array that we want to convert into a Categorical. - - Returns - ------- - cat_arr : The original array cast as a Categorical. If it already - is a Categorical, we return as is. - """ - if not is_categorical_dtype(arr.dtype): - from pandas import Categorical - - arr = Categorical(arr) - return arr - - def ensure_int_or_float(arr: ArrayLike, copy: bool = False) -> np.array: """ Ensure that an dtype array of some integer dtype diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 9bd098d1d49a3..235b3113a2ae7 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -11,7 +11,6 @@ from pandas.util._decorators import cache_readonly from pandas.core.dtypes.common import ( - ensure_categorical, is_categorical_dtype, is_datetime64_dtype, is_list_like, @@ -418,7 +417,7 @@ def indices(self): if isinstance(self.grouper, ops.BaseGrouper): return self.grouper.indices - values = ensure_categorical(self.grouper) + values = Categorical(self.grouper) return values._reverse_indexer() @property diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 530aaee24c7fb..a91757fc45c9d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -24,7 +24,6 @@ validate_numeric_casting, ) from pandas.core.dtypes.common import ( - ensure_categorical, ensure_int64, ensure_object, ensure_platform_int, @@ -68,7 +67,7 @@ from pandas.core import ops from pandas.core.accessor import CachedAccessor import pandas.core.algorithms as algos -from pandas.core.arrays import ExtensionArray +from pandas.core.arrays import Categorical, ExtensionArray from pandas.core.arrays.datetimes import tz_to_dtype, validate_tz_from_dtype from pandas.core.base import IndexOpsMixin, PandasObject import pandas.core.common as com @@ -4727,8 +4726,8 @@ 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 = ensure_categorical(values) + values = values._values + values = Categorical(values) result = values._reverse_indexer() # map to the label diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 82d6b1df19393..3d58df258e8e9 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -21,7 +21,6 @@ from pandas.core.dtypes import inference from pandas.core.dtypes.common import ( - ensure_categorical, ensure_int32, is_bool, is_datetime64_any_dtype, @@ -1502,13 +1501,3 @@ def test_ensure_int32(): values = np.arange(10, dtype=np.int64) result = ensure_int32(values) assert result.dtype == np.int32 - - -def test_ensure_categorical(): - values = np.arange(10, dtype=np.int32) - result = ensure_categorical(values) - assert result.dtype == "category" - - values = Categorical(values) - result = ensure_categorical(values) - tm.assert_categorical_equal(result, values)