Skip to content

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

Merged
merged 57 commits into from
Oct 21, 2021

Conversation

usersblock
Copy link
Contributor

@jreback jreback added this to the 1.4 milestone Oct 6, 2021
Copy link
Contributor

@jreback jreback left a 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

@pep8speaks
Copy link

pep8speaks commented Oct 6, 2021

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

@jreback
Copy link
Contributor

jreback commented Oct 7, 2021

cc @jbrockmendel if comments

@jbrockmendel
Copy link
Member

i think in test_algos.TestFactorize.test_factorize with this PR you could pass exact=True to assert_index_equal

sorted_index = self
if ascending:
if self.step < 0:
sorted_index = RangeIndex(
Copy link
Member

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]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

indexer = RangeIndex(start=self.size - 1, stop=-1, step=-1)
else:
if self.step > 0:
sorted_index = RangeIndex(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto self[::-1]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

key: Callable | None = None,
):
sorted_index = self
indexer = RangeIndex(start=0, stop=self.size, step=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RangeIndex(range(len(self))?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

stop=self.start - self.step,
step=self.step * -1,
)
indexer = RangeIndex(start=self.size - 1, stop=-1, step=-1)
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jreback jreback merged commit 755b917 into pandas-dev:master Oct 21, 2021
@jreback
Copy link
Contributor

jreback commented Oct 21, 2021

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")
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Index Related to the Index class or subclasses Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: RangeIndex.sort_values can avoid materialization of values.
4 participants