Skip to content

Commit 744859a

Browse files
jbrockmendelCloseChoice
authored andcommitted
CLN: remove ensure_categorical (pandas-dev#33495)
1 parent c38dab8 commit 744859a

File tree

4 files changed

+4
-38
lines changed

4 files changed

+4
-38
lines changed

pandas/core/dtypes/common.py

-21
Original file line numberDiff line numberDiff line change
@@ -111,27 +111,6 @@ def ensure_str(value: Union[bytes, Any]) -> str:
111111
return value
112112

113113

114-
def ensure_categorical(arr):
115-
"""
116-
Ensure that an array-like object is a Categorical (if not already).
117-
118-
Parameters
119-
----------
120-
arr : array-like
121-
The array that we want to convert into a Categorical.
122-
123-
Returns
124-
-------
125-
cat_arr : The original array cast as a Categorical. If it already
126-
is a Categorical, we return as is.
127-
"""
128-
if not is_categorical_dtype(arr.dtype):
129-
from pandas import Categorical
130-
131-
arr = Categorical(arr)
132-
return arr
133-
134-
135114
def ensure_int_or_float(arr: ArrayLike, copy: bool = False) -> np.array:
136115
"""
137116
Ensure that an dtype array of some integer dtype

pandas/core/groupby/grouper.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pandas.util._decorators import cache_readonly
1212

1313
from pandas.core.dtypes.common import (
14-
ensure_categorical,
1514
is_categorical_dtype,
1615
is_datetime64_dtype,
1716
is_list_like,
@@ -418,7 +417,7 @@ def indices(self):
418417
if isinstance(self.grouper, ops.BaseGrouper):
419418
return self.grouper.indices
420419

421-
values = ensure_categorical(self.grouper)
420+
values = Categorical(self.grouper)
422421
return values._reverse_indexer()
423422

424423
@property

pandas/core/indexes/base.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
validate_numeric_casting,
2525
)
2626
from pandas.core.dtypes.common import (
27-
ensure_categorical,
2827
ensure_int64,
2928
ensure_object,
3029
ensure_platform_int,
@@ -68,7 +67,7 @@
6867
from pandas.core import ops
6968
from pandas.core.accessor import CachedAccessor
7069
import pandas.core.algorithms as algos
71-
from pandas.core.arrays import ExtensionArray
70+
from pandas.core.arrays import Categorical, ExtensionArray
7271
from pandas.core.arrays.datetimes import tz_to_dtype, validate_tz_from_dtype
7372
from pandas.core.base import IndexOpsMixin, PandasObject
7473
import pandas.core.common as com
@@ -4727,8 +4726,8 @@ def groupby(self, values) -> PrettyDict[Hashable, np.ndarray]:
47274726
# TODO: if we are a MultiIndex, we can do better
47284727
# that converting to tuples
47294728
if isinstance(values, ABCMultiIndex):
4730-
values = values.values
4731-
values = ensure_categorical(values)
4729+
values = values._values
4730+
values = Categorical(values)
47324731
result = values._reverse_indexer()
47334732

47344733
# map to the label

pandas/tests/dtypes/test_inference.py

-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
from pandas.core.dtypes import inference
2323
from pandas.core.dtypes.common import (
24-
ensure_categorical,
2524
ensure_int32,
2625
is_bool,
2726
is_datetime64_any_dtype,
@@ -1502,13 +1501,3 @@ def test_ensure_int32():
15021501
values = np.arange(10, dtype=np.int64)
15031502
result = ensure_int32(values)
15041503
assert result.dtype == np.int32
1505-
1506-
1507-
def test_ensure_categorical():
1508-
values = np.arange(10, dtype=np.int32)
1509-
result = ensure_categorical(values)
1510-
assert result.dtype == "category"
1511-
1512-
values = Categorical(values)
1513-
result = ensure_categorical(values)
1514-
tm.assert_categorical_equal(result, values)

0 commit comments

Comments
 (0)