Skip to content

Commit 16baf87

Browse files
authored
CLN: CategoricalIndex.reindex (#49513)
* remove CategoricalIndex.reindex in favor of base class * retain checks
1 parent 2a2daf7 commit 16baf87

File tree

1 file changed

+1
-46
lines changed

1 file changed

+1
-46
lines changed

pandas/core/indexes/category.py

+1-46
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ def __contains__(self, key: Any) -> bool:
365365

366366
return contains(self, key, container=self._engine)
367367

368-
# TODO(2.0): remove reindex once non-unique deprecation is enforced
369368
def reindex(
370369
self, target, method=None, level=None, limit=None, tolerance=None
371370
) -> tuple[Index, npt.NDArray[np.intp] | None]:
@@ -392,51 +391,7 @@ def reindex(
392391
raise NotImplementedError(
393392
"argument limit is not implemented for CategoricalIndex.reindex"
394393
)
395-
396-
target = ibase.ensure_index(target)
397-
398-
if self.equals(target):
399-
indexer = None
400-
missing = np.array([], dtype=np.intp)
401-
else:
402-
indexer, missing = self.get_indexer_non_unique(target)
403-
if not self.is_unique:
404-
# GH#42568
405-
raise ValueError("cannot reindex on an axis with duplicate labels")
406-
407-
new_target: Index
408-
if len(self) and indexer is not None:
409-
new_target = self.take(indexer)
410-
else:
411-
new_target = target
412-
413-
# filling in missing if needed
414-
if len(missing):
415-
cats = self.categories.get_indexer(target)
416-
417-
if not isinstance(target, CategoricalIndex) or (cats == -1).any():
418-
new_target, indexer, _ = super()._reindex_non_unique(target)
419-
else:
420-
# error: "Index" has no attribute "codes"
421-
codes = new_target.codes.copy() # type: ignore[attr-defined]
422-
codes[indexer == -1] = cats[missing]
423-
cat = self._data._from_backing_data(codes)
424-
new_target = type(self)._simple_new(cat, name=self.name)
425-
426-
# we always want to return an Index type here
427-
# to be consistent with .reindex for other index types (e.g. they don't
428-
# coerce based on the actual values, only on the dtype)
429-
# unless we had an initial Categorical to begin with
430-
# in which case we are going to conform to the passed Categorical
431-
if is_categorical_dtype(target):
432-
cat = Categorical(new_target, dtype=target.dtype)
433-
new_target = type(self)._simple_new(cat, name=self.name)
434-
else:
435-
# e.g. test_reindex_with_categoricalindex, test_reindex_duplicate_target
436-
new_target_array = np.asarray(new_target)
437-
new_target = Index._with_infer(new_target_array, name=self.name)
438-
439-
return new_target, indexer
394+
return super().reindex(target)
440395

441396
# --------------------------------------------------------------------
442397
# Indexing Methods

0 commit comments

Comments
 (0)