Skip to content

Commit

Permalink
REF: remove CategoricalIndex._reindex_non_unique (#42094)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Jun 18, 2021
1 parent 50e55e6 commit 7ff396d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 34 deletions.
8 changes: 5 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3794,6 +3794,7 @@ def reindex(

return target, indexer

@final
def _reindex_non_unique(
self, target: Index
) -> tuple[Index, np.ndarray, np.ndarray | None]:
Expand Down Expand Up @@ -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
Expand Down
31 changes: 0 additions & 31 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7ff396d

Please sign in to comment.