Skip to content

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

Merged
merged 37 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f18e96b
Add diff() and round() methods for Index and a test for each
GrammatikakisDimitris Apr 9, 2023
c6b63d2
Fix code style
GrammatikakisDimitris Apr 9, 2023
6329f8c
Fix code style
GrammatikakisDimitris Apr 9, 2023
3ce1f72
Add pytest.mark.parametrize and fix docstring style
GrammatikakisDimitris Apr 10, 2023
57032c9
Move Index call
GrammatikakisDimitris Apr 10, 2023
9f46805
Fix diff() and round() call
GrammatikakisDimitris Apr 11, 2023
76742e4
Change Index to float
GrammatikakisDimitris Apr 11, 2023
1eeab2a
Fix docstring style
GrammatikakisDimitris Apr 11, 2023
fe713fd
Fix docstring style
GrammatikakisDimitris Apr 11, 2023
837ab45
Fix pre-commit errors
GrammatikakisDimitris Apr 11, 2023
8925aba
Add example section in docstring and change return statement
GrammatikakisDimitris Apr 11, 2023
7390230
Fix docstring example section
GrammatikakisDimitris Apr 11, 2023
8cce642
Fix docstring
GrammatikakisDimitris Apr 11, 2023
4a9cca3
Add whatsnew in v2.1.0.rst
GrammatikakisDimitris Apr 13, 2023
d7eaf04
Fix whatsnew entry
GrammatikakisDimitris Apr 13, 2023
22b3e59
Resolve conflict
GrammatikakisDimitris Apr 13, 2023
50a5181
Resolve conflict
GrammatikakisDimitris Apr 13, 2023
06b8ff8
Add diff() and round() methods for Index and a test for each
GrammatikakisDimitris Apr 9, 2023
bedf16d
Fix code style
GrammatikakisDimitris Apr 9, 2023
61086a6
Fix code style
GrammatikakisDimitris Apr 9, 2023
364ae36
Add pytest.mark.parametrize and fix docstring style
GrammatikakisDimitris Apr 10, 2023
3f56973
Move Index call
GrammatikakisDimitris Apr 10, 2023
d96196d
Fix diff() and round() call
GrammatikakisDimitris Apr 11, 2023
5a65334
Change Index to float
GrammatikakisDimitris Apr 11, 2023
db30757
Fix docstring style
GrammatikakisDimitris Apr 11, 2023
2b105e9
Fix docstring style
GrammatikakisDimitris Apr 11, 2023
47e9866
Fix pre-commit errors
GrammatikakisDimitris Apr 11, 2023
69d32f7
Add example section in docstring and change return statement
GrammatikakisDimitris Apr 11, 2023
5475c37
Fix docstring example section
GrammatikakisDimitris Apr 11, 2023
960540c
Fix docstring
GrammatikakisDimitris Apr 11, 2023
e79323f
Add whatsnew in v2.1.0.rst
GrammatikakisDimitris Apr 13, 2023
cea737a
Fix whatsnew entry
GrammatikakisDimitris Apr 13, 2023
ca5cd93
Resolve conflict
GrammatikakisDimitris Apr 13, 2023
600d46b
Add whatsnew entry
GrammatikakisDimitris Apr 13, 2023
431c4b2
Fix whatsnew entry
GrammatikakisDimitris Apr 13, 2023
56d92af
Merge remote-tracking branch 'upstream/main' into pandas_branch_1
GrammatikakisDimitris Apr 15, 2023
a428bc8
Add blank line in v2.1.0.rst
GrammatikakisDimitris Apr 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Other enhancements
- :meth:`MultiIndex.sortlevel` and :meth:`Index.sortlevel` gained a new keyword ``na_position`` (:issue:`51612`)
- :meth:`arrays.DatetimeArray.map`, :meth:`arrays.TimedeltaArray.map` and :meth:`arrays.PeriodArray.map` can now take a ``na_action`` argument (:issue:`51644`)
- :meth:`arrays.SparseArray.map` now supports ``na_action`` (:issue:`52096`).
- Add :meth:`diff()` and :meth:`round()` for :class:`Index` (:issue:`19708`)
- Add dtype of categories to ``repr`` information of :class:`CategoricalDtype` (:issue:`52179`)
- Added to the escape mode "latex-math" preserving without escaping all characters between "\(" and "\)" in formatter (:issue:`51903`)
- Adding ``engine_kwargs`` parameter to :meth:`DataFrame.read_excel` (:issue:`52214`)
Expand Down
53 changes: 53 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6758,6 +6758,59 @@ 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, optional
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.

Examples
--------
>>> import pandas as pd
>>> idx = pd.Index([10, 20, 30, 40, 50])
>>> idx.diff()
Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64')

"""
return self._constructor(self.to_series().diff(periods))

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.

Examples
--------
>>> import pandas as pd
>>> idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890])
>>> idx.round(decimals=2)
Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64')

"""
return self._constructor(self.to_series().round(decimals))

# --------------------------------------------------------------------
# Generated Arithmetic, Comparison, and Unary Methods

Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,38 @@ def test_sortlevel_na_position(self):
expected = Index([np.nan, 1])
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"periods, expected_results",
[
(1, [np.nan, 10, 10, 10, 10]),
(2, [np.nan, np.nan, 20, 20, 20]),
(3, [np.nan, np.nan, np.nan, 30, 30]),
],
)
def test_index_diff(self, periods, expected_results):
# GH#19708
idx = Index([10, 20, 30, 40, 50])
result = idx.diff(periods)
expected = Index(expected_results)

tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"decimals, expected_results",
[
(0, [1.0, 2.0, 3.0]),
(1, [1.2, 2.3, 3.5]),
(2, [1.23, 2.35, 3.46]),
],
)
def test_index_round(self, decimals, expected_results):
# GH#19708
idx = Index([1.234, 2.345, 3.456])
result = idx.round(decimals)
expected = Index(expected_results)

tm.assert_index_equal(result, expected)


class TestMixedIntIndex(Base):
# Mostly the tests from common.py for which the results differ
Expand Down