Skip to content

CLN: remove ensure_categorical #33495

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

Merged
merged 6 commits into from
Apr 17, 2020
21 changes: 0 additions & 21 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
validate_numeric_casting,
)
from pandas.core.dtypes.common import (
ensure_categorical,
ensure_int64,
ensure_object,
ensure_platform_int,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)