diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index f06d118538c1a..287f94fb6b723 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -365,7 +365,6 @@ def __contains__(self, key: Any) -> bool: return contains(self, key, container=self._engine) - # TODO(2.0): remove reindex once non-unique deprecation is enforced def reindex( self, target, method=None, level=None, limit=None, tolerance=None ) -> tuple[Index, npt.NDArray[np.intp] | None]: @@ -392,51 +391,7 @@ def reindex( raise NotImplementedError( "argument limit is not implemented for CategoricalIndex.reindex" ) - - target = ibase.ensure_index(target) - - if self.equals(target): - indexer = None - missing = np.array([], dtype=np.intp) - else: - indexer, missing = self.get_indexer_non_unique(target) - if not self.is_unique: - # GH#42568 - raise ValueError("cannot reindex on an axis with duplicate labels") - - new_target: Index - if len(self) and indexer is not None: - new_target = self.take(indexer) - else: - new_target = target - - # filling in missing if needed - if len(missing): - cats = self.categories.get_indexer(target) - - if not isinstance(target, CategoricalIndex) or (cats == -1).any(): - new_target, indexer, _ = super()._reindex_non_unique(target) - else: - # error: "Index" has no attribute "codes" - codes = new_target.codes.copy() # type: ignore[attr-defined] - codes[indexer == -1] = cats[missing] - cat = self._data._from_backing_data(codes) - new_target = type(self)._simple_new(cat, name=self.name) - - # we always want to return an Index type here - # to be consistent with .reindex for other index types (e.g. they don't - # coerce based on the actual values, only on the dtype) - # unless we had an initial Categorical to begin with - # in which case we are going to conform to the passed Categorical - if is_categorical_dtype(target): - cat = Categorical(new_target, dtype=target.dtype) - new_target = type(self)._simple_new(cat, name=self.name) - else: - # e.g. test_reindex_with_categoricalindex, test_reindex_duplicate_target - new_target_array = np.asarray(new_target) - new_target = Index._with_infer(new_target_array, name=self.name) - - return new_target, indexer + return super().reindex(target) # -------------------------------------------------------------------- # Indexing Methods