Skip to content

Commit 1567d9e

Browse files
Add diff() and round() methods for Index and a test for each (#52551)
* Add diff() and round() methods for Index and a test for each * Fix code style * Fix code style * Add pytest.mark.parametrize and fix docstring style * Move Index call * Fix diff() and round() call * Change Index to float * Fix docstring style * Fix docstring style * Fix pre-commit errors * Add example section in docstring and change return statement * Fix docstring example section * Fix docstring * Add whatsnew in v2.1.0.rst * Fix whatsnew entry * Resolve conflict * Resolve conflict * Add diff() and round() methods for Index and a test for each * Fix code style * Fix code style * Add pytest.mark.parametrize and fix docstring style * Move Index call * Fix diff() and round() call * Change Index to float * Fix docstring style * Fix docstring style * Fix pre-commit errors * Add example section in docstring and change return statement * Fix docstring example section * Fix docstring * Add whatsnew in v2.1.0.rst * Fix whatsnew entry * Add whatsnew entry * Fix whatsnew entry * Add blank line in v2.1.0.rst
1 parent 34b3e7f commit 1567d9e

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Other enhancements
8282
- :meth:`MultiIndex.sortlevel` and :meth:`Index.sortlevel` gained a new keyword ``na_position`` (:issue:`51612`)
8383
- :meth:`arrays.DatetimeArray.map`, :meth:`arrays.TimedeltaArray.map` and :meth:`arrays.PeriodArray.map` can now take a ``na_action`` argument (:issue:`51644`)
8484
- :meth:`arrays.SparseArray.map` now supports ``na_action`` (:issue:`52096`).
85+
- Add :meth:`diff()` and :meth:`round()` for :class:`Index` (:issue:`19708`)
8586
- Add dtype of categories to ``repr`` information of :class:`CategoricalDtype` (:issue:`52179`)
8687
- Added to the escape mode "latex-math" preserving without escaping all characters between "\(" and "\)" in formatter (:issue:`51903`)
8788
- Adding ``engine_kwargs`` parameter to :meth:`DataFrame.read_excel` (:issue:`52214`)

pandas/core/indexes/base.py

+53
Original file line numberDiff line numberDiff line change
@@ -6758,6 +6758,59 @@ def infer_objects(self, copy: bool = True) -> Index:
67586758
result._references.add_index_reference(result)
67596759
return result
67606760

6761+
def diff(self, periods: int = 1):
6762+
"""
6763+
Computes the difference between consecutive values in the Index object.
6764+
6765+
If periods is greater than 1, computes the difference between values that
6766+
are `periods` number of positions apart.
6767+
6768+
Parameters
6769+
----------
6770+
periods : int, optional
6771+
The number of positions between the current and previous
6772+
value to compute the difference with. Default is 1.
6773+
6774+
Returns
6775+
-------
6776+
Index
6777+
A new Index object with the computed differences.
6778+
6779+
Examples
6780+
--------
6781+
>>> import pandas as pd
6782+
>>> idx = pd.Index([10, 20, 30, 40, 50])
6783+
>>> idx.diff()
6784+
Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64')
6785+
6786+
"""
6787+
return self._constructor(self.to_series().diff(periods))
6788+
6789+
def round(self, decimals: int = 0):
6790+
"""
6791+
Round each value in the Index to the given number of decimals.
6792+
6793+
Parameters
6794+
----------
6795+
decimals : int, optional
6796+
Number of decimal places to round to. If decimals is negative,
6797+
it specifies the number of positions to the left of the decimal point.
6798+
6799+
Returns
6800+
-------
6801+
Index
6802+
A new Index with the rounded values.
6803+
6804+
Examples
6805+
--------
6806+
>>> import pandas as pd
6807+
>>> idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890])
6808+
>>> idx.round(decimals=2)
6809+
Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64')
6810+
6811+
"""
6812+
return self._constructor(self.to_series().round(decimals))
6813+
67616814
# --------------------------------------------------------------------
67626815
# Generated Arithmetic, Comparison, and Unary Methods
67636816

pandas/tests/indexes/test_base.py

+32
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,38 @@ def test_sortlevel_na_position(self):
12701270
expected = Index([np.nan, 1])
12711271
tm.assert_index_equal(result, expected)
12721272

1273+
@pytest.mark.parametrize(
1274+
"periods, expected_results",
1275+
[
1276+
(1, [np.nan, 10, 10, 10, 10]),
1277+
(2, [np.nan, np.nan, 20, 20, 20]),
1278+
(3, [np.nan, np.nan, np.nan, 30, 30]),
1279+
],
1280+
)
1281+
def test_index_diff(self, periods, expected_results):
1282+
# GH#19708
1283+
idx = Index([10, 20, 30, 40, 50])
1284+
result = idx.diff(periods)
1285+
expected = Index(expected_results)
1286+
1287+
tm.assert_index_equal(result, expected)
1288+
1289+
@pytest.mark.parametrize(
1290+
"decimals, expected_results",
1291+
[
1292+
(0, [1.0, 2.0, 3.0]),
1293+
(1, [1.2, 2.3, 3.5]),
1294+
(2, [1.23, 2.35, 3.46]),
1295+
],
1296+
)
1297+
def test_index_round(self, decimals, expected_results):
1298+
# GH#19708
1299+
idx = Index([1.234, 2.345, 3.456])
1300+
result = idx.round(decimals)
1301+
expected = Index(expected_results)
1302+
1303+
tm.assert_index_equal(result, expected)
1304+
12731305

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

0 commit comments

Comments
 (0)