-
-
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
Changes from 4 commits
f18e96b
c6b63d2
6329f8c
3ce1f72
57032c9
9f46805
76742e4
1eeab2a
fe713fd
837ab45
8925aba
7390230
8cce642
4a9cca3
d7eaf04
22b3e59
50a5181
06b8ff8
bedf16d
61086a6
364ae36
3f56973
d96196d
5a65334
db30757
2b105e9
47e9866
69d32f7
5475c37
960540c
e79323f
cea737a
ca5cd93
600d46b
431c4b2
56d92af
a428bc8
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 | ||||
---|---|---|---|---|---|---|
|
@@ -6756,6 +6756,42 @@ def infer_objects(self, copy: bool = True) -> Index: | |||||
result._references.add_index_reference(result) | ||||||
return result | ||||||
|
||||||
def diff(self, periods: int = 1): | ||||||
""" | ||||||
Computes the difference between consecutive values in the Index object. | ||||||
|
||||||
If periods is greater than 1, computes the difference between values that | ||||||
are `periods` number of positions apart. | ||||||
|
||||||
Parameters: | ||||||
periods (int): The number of positions between the current and previous | ||||||
value to compute the difference with. Default is 1. | ||||||
|
||||||
Returns: | ||||||
Index | ||||||
A new Index object with the computed differences. | ||||||
|
||||||
mroeschke marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
""" | ||||||
return Index(self.to_series().diff(periods)) | ||||||
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
|
||||||
|
||||||
def round(self, decimals: int = 0): | ||||||
""" | ||||||
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 | ||||||
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 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. Done |
||||||
|
||||||
# -------------------------------------------------------------------- | ||||||
# Generated Arithmetic, Comparison, and Unary Methods | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1270,6 +1270,36 @@ def test_sortlevel_na_position(self): | |
expected = Index([np.nan, 1]) | ||
tm.assert_index_equal(result, expected) | ||
|
||
@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 commentThe reason will be displayed to describe this comment to others. Learn more. Could you move the |
||
(2, Index([np.nan, np.nan, 20, 20, 20])), | ||
(3, Index([np.nan, np.nan, np.nan, 30, 30])), | ||
], | ||
) | ||
def test_index_diff(self, periods, expected): | ||
# GH#19708 | ||
idx = Index([10, 20, 30, 40, 50]) | ||
result = idx.diff() | ||
|
||
tm.assert_index_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"decimals, expected", | ||
[ | ||
(0, Index([1, 2, 3])), | ||
(1, Index([1.2, 2.3, 3.5])), | ||
(2, Index([1.23, 2.35, 3.46])), | ||
], | ||
) | ||
def test_index_round(self, decimals, expected): | ||
# GH#19708 | ||
idx = Index([1.234, 2.345, 3.456]) | ||
result = idx.round() | ||
|
||
tm.assert_index_equal(result, expected) | ||
|
||
|
||
class TestMixedIntIndex(Base): | ||
# Mostly the tests from common.py for which the results differ | ||
|
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