Skip to content

Commit 7ff396d

Browse files
authored
REF: remove CategoricalIndex._reindex_non_unique (#42094)
1 parent 50e55e6 commit 7ff396d

File tree

2 files changed

+5
-34
lines changed

2 files changed

+5
-34
lines changed

pandas/core/indexes/base.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3794,6 +3794,7 @@ def reindex(
37943794

37953795
return target, indexer
37963796

3797+
@final
37973798
def _reindex_non_unique(
37983799
self, target: Index
37993800
) -> tuple[Index, np.ndarray, np.ndarray | None]:
@@ -3825,14 +3826,15 @@ def _reindex_non_unique(
38253826
new_indexer = None
38263827

38273828
if len(missing):
3828-
length = np.arange(len(indexer))
3829+
length = np.arange(len(indexer), dtype=np.intp)
38293830

38303831
missing = ensure_platform_int(missing)
38313832
missing_labels = target.take(missing)
3832-
missing_indexer = ensure_platform_int(length[~check])
3833+
missing_indexer = length[~check]
38333834
cur_labels = self.take(indexer[check]).values
3834-
cur_indexer = ensure_platform_int(length[check])
3835+
cur_indexer = length[check]
38353836

3837+
# Index constructor below will do inference
38363838
new_labels = np.empty((len(indexer),), dtype=object)
38373839
new_labels[cur_indexer] = cur_labels
38383840
new_labels[missing_indexer] = missing_labels

pandas/core/indexes/category.py

-31
Original file line numberDiff line numberDiff line change
@@ -442,37 +442,6 @@ def reindex(
442442

443443
return new_target, indexer
444444

445-
# error: Return type "Tuple[Index, Optional[ndarray], Optional[ndarray]]"
446-
# of "_reindex_non_unique" incompatible with return type
447-
# "Tuple[Index, ndarray, Optional[ndarray]]" in supertype "Index"
448-
def _reindex_non_unique( # type: ignore[override]
449-
self, target: Index
450-
) -> tuple[Index, np.ndarray | None, np.ndarray | None]:
451-
"""
452-
reindex from a non-unique; which CategoricalIndex's are almost
453-
always
454-
"""
455-
# TODO: rule out `indexer is None` here to make the signature
456-
# match the parent class's signature. This should be equivalent
457-
# to ruling out `self.equals(target)`
458-
new_target, indexer = self.reindex(target)
459-
new_indexer = None
460-
461-
check = indexer == -1
462-
# error: Item "bool" of "Union[Any, bool]" has no attribute "any"
463-
if check.any(): # type: ignore[union-attr]
464-
new_indexer = np.arange(len(self.take(indexer)), dtype=np.intp)
465-
new_indexer[check] = -1
466-
467-
cats = self.categories.get_indexer(target)
468-
if not (cats == -1).any():
469-
# .reindex returns normal Index. Revert to CategoricalIndex if
470-
# all targets are included in my categories
471-
cat = Categorical(new_target, dtype=self.dtype)
472-
new_target = type(self)._simple_new(cat, name=self.name)
473-
474-
return new_target, indexer, new_indexer
475-
476445
# --------------------------------------------------------------------
477446
# Indexing Methods
478447

0 commit comments

Comments
 (0)