Skip to content

Commit 2fc8559

Browse files
authored
CLN: simplify CategoricalIndex._simple_new (#32204)
1 parent 7c5d3d5 commit 2fc8559

File tree

2 files changed

+8
-41
lines changed

2 files changed

+8
-41
lines changed

pandas/core/indexes/category.py

+8-32
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
is_scalar,
2020
)
2121
from pandas.core.dtypes.dtypes import CategoricalDtype
22-
from pandas.core.dtypes.generic import ABCCategorical, ABCSeries
2322
from pandas.core.dtypes.missing import isna
2423

2524
from pandas.core import accessor
@@ -195,7 +194,9 @@ def __new__(
195194
raise cls._scalar_data_error(data)
196195
data = []
197196

198-
data = cls._create_categorical(data, dtype=dtype)
197+
assert isinstance(dtype, CategoricalDtype), dtype
198+
if not isinstance(data, Categorical) or data.dtype != dtype:
199+
data = Categorical(data, dtype=dtype)
199200

200201
data = data.copy() if copy else data
201202

@@ -225,37 +226,11 @@ def _create_from_codes(self, codes, dtype=None, name=None):
225226
return CategoricalIndex(cat, name=name)
226227

227228
@classmethod
228-
def _create_categorical(cls, data, dtype=None):
229-
"""
230-
*this is an internal non-public method*
231-
232-
create the correct categorical from data and the properties
233-
234-
Parameters
235-
----------
236-
data : data for new Categorical
237-
dtype : CategoricalDtype, defaults to existing
238-
239-
Returns
240-
-------
241-
Categorical
242-
"""
243-
if isinstance(data, (cls, ABCSeries)) and is_categorical_dtype(data):
244-
data = data.values
245-
246-
if not isinstance(data, ABCCategorical):
247-
return Categorical(data, dtype=dtype)
248-
249-
if isinstance(dtype, CategoricalDtype) and dtype != data.dtype:
250-
# we want to silently ignore dtype='category'
251-
data = data._set_dtype(dtype)
252-
return data
253-
254-
@classmethod
255-
def _simple_new(cls, values, name=None, dtype=None):
229+
def _simple_new(cls, values: Categorical, name=None, dtype=None):
230+
# GH#32204 dtype is included for compat with Index._simple_new
231+
assert isinstance(values, Categorical), type(values)
256232
result = object.__new__(cls)
257233

258-
values = cls._create_categorical(values, dtype=dtype)
259234
result._data = values
260235
result.name = name
261236

@@ -298,7 +273,8 @@ def _is_dtype_compat(self, other) -> bool:
298273
values = other
299274
if not is_list_like(values):
300275
values = [values]
301-
other = CategoricalIndex(self._create_categorical(other, dtype=self.dtype))
276+
cat = Categorical(other, dtype=self.dtype)
277+
other = CategoricalIndex(cat)
302278
if not other.isin(values).all():
303279
raise TypeError(
304280
"cannot append a non-category item to a CategoricalIndex"

pandas/tests/indexes/categorical/test_constructors.py

-9
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,3 @@ def test_construction_with_categorical_dtype(self):
136136

137137
with pytest.raises(ValueError, match=msg):
138138
Index(data, ordered=ordered, dtype=dtype)
139-
140-
def test_create_categorical(self):
141-
# GH#17513 The public CI constructor doesn't hit this code path with
142-
# instances of CategoricalIndex, but we still want to test the code
143-
ci = CategoricalIndex(["a", "b", "c"])
144-
# First ci is self, second ci is data.
145-
result = CategoricalIndex._create_categorical(ci, ci)
146-
expected = Categorical(["a", "b", "c"])
147-
tm.assert_categorical_equal(result, expected)

0 commit comments

Comments
 (0)