From 7ff396d3079983fc5b3073b2c37ef84ddbbf10f4 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 17 Jun 2021 18:48:24 -0700 Subject: [PATCH] REF: remove CategoricalIndex._reindex_non_unique (#42094) --- pandas/core/indexes/base.py | 8 +++++--- pandas/core/indexes/category.py | 31 ------------------------------- 2 files changed, 5 insertions(+), 34 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index bd44ef9362fa0..88fded29810ff 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3794,6 +3794,7 @@ def reindex( return target, indexer + @final def _reindex_non_unique( self, target: Index ) -> tuple[Index, np.ndarray, np.ndarray | None]: @@ -3825,14 +3826,15 @@ def _reindex_non_unique( new_indexer = None if len(missing): - length = np.arange(len(indexer)) + length = np.arange(len(indexer), dtype=np.intp) missing = ensure_platform_int(missing) missing_labels = target.take(missing) - missing_indexer = ensure_platform_int(length[~check]) + missing_indexer = length[~check] cur_labels = self.take(indexer[check]).values - cur_indexer = ensure_platform_int(length[check]) + cur_indexer = length[check] + # Index constructor below will do inference new_labels = np.empty((len(indexer),), dtype=object) new_labels[cur_indexer] = cur_labels new_labels[missing_indexer] = missing_labels diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 2ebc39664ad60..ce5cb0779ee53 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -442,37 +442,6 @@ def reindex( return new_target, indexer - # error: Return type "Tuple[Index, Optional[ndarray], Optional[ndarray]]" - # of "_reindex_non_unique" incompatible with return type - # "Tuple[Index, ndarray, Optional[ndarray]]" in supertype "Index" - def _reindex_non_unique( # type: ignore[override] - self, target: Index - ) -> tuple[Index, np.ndarray | None, np.ndarray | None]: - """ - reindex from a non-unique; which CategoricalIndex's are almost - always - """ - # TODO: rule out `indexer is None` here to make the signature - # match the parent class's signature. This should be equivalent - # to ruling out `self.equals(target)` - new_target, indexer = self.reindex(target) - new_indexer = None - - check = indexer == -1 - # error: Item "bool" of "Union[Any, bool]" has no attribute "any" - if check.any(): # type: ignore[union-attr] - new_indexer = np.arange(len(self.take(indexer)), dtype=np.intp) - new_indexer[check] = -1 - - cats = self.categories.get_indexer(target) - if not (cats == -1).any(): - # .reindex returns normal Index. Revert to CategoricalIndex if - # all targets are included in my categories - cat = Categorical(new_target, dtype=self.dtype) - new_target = type(self)._simple_new(cat, name=self.name) - - return new_target, indexer, new_indexer - # -------------------------------------------------------------------- # Indexing Methods