Skip to content

PERF: RangeIndex.round returns RangeIndex when possible #57824

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 7 commits into from
Mar 13, 2024

Conversation

mroeschke
Copy link
Member

No description provided.

@mroeschke mroeschke added Performance Memory or execution speed performance Index Related to the Index class or subclasses labels Mar 12, 2024
Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

Looks good to me, added few suggestions.

if decimals >= 0:
return self.copy()
elif all(
getattr(self, attr) % 10**-decimals == 0 for attr in ("start", "step")
Copy link
Member

Choose a reason for hiding this comment

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

Personal preference, but I'd find more readable to simply use self.start % 10 ... and self.stop % 10 ....

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair point. I'll change this to your suggestion

elif all(
getattr(self, attr) % 10**-decimals == 0 for attr in ("start", "step")
):
# e.g. Range(10, 30, 10).round(-1) doesn't need rounding
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# e.g. Range(10, 30, 10).round(-1) doesn't need rounding
# e.g. RangeIndex(10, 30, 10).round(-1) doesn't need rounding

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

----------
decimals : int, optional
Number of decimal places to round to. If decimals is negative,
it specifies the number of positions to the left of the decimal point.
Copy link
Member

Choose a reason for hiding this comment

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

I know this is from the original docstring, but for me it's not obvious what this means by reading it, maybe adding an example or two here would be helpful.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure thing. I added an example for the negative case.

@mroeschke mroeschke added this to the 3.0 milestone Mar 13, 2024
@mroeschke mroeschke merged commit 3132971 into pandas-dev:main Mar 13, 2024
47 checks passed
@mroeschke mroeschke deleted the perf/ri/round branch March 13, 2024 19:32
pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
…7824)

* PERF: RangeIndex.round returns RangeIndex when possible

* Add whatsnew

* Add typing

* Address feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

2 participants