-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: RangeIndex.sort_values now avoids materialization of values #43666
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
Conversation
This reverts commit 6194a85.
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.
and pls add a whatsnew note in performance section
Hello @usersblock! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found: There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻 Comment last updated at 2021-10-19 22:06:41 UTC |
cc @jbrockmendel if comments |
i think in test_algos.TestFactorize.test_factorize with this PR you could pass |
pandas/core/indexes/range.py
Outdated
sorted_index = self | ||
if ascending: | ||
if self.step < 0: | ||
sorted_index = RangeIndex( |
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.
would this be simpler as just self[::-1]
?
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.
Done
pandas/core/indexes/range.py
Outdated
indexer = RangeIndex(start=self.size - 1, stop=-1, step=-1) | ||
else: | ||
if self.step > 0: | ||
sorted_index = RangeIndex( |
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.
ditto self[::-1]
?
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.
Done
pandas/core/indexes/range.py
Outdated
key: Callable | None = None, | ||
): | ||
sorted_index = self | ||
indexer = RangeIndex(start=0, stop=self.size, step=1) |
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.
RangeIndex(range(len(self))
?
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.
Done
pandas/core/indexes/range.py
Outdated
stop=self.start - self.step, | ||
step=self.step * -1, | ||
) | ||
indexer = RangeIndex(start=self.size - 1, stop=-1, step=-1) |
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.
indexer[::-1]
since indexer is defined above?
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.
Done
thanks @usersblock very nice! |
sort_order = {8: 2, 6: 0, 4: 8, 2: 10, 0: 12} | ||
values = RangeIndex(0, 10, 2) | ||
result = values.sort_values(key=lambda x: x.map(sort_order)) | ||
expected = Index([4, 8, 6, 0, 2], dtype="int64") |
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.
@usersblock where did you get this expected
from? it doesn't match what we'd get if we converted to Series before doing sort_values
RangeIndex.sort_values
can avoid materialization of values. #43584What's new: On any instance of RangeIndex, sort_values will not sort as it is already sorted.