Skip to content

TYP: Index.reindex #40950

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

Merged
merged 1 commit into from
Apr 16, 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
19 changes: 12 additions & 7 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3762,7 +3762,9 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None:
if not self._index_as_unique and len(indexer):
raise ValueError("cannot reindex from a duplicate axis")

def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
def reindex(
self, target, method=None, level=None, limit=None, tolerance=None
) -> tuple[Index, np.ndarray | None]:
"""
Create index with target's values.

Expand All @@ -3774,7 +3776,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
-------
new_index : pd.Index
Resulting index.
indexer : np.ndarray or None
indexer : np.ndarray[np.intp] or None
Indices of output values in original index.
"""
# GH6552: preserve names when reindexing to non-named target
Expand Down Expand Up @@ -3815,7 +3817,9 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):

return target, indexer

def _reindex_non_unique(self, target):
def _reindex_non_unique(
self, target: Index
) -> tuple[Index, np.ndarray, np.ndarray | None]:
"""
Create a new index with target's values (move/add/delete values as
necessary) use with non-unique Index and a possibly non-unique target.
Expand All @@ -3828,8 +3832,9 @@ def _reindex_non_unique(self, target):
-------
new_index : pd.Index
Resulting index.
indexer : np.ndarray or None
indexer : np.ndarray[np.intp]
Indices of output values in original index.
new_indexer : np.ndarray[np.intp] or None

"""
target = ensure_index(target)
Expand Down Expand Up @@ -3858,13 +3863,13 @@ def _reindex_non_unique(self, target):
# GH#38906
if not len(self):

new_indexer = np.arange(0)
new_indexer = np.arange(0, dtype=np.intp)

# a unique indexer
elif target.is_unique:

# see GH5553, make sure we use the right indexer
new_indexer = np.arange(len(indexer))
new_indexer = np.arange(len(indexer), dtype=np.intp)
new_indexer[cur_indexer] = np.arange(len(cur_labels))
new_indexer[missing_indexer] = -1

Expand All @@ -3876,7 +3881,7 @@ def _reindex_non_unique(self, target):
indexer[~check] = -1

# reset the new indexer to account for the new size
new_indexer = np.arange(len(self.take(indexer)))
new_indexer = np.arange(len(self.take(indexer)), dtype=np.intp)
new_indexer[~check] = -1

if isinstance(self, ABCMultiIndex):
Expand Down
27 changes: 19 additions & 8 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,17 @@ def unique(self, level=None):
# of result, not self.
return type(self)._simple_new(result, name=self.name)

def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
def reindex(
self, target, method=None, level=None, limit=None, tolerance=None
) -> tuple[Index, np.ndarray | None]:
"""
Create index with target's values (move/add/delete values as necessary)

Returns
-------
new_index : pd.Index
Resulting index
indexer : np.ndarray or None
indexer : np.ndarray[np.intp] or None
Indices of output values in original index

"""
Expand Down Expand Up @@ -440,7 +442,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
if not isinstance(cats, CategoricalIndex) or (cats == -1).any():
# coerce to a regular index here!
result = Index(np.array(self), name=self.name)
new_target, indexer, _ = result._reindex_non_unique(np.array(target))
new_target, indexer, _ = result._reindex_non_unique(target)
else:

codes = new_target.codes.copy()
Expand All @@ -462,25 +464,34 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):

return new_target, indexer

def _reindex_non_unique(self, target):
# 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
if check.any():
new_indexer = np.arange(len(self.take(indexer)))
# 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
new_target = Categorical(new_target, dtype=self.dtype)
new_target = type(self)._simple_new(new_target, name=self.name)
cat = Categorical(new_target, dtype=self.dtype)
new_target = type(self)._simple_new(cat, name=self.name)

return new_target, indexer, new_indexer

Expand Down
6 changes: 4 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2503,15 +2503,17 @@ def sortlevel(

return new_index, indexer

def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
def reindex(
self, target, method=None, level=None, limit=None, tolerance=None
) -> tuple[MultiIndex, np.ndarray | None]:
"""
Create index with target's values (move/add/delete values as necessary)

Returns
-------
new_index : pd.MultiIndex
Resulting index
indexer : np.ndarray or None
indexer : np.ndarray[np.intp] or None
Indices of output values in original index.

"""
Expand Down