Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REF: remove CategoricalIndex._reindex_non_unique #42094

Merged
merged 1 commit into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3772,6 +3772,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 @@ -3803,14 +3804,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