-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 5 commits
35c9d47
f8a3798
ea4688b
203b330
bac634f
8fa8683
8cb0c8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1165,6 +1165,43 @@ def any(self, *args, **kwargs) -> bool: | |||||
|
||||||
# -------------------------------------------------------------------- | ||||||
|
||||||
# error: Return type "RangeIndex | Index" of "round" incompatible with | ||||||
# return type "RangeIndex" in supertype "Index" | ||||||
def round(self, decimals: int = 0) -> Self | Index: # type: ignore[override] | ||||||
""" | ||||||
Round each value in the Index to the given number of decimals. | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
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. | ||||||
|
||||||
Returns | ||||||
------- | ||||||
Index or RangeIndex | ||||||
A new Index with the rounded values. | ||||||
|
||||||
Examples | ||||||
-------- | ||||||
>>> import pandas as pd | ||||||
>>> idx = pd.RangeIndex(10, 30, 10) | ||||||
>>> idx.round(decimals=-1) | ||||||
RangeIndex(start=10, stop=30, step=10) | ||||||
>>> idx = pd.RangeIndex(10, 15, 1) | ||||||
>>> idx.round(decimals=-1) | ||||||
Index([10, 10, 10, 10, 10], dtype='int64') | ||||||
""" | ||||||
if decimals >= 0: | ||||||
return self.copy() | ||||||
elif all( | ||||||
getattr(self, attr) % 10**-decimals == 0 for attr in ("start", "step") | ||||||
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. Personal preference, but I'd find more readable to simply use 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. Fair point. I'll change this to your suggestion |
||||||
): | ||||||
# e.g. Range(10, 30, 10).round(-1) doesn't need rounding | ||||||
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.
Suggested change
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. Thanks! |
||||||
return self.copy() | ||||||
else: | ||||||
return super().round(decimals=decimals) | ||||||
|
||||||
def _cmp_method(self, other, op): | ||||||
if isinstance(other, RangeIndex) and self._range == other._range: | ||||||
# Both are immutable so if ._range attr. are equal, shortcut is possible | ||||||
|
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.
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.
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.
Sure thing. I added an example for the negative case.