-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TYP: get_reverse_indexer, get_group_index_sorter #40476
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
jreback
merged 6 commits into
pandas-dev:master
from
jbrockmendel:typ-get_reverse_indexer
Mar 20, 2021
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1689adf
TYP: get_reverse_indexer, get_group_index_sorter
jbrockmendel 33158ad
mypy ignore
jbrockmendel 7becae6
indexer_as_slice use intp
jbrockmendel 2a24e13
update usages
jbrockmendel 7e3bbec
Merge branch 'master' into typ-get_reverse_indexer
jbrockmendel 0be16b0
ignore->cast
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4125,13 +4125,22 @@ def _join_level( | |
""" | ||
from pandas.core.indexes.multi import MultiIndex | ||
|
||
def _get_leaf_sorter(labels): | ||
def _get_leaf_sorter(labels: List[np.ndarray]) -> np.ndarray: | ||
""" | ||
Returns sorter for the inner most level while preserving the | ||
order of higher levels. | ||
|
||
Parameters | ||
---------- | ||
labels : list[np.ndarray] | ||
Each ndarray has signed integer dtype, not necessarily identical. | ||
|
||
Returns | ||
------- | ||
np.ndarray[np.intp] | ||
""" | ||
if labels[0].size == 0: | ||
return np.empty(0, dtype="int64") | ||
return np.empty(0, dtype=np.intp) | ||
|
||
if len(labels) == 1: | ||
return get_group_index_sorter(labels[0]) | ||
|
@@ -4144,7 +4153,7 @@ def _get_leaf_sorter(labels): | |
|
||
starts = np.hstack(([True], tic, [True])).nonzero()[0] | ||
lab = ensure_int64(labels[-1]) | ||
return lib.get_level_sorter(lab, ensure_int64(starts)) | ||
return lib.get_level_sorter(lab, ensure_platform_int(starts)) | ||
|
||
if isinstance(self, MultiIndex) and isinstance(other, MultiIndex): | ||
raise TypeError("Join on level between two MultiIndex objects is ambiguous") | ||
|
@@ -4179,12 +4188,12 @@ def _get_leaf_sorter(labels): | |
join_index = left[left_indexer] | ||
|
||
else: | ||
left_lev_indexer = ensure_int64(left_lev_indexer) | ||
left_lev_indexer = ensure_platform_int(left_lev_indexer) | ||
rev_indexer = lib.get_reverse_indexer(left_lev_indexer, len(old_level)) | ||
old_codes = left.codes[level] | ||
new_lev_codes = algos.take_nd( | ||
rev_indexer, old_codes[old_codes != -1], allow_fill=False | ||
) | ||
|
||
taker = old_codes[old_codes != -1] | ||
new_lev_codes = rev_indexer.take(taker) | ||
|
||
new_codes = list(left.codes) | ||
new_codes[level] = new_lev_codes | ||
|
@@ -4197,17 +4206,19 @@ def _get_leaf_sorter(labels): | |
mask = new_lev_codes != -1 | ||
if not mask.all(): | ||
new_codes = [lab[mask] for lab in new_codes] | ||
left_indexer = left_indexer[mask] | ||
# error: Value of type "Optional[ndarray]" is not indexable | ||
left_indexer = left_indexer[mask] # type: ignore[index] | ||
|
||
else: # tie out the order with other | ||
if level == 0: # outer most level, take the fast route | ||
ngroups = 1 + new_lev_codes.max() | ||
left_indexer, counts = libalgos.groupsort_indexer( | ||
new_lev_codes, ngroups | ||
ensure_int64(new_lev_codes), ngroups | ||
) | ||
|
||
# missing values are placed first; drop them! | ||
left_indexer = left_indexer[counts[0] :] | ||
# error: Value of type "Optional[ndarray]" is not indexable | ||
left_indexer = left_indexer[counts[0] :] # type: ignore[index] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could maybe leave this one as a "fix later" ignore instead of a cast as we can fix ourselves by typing |
||
new_codes = [lab[left_indexer] for lab in new_codes] | ||
|
||
else: # sort the leaves | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np.arange resolves to
Any
. https://github.com/numpy/numpy/blob/main/numpy/__init__.pyias a result
left_indexer
is not narrowed withleft_indexer = np.arange(len(left), dtype=np.intp)
(L4215 after merge master)so I think in this case better to cast than add an ignore. (We have
warn_redundant_casts = True
in setup.cfg) so should be aware when the cast is no longer required)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated + green