-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Add diff() and round() methods for Index and a test for each #52551
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
Add diff() and round() methods for Index and a test for each #52551
Conversation
pandas/core/indexes/base.py
Outdated
If periods is greater than 1, computes the difference between values that | ||
are `periods` number of positions apart. | ||
|
||
Parameters: |
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.
Looks the formatting here is off
pandas/tests/indexes/test_base.py
Outdated
# GH#19708 | ||
idx = Index([10, 20, 30, 40, 50]) | ||
diffs = idx.diff() | ||
diffs_period2 = idx.diff(periods=2) |
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.
Could you use pytest.mark.parameterize
to test different periods?
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 you like pytest.mark.parametrize for the round_test as well?
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.
Yes please
pandas/tests/indexes/test_base.py
Outdated
diffs = idx.diff() | ||
diffs_period2 = idx.diff(periods=2) | ||
|
||
assert diffs.equals(Index([np.nan, 10, 10, 10, 10])) |
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.
Could you make an expected =
variable and then call tm.assert_index_equal(result, expected)
?
pandas/tests/indexes/test_base.py
Outdated
@pytest.mark.parametrize( | ||
"periods, expected", | ||
[ | ||
(1, Index([np.nan, 10, 10, 10, 10])), |
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.
Could you move the Index
call to the body of the test (same as below)
Forgot to pass parameters periods and decimals in the call of the functions, i will fix this. |
pandas/core/indexes/base.py
Outdated
A new Index object with the computed differences. | ||
|
||
""" | ||
return Index(self.to_series().diff(periods)) |
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.
return Index(self.to_series().diff(periods)) | |
return self._constructor(self.to_series().diff(periods)) |
pandas/core/indexes/base.py
Outdated
A new Index with the rounded values. | ||
|
||
""" | ||
return Index(self.to_series().round(decimals)) |
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.
return Index(self.to_series().round(decimals)) | |
return self._constructor(self.to_series().round(decimals)) |
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/base.py
Outdated
>>> diff_idx = idx.diff() | ||
>>> print(diff_idx) |
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.
>>> diff_idx = idx.diff() | |
>>> print(diff_idx) | |
>>> idx.diff() |
pandas/core/indexes/base.py
Outdated
>>> print(diff_idx) | ||
Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') | ||
|
||
This example creates a new `Index` object `idx` with values |
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.
The rest of this explanation is not needed
pandas/core/indexes/base.py
Outdated
Example | ||
------- |
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.
Example | |
------- | |
Examples | |
-------- |
pandas/core/indexes/base.py
Outdated
>>> rounded_idx = idx.round(decimals=2) | ||
>>> print(rounded_idx) |
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.
>>> rounded_idx = idx.round(decimals=2) | |
>>> print(rounded_idx) | |
>>> idx.round(decimals=2) |
pandas/core/indexes/base.py
Outdated
Example | ||
------- |
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.
Example | |
------- | |
Examples | |
-------- |
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.
Looks good. Could you add a whatsnew entry in v2.1.0.rst
?
What should i do about the conflict? |
The conflict should be resolved without removing someone else's contribution |
Sorry for the mess, i pulled all the changes to fix the conflict. If you like i can open a new PR. |
I think all you need to do is
into this branch |
Thanks @GrammatikakisDimitris |
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.