From f18e96bda6171e451a2d3565d18930586bb3b644 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Sun, 9 Apr 2023 20:25:50 +0000 Subject: [PATCH 01/35] Add diff() and round() methods for Index and a test for each --- pandas/core/indexes/base.py | 35 +++++++++++++++++++++++++++++++ pandas/tests/indexes/test_base.py | 18 ++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 8b191897e9409..2014c784861f1 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6756,6 +6756,41 @@ def infer_objects(self, copy: bool = True) -> Index: result._references.add_index_reference(result) return result + def diff(self, periods=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. + + """ + return Index(self.to_series().diff(periods)) + + def round(self, decimals=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)) + # -------------------------------------------------------------------- # Generated Arithmetic, Comparison, and Unary Methods diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index e681223933abb..a69a50fc82bc6 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1270,6 +1270,24 @@ def test_sortlevel_na_position(self): expected = Index([np.nan, 1]) tm.assert_index_equal(result, expected) + def test_index_diff(self): + # GH#19708 + idx = Index([10, 20, 30, 40, 50]) + diffs = idx.diff() + diffs_period2 = idx.diff(periods=2) + + assert diffs.equals(Index([np.nan, 10, 10, 10, 10])) + assert diffs_period2.equals(Index([np.nan, np.nan, 20, 20, 20])) + + def test_index_round(self): + # GH#19708 + idx = Index([1.234, 2.345, 3.456]) + rounded = idx.round() + rounded_2decimals = idx.round(2) + + assert rounded.equals(Index([1, 2, 3])) + assert rounded_2decimals.equals(Index([1.23, 2.35, 3.46])) + class TestMixedIntIndex(Base): # Mostly the tests from common.py for which the results differ From c6b63d2071ba211602f8db4b213ce79da6559ae0 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Sun, 9 Apr 2023 21:07:13 +0000 Subject: [PATCH 02/35] Fix code style --- pandas/core/indexes/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2014c784861f1..f8cdd9d01ab0f 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6756,7 +6756,7 @@ def infer_objects(self, copy: bool = True) -> Index: result._references.add_index_reference(result) return result - def diff(self, periods=1): + 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 @@ -6773,7 +6773,7 @@ def diff(self, periods=1): """ return Index(self.to_series().diff(periods)) - def round(self, decimals=0): + def round(self, decimals: int=0): """ Round each value in the Index to the given number of decimals. From 6329f8c47de3614e51f429e8097b089c12c7b613 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Sun, 9 Apr 2023 21:15:17 +0000 Subject: [PATCH 03/35] Fix code style --- pandas/core/indexes/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f8cdd9d01ab0f..bebef4528548f 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6756,7 +6756,7 @@ def infer_objects(self, copy: bool = True) -> Index: result._references.add_index_reference(result) return result - def diff(self, periods: int=1): + 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 @@ -6773,7 +6773,7 @@ def diff(self, periods: int=1): """ return Index(self.to_series().diff(periods)) - def round(self, decimals: int=0): + def round(self, decimals: int = 0): """ Round each value in the Index to the given number of decimals. From 3ce1f726015d8d93d2063a0b38fb09ae1ddb3ecf Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Mon, 10 Apr 2023 23:34:29 +0000 Subject: [PATCH 04/35] Add pytest.mark.parametrize and fix docstring style --- pandas/core/indexes/base.py | 1 + pandas/tests/indexes/test_base.py | 32 +++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index bebef4528548f..b54af796ba0f8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6759,6 +6759,7 @@ def infer_objects(self, copy: bool = True) -> Index: 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. diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index a69a50fc82bc6..ffa0928c802bf 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1270,23 +1270,35 @@ def test_sortlevel_na_position(self): expected = Index([np.nan, 1]) tm.assert_index_equal(result, expected) - def test_index_diff(self): + @pytest.mark.parametrize( + "periods, expected", + [ + (1, Index([np.nan, 10, 10, 10, 10])), + (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]) - diffs = idx.diff() - diffs_period2 = idx.diff(periods=2) + result = idx.diff() - assert diffs.equals(Index([np.nan, 10, 10, 10, 10])) - assert diffs_period2.equals(Index([np.nan, np.nan, 20, 20, 20])) + tm.assert_index_equal(result, expected) - def test_index_round(self): + @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]) - rounded = idx.round() - rounded_2decimals = idx.round(2) + result = idx.round() - assert rounded.equals(Index([1, 2, 3])) - assert rounded_2decimals.equals(Index([1.23, 2.35, 3.46])) + tm.assert_index_equal(result, expected) class TestMixedIntIndex(Base): From 57032c9d944f7fc50e6923e13fc67a6f959f2452 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Mon, 10 Apr 2023 23:51:30 +0000 Subject: [PATCH 05/35] Move Index call --- pandas/tests/indexes/test_base.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index ffa0928c802bf..fb60a1be176ed 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1271,32 +1271,34 @@ def test_sortlevel_na_position(self): tm.assert_index_equal(result, expected) @pytest.mark.parametrize( - "periods, expected", + "periods, expected_results", [ - (1, Index([np.nan, 10, 10, 10, 10])), - (2, Index([np.nan, np.nan, 20, 20, 20])), - (3, Index([np.nan, np.nan, np.nan, 30, 30])), + (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): + def test_index_diff(self, periods, expected_results): # GH#19708 idx = Index([10, 20, 30, 40, 50]) result = idx.diff() + expected = Index(expected_results) tm.assert_index_equal(result, expected) @pytest.mark.parametrize( - "decimals, expected", + "decimals, expected_results", [ - (0, Index([1, 2, 3])), - (1, Index([1.2, 2.3, 3.5])), - (2, Index([1.23, 2.35, 3.46])), + (0, [1, 2, 3]), + (1, [1.2, 2.3, 3.5]), + (2, [1.23, 2.35, 3.46]), ], ) - def test_index_round(self, decimals, expected): + def test_index_round(self, decimals, expected_results): # GH#19708 idx = Index([1.234, 2.345, 3.456]) result = idx.round() + expected = Index(expected_results) tm.assert_index_equal(result, expected) From 9f4680570d0cb8c9c2c321d95a7cb5a398cbc64d Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 12:04:41 +0000 Subject: [PATCH 06/35] Fix diff() and round() call --- pandas/tests/indexes/test_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index fb60a1be176ed..790534f35abd5 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1281,7 +1281,7 @@ def test_sortlevel_na_position(self): def test_index_diff(self, periods, expected_results): # GH#19708 idx = Index([10, 20, 30, 40, 50]) - result = idx.diff() + result = idx.diff(periods) expected = Index(expected_results) tm.assert_index_equal(result, expected) @@ -1297,7 +1297,7 @@ def test_index_diff(self, periods, expected_results): def test_index_round(self, decimals, expected_results): # GH#19708 idx = Index([1.234, 2.345, 3.456]) - result = idx.round() + result = idx.round(decimals) expected = Index(expected_results) tm.assert_index_equal(result, expected) From 76742e42bc8f192495bb5210808a42345eea38a5 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 12:38:28 +0000 Subject: [PATCH 07/35] Change Index to float --- pandas/tests/indexes/test_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 790534f35abd5..db317a819c520 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1289,7 +1289,7 @@ def test_index_diff(self, periods, expected_results): @pytest.mark.parametrize( "decimals, expected_results", [ - (0, [1, 2, 3]), + (0, [1.0, 2.0, 3.0]), (1, [1.2, 2.3, 3.5]), (2, [1.23, 2.35, 3.46]), ], From 1eeab2a72ddd8087ec497d203a1573e59c36aa82 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 14:13:33 +0000 Subject: [PATCH 08/35] Fix docstring style --- pandas/core/indexes/base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b54af796ba0f8..e0ccae1a63820 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6768,6 +6768,7 @@ def diff(self, periods: int = 1): value to compute the difference with. Default is 1. Returns: + ------- Index A new Index object with the computed differences. From fe713fd5abccad5700e8dbe8fd0a7299ae3c1bf7 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 15:46:08 +0000 Subject: [PATCH 09/35] Fix docstring style --- pandas/core/indexes/base.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index e0ccae1a63820..a74c7faea0e12 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6763,11 +6763,13 @@ def diff(self, periods: int = 1): 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 + Parameters + ---------- + periods : int, optional + The number of positions between the current and previous value to compute the difference with. Default is 1. - Returns: + Returns ------- Index A new Index object with the computed differences. From 837ab45515e9e35e7c16b2d8f38888443dbaa924 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 15:56:34 +0000 Subject: [PATCH 10/35] Fix pre-commit errors --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a74c7faea0e12..022a3b1cfb6e2 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6765,7 +6765,7 @@ def diff(self, periods: int = 1): Parameters ---------- - periods : int, optional + periods : int, optional The number of positions between the current and previous value to compute the difference with. Default is 1. From 8925abaaecbd4d66d94decd6b48755ed76fd0892 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 20:49:07 +0000 Subject: [PATCH 11/35] Add example section in docstring and change return statement --- pandas/core/indexes/base.py | 39 +++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 022a3b1cfb6e2..821f825c6aeca 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6774,8 +6774,27 @@ def diff(self, periods: int = 1): Index A new Index object with the computed differences. + Example + ------- + >>> import pandas as pd + >>> idx = pd.Index([10, 20, 30, 40, 50]) + >>> diff_idx = idx.diff() + >>> print(diff_idx) + Float64Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') + + This example creates a new `Index` object `idx` with values + `[10, 20, 30, 40, 50]`. + + The `diff` method is then used to compute the difference between + consecutive values in the `Index` object. + + The resulting `diff_idx` object has the values `[nan, 10.0, 10.0, 10.0, 10.0]`. + + Note that the first value is `nan` since there is no previous value to compute + the difference with. + """ - return Index(self.to_series().diff(periods)) + return self._constructor(self.to_series().diff(periods)) def round(self, decimals: int = 0): """ @@ -6792,8 +6811,24 @@ def round(self, decimals: int = 0): Index A new Index with the rounded values. + Example + ------- + >>> import pandas as pd + >>> idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) + >>> rounded_idx = idx.round(decimals=2) + >>> print(rounded_idx) + Float64Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64') + + This example creates a new `Index` object `idx` with values + `[10.1234, 20.5678, 30.9123, 40.4567, 50.7890]`. + + The `round` method is then used to round each value to two decimal places. + + The resulting `rounded_idx` object has the values + `[10.12, 20.57, 30.91, 40.46, 50.79]`. + """ - return Index(self.to_series().round(decimals)) + return self._constructor(self.to_series().round(decimals)) # -------------------------------------------------------------------- # Generated Arithmetic, Comparison, and Unary Methods From 73902304b730206eaeebb16b1587cee27186be34 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 21:30:51 +0000 Subject: [PATCH 12/35] Fix docstring example section --- pandas/core/indexes/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 821f825c6aeca..12bd44816a8fb 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6780,7 +6780,7 @@ def diff(self, periods: int = 1): >>> idx = pd.Index([10, 20, 30, 40, 50]) >>> diff_idx = idx.diff() >>> print(diff_idx) - Float64Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') + Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') This example creates a new `Index` object `idx` with values `[10, 20, 30, 40, 50]`. @@ -6817,7 +6817,7 @@ def round(self, decimals: int = 0): >>> idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) >>> rounded_idx = idx.round(decimals=2) >>> print(rounded_idx) - Float64Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64') + Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64') This example creates a new `Index` object `idx` with values `[10.1234, 20.5678, 30.9123, 40.4567, 50.7890]`. From 8cce64207ecd03dcea7e63d6df54d64f7a5c2ac4 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 23:57:12 +0000 Subject: [PATCH 13/35] Fix docstring --- pandas/core/indexes/base.py | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 12bd44816a8fb..785c57a8a6cbf 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6774,25 +6774,13 @@ def diff(self, periods: int = 1): Index A new Index object with the computed differences. - Example - ------- + Examples + -------- >>> import pandas as pd >>> idx = pd.Index([10, 20, 30, 40, 50]) - >>> diff_idx = idx.diff() - >>> print(diff_idx) + >>> idx.diff() Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') - This example creates a new `Index` object `idx` with values - `[10, 20, 30, 40, 50]`. - - The `diff` method is then used to compute the difference between - consecutive values in the `Index` object. - - The resulting `diff_idx` object has the values `[nan, 10.0, 10.0, 10.0, 10.0]`. - - Note that the first value is `nan` since there is no previous value to compute - the difference with. - """ return self._constructor(self.to_series().diff(periods)) @@ -6811,22 +6799,13 @@ def round(self, decimals: int = 0): Index A new Index with the rounded values. - Example - ------- + Examples + -------- >>> import pandas as pd >>> idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) - >>> rounded_idx = idx.round(decimals=2) - >>> print(rounded_idx) + >>> idx.round(decimals=2) Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64') - This example creates a new `Index` object `idx` with values - `[10.1234, 20.5678, 30.9123, 40.4567, 50.7890]`. - - The `round` method is then used to round each value to two decimal places. - - The resulting `rounded_idx` object has the values - `[10.12, 20.57, 30.91, 40.46, 50.79]`. - """ return self._constructor(self.to_series().round(decimals)) From 4a9cca3cfe70c08901a86f2e13618fdede96449f Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Thu, 13 Apr 2023 03:39:50 +0300 Subject: [PATCH 14/35] Add whatsnew in v2.1.0.rst --- doc/source/whatsnew/v2.1.0.rst | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 9f5d6011a7780..0b3a2a005ed95 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -67,6 +67,40 @@ Also, note that :meth:`Categorical.map` implicitly has had its ``na_action`` set This has been deprecated and will :meth:`Categorical.map` in the future change the default to ``na_action=None``, like for all the other array types. +.. _whatsnew_210.enhancements.enhancement3: + +Index now has diff() and round() methods +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The :class:`Index` object in Pandas version 2.1.0 has two new methods: :meth:`diff()` and :meth:`round()`. +These methods are useful for performing computations on the values in an :class:`Index` object (:issue:`19708`). + +The :meth:`diff()` method computes the difference between consecutive values in the index. +If the periods parameter is greater than 1, it computes the difference between values +that are periods number of positions apart. + +For example: + +.. code-block:: python + + import pandas as pd + + idx = pd.Index([10, 20, 30, 40, 50]) + print(idx.diff()) # Output: [nan, 10.0, 10.0, 10.0, 10.0] + +The :meth:`round()` method rounds each value in the index to the given number of decimals. + +For example: + +.. code-block:: python + + import pandas as pd + + idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) + print(idx.round(decimals=2)) # Output: [10.12, 20.57, 30.91, 40.46, 50.79] + +We hope these new methods enhance your experience with Pandas! + .. _whatsnew_210.enhancements.other: Other enhancements From d7eaf04eba7d5c42cf6401140c5590b0fe94c003 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Thu, 13 Apr 2023 04:08:31 +0300 Subject: [PATCH 15/35] Fix whatsnew entry --- doc/source/whatsnew/v2.1.0.rst | 36 +--------------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 0b3a2a005ed95..dc219c51044a8 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -67,40 +67,6 @@ Also, note that :meth:`Categorical.map` implicitly has had its ``na_action`` set This has been deprecated and will :meth:`Categorical.map` in the future change the default to ``na_action=None``, like for all the other array types. -.. _whatsnew_210.enhancements.enhancement3: - -Index now has diff() and round() methods -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -The :class:`Index` object in Pandas version 2.1.0 has two new methods: :meth:`diff()` and :meth:`round()`. -These methods are useful for performing computations on the values in an :class:`Index` object (:issue:`19708`). - -The :meth:`diff()` method computes the difference between consecutive values in the index. -If the periods parameter is greater than 1, it computes the difference between values -that are periods number of positions apart. - -For example: - -.. code-block:: python - - import pandas as pd - - idx = pd.Index([10, 20, 30, 40, 50]) - print(idx.diff()) # Output: [nan, 10.0, 10.0, 10.0, 10.0] - -The :meth:`round()` method rounds each value in the index to the given number of decimals. - -For example: - -.. code-block:: python - - import pandas as pd - - idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) - print(idx.round(decimals=2)) # Output: [10.12, 20.57, 30.91, 40.46, 50.79] - -We hope these new methods enhance your experience with Pandas! - .. _whatsnew_210.enhancements.other: Other enhancements @@ -121,8 +87,8 @@ Other enhancements - :meth:`DataFrame.applymap` now uses the :meth:`~api.extensions.ExtensionArray.map` method of underlying :class:`api.extensions.ExtensionArray` instances (:issue:`52219`) - :meth:`arrays.SparseArray.map` now supports ``na_action`` (:issue:`52096`). - Add dtype of categories to ``repr`` information of :class:`CategoricalDtype` (:issue:`52179`) +- Add :meth:`diff()` and :meth:`round()` for :class:`Index` (:issue:`19708`) - - .. --------------------------------------------------------------------------- .. _whatsnew_210.notable_bug_fixes: From 22b3e59d507c62979c1c0e6dc7816a7957b9c380 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Thu, 13 Apr 2023 04:16:37 +0300 Subject: [PATCH 16/35] Resolve conflict --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index dc219c51044a8..ac2038b4fa722 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -87,6 +87,7 @@ Other enhancements - :meth:`DataFrame.applymap` now uses the :meth:`~api.extensions.ExtensionArray.map` method of underlying :class:`api.extensions.ExtensionArray` instances (:issue:`52219`) - :meth:`arrays.SparseArray.map` now supports ``na_action`` (:issue:`52096`). - Add dtype of categories to ``repr`` information of :class:`CategoricalDtype` (:issue:`52179`) +- Adding ``engine_kwargs`` parameter to :meth:`DataFrame.read_excel` (:issue:`52214`) - Add :meth:`diff()` and :meth:`round()` for :class:`Index` (:issue:`19708`) - .. --------------------------------------------------------------------------- From 50a518138ca0e364a86a00e9871d38f2caad3670 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Thu, 13 Apr 2023 04:21:56 +0300 Subject: [PATCH 17/35] Resolve conflict --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index ac2038b4fa722..f85442b3692c1 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -87,8 +87,8 @@ Other enhancements - :meth:`DataFrame.applymap` now uses the :meth:`~api.extensions.ExtensionArray.map` method of underlying :class:`api.extensions.ExtensionArray` instances (:issue:`52219`) - :meth:`arrays.SparseArray.map` now supports ``na_action`` (:issue:`52096`). - Add dtype of categories to ``repr`` information of :class:`CategoricalDtype` (:issue:`52179`) -- Adding ``engine_kwargs`` parameter to :meth:`DataFrame.read_excel` (:issue:`52214`) - Add :meth:`diff()` and :meth:`round()` for :class:`Index` (:issue:`19708`) +- Adding ``engine_kwargs`` parameter to :meth:`DataFrame.read_excel` (:issue:`52214`) - .. --------------------------------------------------------------------------- .. _whatsnew_210.notable_bug_fixes: From 06b8ff8bb42f6e8f1ccd615bffb771789a1ee7b1 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Sun, 9 Apr 2023 20:25:50 +0000 Subject: [PATCH 18/35] Add diff() and round() methods for Index and a test for each --- pandas/core/indexes/base.py | 35 +++++++++++++++++++++++++++++++ pandas/tests/indexes/test_base.py | 18 ++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 0a56fa4d031d6..78005419e854e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6758,6 +6758,41 @@ def infer_objects(self, copy: bool = True) -> Index: result._references.add_index_reference(result) return result + def diff(self, periods=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. + + """ + return Index(self.to_series().diff(periods)) + + def round(self, decimals=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)) + # -------------------------------------------------------------------- # Generated Arithmetic, Comparison, and Unary Methods diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index e681223933abb..a69a50fc82bc6 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1270,6 +1270,24 @@ def test_sortlevel_na_position(self): expected = Index([np.nan, 1]) tm.assert_index_equal(result, expected) + def test_index_diff(self): + # GH#19708 + idx = Index([10, 20, 30, 40, 50]) + diffs = idx.diff() + diffs_period2 = idx.diff(periods=2) + + assert diffs.equals(Index([np.nan, 10, 10, 10, 10])) + assert diffs_period2.equals(Index([np.nan, np.nan, 20, 20, 20])) + + def test_index_round(self): + # GH#19708 + idx = Index([1.234, 2.345, 3.456]) + rounded = idx.round() + rounded_2decimals = idx.round(2) + + assert rounded.equals(Index([1, 2, 3])) + assert rounded_2decimals.equals(Index([1.23, 2.35, 3.46])) + class TestMixedIntIndex(Base): # Mostly the tests from common.py for which the results differ From bedf16d9ebee18fed390545b5c191fe84750630c Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Sun, 9 Apr 2023 21:07:13 +0000 Subject: [PATCH 19/35] Fix code style --- pandas/core/indexes/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 78005419e854e..e6fffc5f8da77 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6758,7 +6758,7 @@ def infer_objects(self, copy: bool = True) -> Index: result._references.add_index_reference(result) return result - def diff(self, periods=1): + 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 @@ -6775,7 +6775,7 @@ def diff(self, periods=1): """ return Index(self.to_series().diff(periods)) - def round(self, decimals=0): + def round(self, decimals: int=0): """ Round each value in the Index to the given number of decimals. From 61086a645246d1b93c528c17baafefffad0071d2 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Sun, 9 Apr 2023 21:15:17 +0000 Subject: [PATCH 20/35] Fix code style --- pandas/core/indexes/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index e6fffc5f8da77..a9fe8a05c59c7 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6758,7 +6758,7 @@ def infer_objects(self, copy: bool = True) -> Index: result._references.add_index_reference(result) return result - def diff(self, periods: int=1): + 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 @@ -6775,7 +6775,7 @@ def diff(self, periods: int=1): """ return Index(self.to_series().diff(periods)) - def round(self, decimals: int=0): + def round(self, decimals: int = 0): """ Round each value in the Index to the given number of decimals. From 364ae36d6235786784dff39ee6c0c7171b8c5d96 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Mon, 10 Apr 2023 23:34:29 +0000 Subject: [PATCH 21/35] Add pytest.mark.parametrize and fix docstring style --- pandas/core/indexes/base.py | 1 + pandas/tests/indexes/test_base.py | 32 +++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a9fe8a05c59c7..270286177bca0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6761,6 +6761,7 @@ def infer_objects(self, copy: bool = True) -> Index: 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. diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index a69a50fc82bc6..ffa0928c802bf 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1270,23 +1270,35 @@ def test_sortlevel_na_position(self): expected = Index([np.nan, 1]) tm.assert_index_equal(result, expected) - def test_index_diff(self): + @pytest.mark.parametrize( + "periods, expected", + [ + (1, Index([np.nan, 10, 10, 10, 10])), + (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]) - diffs = idx.diff() - diffs_period2 = idx.diff(periods=2) + result = idx.diff() - assert diffs.equals(Index([np.nan, 10, 10, 10, 10])) - assert diffs_period2.equals(Index([np.nan, np.nan, 20, 20, 20])) + tm.assert_index_equal(result, expected) - def test_index_round(self): + @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]) - rounded = idx.round() - rounded_2decimals = idx.round(2) + result = idx.round() - assert rounded.equals(Index([1, 2, 3])) - assert rounded_2decimals.equals(Index([1.23, 2.35, 3.46])) + tm.assert_index_equal(result, expected) class TestMixedIntIndex(Base): From 3f5697349ab742998e66fafd0f62c99631e739e2 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Mon, 10 Apr 2023 23:51:30 +0000 Subject: [PATCH 22/35] Move Index call --- pandas/tests/indexes/test_base.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index ffa0928c802bf..fb60a1be176ed 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1271,32 +1271,34 @@ def test_sortlevel_na_position(self): tm.assert_index_equal(result, expected) @pytest.mark.parametrize( - "periods, expected", + "periods, expected_results", [ - (1, Index([np.nan, 10, 10, 10, 10])), - (2, Index([np.nan, np.nan, 20, 20, 20])), - (3, Index([np.nan, np.nan, np.nan, 30, 30])), + (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): + def test_index_diff(self, periods, expected_results): # GH#19708 idx = Index([10, 20, 30, 40, 50]) result = idx.diff() + expected = Index(expected_results) tm.assert_index_equal(result, expected) @pytest.mark.parametrize( - "decimals, expected", + "decimals, expected_results", [ - (0, Index([1, 2, 3])), - (1, Index([1.2, 2.3, 3.5])), - (2, Index([1.23, 2.35, 3.46])), + (0, [1, 2, 3]), + (1, [1.2, 2.3, 3.5]), + (2, [1.23, 2.35, 3.46]), ], ) - def test_index_round(self, decimals, expected): + def test_index_round(self, decimals, expected_results): # GH#19708 idx = Index([1.234, 2.345, 3.456]) result = idx.round() + expected = Index(expected_results) tm.assert_index_equal(result, expected) From d96196db267638b49fd0adbae6a6373dea9598b4 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 12:04:41 +0000 Subject: [PATCH 23/35] Fix diff() and round() call --- pandas/tests/indexes/test_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index fb60a1be176ed..790534f35abd5 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1281,7 +1281,7 @@ def test_sortlevel_na_position(self): def test_index_diff(self, periods, expected_results): # GH#19708 idx = Index([10, 20, 30, 40, 50]) - result = idx.diff() + result = idx.diff(periods) expected = Index(expected_results) tm.assert_index_equal(result, expected) @@ -1297,7 +1297,7 @@ def test_index_diff(self, periods, expected_results): def test_index_round(self, decimals, expected_results): # GH#19708 idx = Index([1.234, 2.345, 3.456]) - result = idx.round() + result = idx.round(decimals) expected = Index(expected_results) tm.assert_index_equal(result, expected) From 5a653346c5358404c0320a333ffeddaeed2d3876 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 12:38:28 +0000 Subject: [PATCH 24/35] Change Index to float --- pandas/tests/indexes/test_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 790534f35abd5..db317a819c520 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1289,7 +1289,7 @@ def test_index_diff(self, periods, expected_results): @pytest.mark.parametrize( "decimals, expected_results", [ - (0, [1, 2, 3]), + (0, [1.0, 2.0, 3.0]), (1, [1.2, 2.3, 3.5]), (2, [1.23, 2.35, 3.46]), ], From db307574ac94d17deead6405fb9b6c3241ed795b Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 14:13:33 +0000 Subject: [PATCH 25/35] Fix docstring style --- pandas/core/indexes/base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 270286177bca0..864cea52603b6 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6770,6 +6770,7 @@ def diff(self, periods: int = 1): value to compute the difference with. Default is 1. Returns: + ------- Index A new Index object with the computed differences. From 2b105e9f810a15a8c5d02f4ab82bb1980b9f4f08 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 15:46:08 +0000 Subject: [PATCH 26/35] Fix docstring style --- pandas/core/indexes/base.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 864cea52603b6..3c8a164c88429 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6765,11 +6765,13 @@ def diff(self, periods: int = 1): 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 + Parameters + ---------- + periods : int, optional + The number of positions between the current and previous value to compute the difference with. Default is 1. - Returns: + Returns ------- Index A new Index object with the computed differences. From 47e9866cad8e110141fd65c69271d5d669312a3a Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 15:56:34 +0000 Subject: [PATCH 27/35] Fix pre-commit errors --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 3c8a164c88429..32c7b9aa36d1e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6767,7 +6767,7 @@ def diff(self, periods: int = 1): Parameters ---------- - periods : int, optional + periods : int, optional The number of positions between the current and previous value to compute the difference with. Default is 1. From 69d32f7e1f64c9d6d7cb903038776c9276e7822a Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 20:49:07 +0000 Subject: [PATCH 28/35] Add example section in docstring and change return statement --- pandas/core/indexes/base.py | 39 +++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 32c7b9aa36d1e..d45cc326b4ff0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6776,8 +6776,27 @@ def diff(self, periods: int = 1): Index A new Index object with the computed differences. + Example + ------- + >>> import pandas as pd + >>> idx = pd.Index([10, 20, 30, 40, 50]) + >>> diff_idx = idx.diff() + >>> print(diff_idx) + Float64Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') + + This example creates a new `Index` object `idx` with values + `[10, 20, 30, 40, 50]`. + + The `diff` method is then used to compute the difference between + consecutive values in the `Index` object. + + The resulting `diff_idx` object has the values `[nan, 10.0, 10.0, 10.0, 10.0]`. + + Note that the first value is `nan` since there is no previous value to compute + the difference with. + """ - return Index(self.to_series().diff(periods)) + return self._constructor(self.to_series().diff(periods)) def round(self, decimals: int = 0): """ @@ -6794,8 +6813,24 @@ def round(self, decimals: int = 0): Index A new Index with the rounded values. + Example + ------- + >>> import pandas as pd + >>> idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) + >>> rounded_idx = idx.round(decimals=2) + >>> print(rounded_idx) + Float64Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64') + + This example creates a new `Index` object `idx` with values + `[10.1234, 20.5678, 30.9123, 40.4567, 50.7890]`. + + The `round` method is then used to round each value to two decimal places. + + The resulting `rounded_idx` object has the values + `[10.12, 20.57, 30.91, 40.46, 50.79]`. + """ - return Index(self.to_series().round(decimals)) + return self._constructor(self.to_series().round(decimals)) # -------------------------------------------------------------------- # Generated Arithmetic, Comparison, and Unary Methods From 5475c37fa27a088eaa3a8bc4812913aa66c77848 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 21:30:51 +0000 Subject: [PATCH 29/35] Fix docstring example section --- pandas/core/indexes/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d45cc326b4ff0..fa05bd0474a04 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6782,7 +6782,7 @@ def diff(self, periods: int = 1): >>> idx = pd.Index([10, 20, 30, 40, 50]) >>> diff_idx = idx.diff() >>> print(diff_idx) - Float64Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') + Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') This example creates a new `Index` object `idx` with values `[10, 20, 30, 40, 50]`. @@ -6819,7 +6819,7 @@ def round(self, decimals: int = 0): >>> idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) >>> rounded_idx = idx.round(decimals=2) >>> print(rounded_idx) - Float64Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64') + Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64') This example creates a new `Index` object `idx` with values `[10.1234, 20.5678, 30.9123, 40.4567, 50.7890]`. From 960540c47e20c61849951b5b647a391ea0471a2a Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Tue, 11 Apr 2023 23:57:12 +0000 Subject: [PATCH 30/35] Fix docstring --- pandas/core/indexes/base.py | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index fa05bd0474a04..60b4bfd65c39d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6776,25 +6776,13 @@ def diff(self, periods: int = 1): Index A new Index object with the computed differences. - Example - ------- + Examples + -------- >>> import pandas as pd >>> idx = pd.Index([10, 20, 30, 40, 50]) - >>> diff_idx = idx.diff() - >>> print(diff_idx) + >>> idx.diff() Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') - This example creates a new `Index` object `idx` with values - `[10, 20, 30, 40, 50]`. - - The `diff` method is then used to compute the difference between - consecutive values in the `Index` object. - - The resulting `diff_idx` object has the values `[nan, 10.0, 10.0, 10.0, 10.0]`. - - Note that the first value is `nan` since there is no previous value to compute - the difference with. - """ return self._constructor(self.to_series().diff(periods)) @@ -6813,22 +6801,13 @@ def round(self, decimals: int = 0): Index A new Index with the rounded values. - Example - ------- + Examples + -------- >>> import pandas as pd >>> idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) - >>> rounded_idx = idx.round(decimals=2) - >>> print(rounded_idx) + >>> idx.round(decimals=2) Index([10.12, 20.57, 30.91, 40.46, 50.79], dtype='float64') - This example creates a new `Index` object `idx` with values - `[10.1234, 20.5678, 30.9123, 40.4567, 50.7890]`. - - The `round` method is then used to round each value to two decimal places. - - The resulting `rounded_idx` object has the values - `[10.12, 20.57, 30.91, 40.46, 50.79]`. - """ return self._constructor(self.to_series().round(decimals)) From e79323f430702246b5116fe794bc013bf9ca23dd Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Thu, 13 Apr 2023 03:39:50 +0300 Subject: [PATCH 31/35] Add whatsnew in v2.1.0.rst --- doc/source/whatsnew/v2.1.0.rst | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 9b5cba1e1ee05..fd4d279aee76c 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -67,6 +67,40 @@ Also, note that :meth:`Categorical.map` implicitly has had its ``na_action`` set This has been deprecated and will :meth:`Categorical.map` in the future change the default to ``na_action=None``, like for all the other array types. +.. _whatsnew_210.enhancements.enhancement3: + +Index now has diff() and round() methods +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The :class:`Index` object in Pandas version 2.1.0 has two new methods: :meth:`diff()` and :meth:`round()`. +These methods are useful for performing computations on the values in an :class:`Index` object (:issue:`19708`). + +The :meth:`diff()` method computes the difference between consecutive values in the index. +If the periods parameter is greater than 1, it computes the difference between values +that are periods number of positions apart. + +For example: + +.. code-block:: python + + import pandas as pd + + idx = pd.Index([10, 20, 30, 40, 50]) + print(idx.diff()) # Output: [nan, 10.0, 10.0, 10.0, 10.0] + +The :meth:`round()` method rounds each value in the index to the given number of decimals. + +For example: + +.. code-block:: python + + import pandas as pd + + idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) + print(idx.round(decimals=2)) # Output: [10.12, 20.57, 30.91, 40.46, 50.79] + +We hope these new methods enhance your experience with Pandas! + .. _whatsnew_210.enhancements.other: Other enhancements From cea737a5125781565d885b2ee913c75f91aacbc2 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Thu, 13 Apr 2023 04:08:31 +0300 Subject: [PATCH 32/35] Fix whatsnew entry --- doc/source/whatsnew/v2.1.0.rst | 35 ---------------------------------- 1 file changed, 35 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index fd4d279aee76c..7b15aa9e0aaaf 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -67,40 +67,6 @@ Also, note that :meth:`Categorical.map` implicitly has had its ``na_action`` set This has been deprecated and will :meth:`Categorical.map` in the future change the default to ``na_action=None``, like for all the other array types. -.. _whatsnew_210.enhancements.enhancement3: - -Index now has diff() and round() methods -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -The :class:`Index` object in Pandas version 2.1.0 has two new methods: :meth:`diff()` and :meth:`round()`. -These methods are useful for performing computations on the values in an :class:`Index` object (:issue:`19708`). - -The :meth:`diff()` method computes the difference between consecutive values in the index. -If the periods parameter is greater than 1, it computes the difference between values -that are periods number of positions apart. - -For example: - -.. code-block:: python - - import pandas as pd - - idx = pd.Index([10, 20, 30, 40, 50]) - print(idx.diff()) # Output: [nan, 10.0, 10.0, 10.0, 10.0] - -The :meth:`round()` method rounds each value in the index to the given number of decimals. - -For example: - -.. code-block:: python - - import pandas as pd - - idx = pd.Index([10.1234, 20.5678, 30.9123, 40.4567, 50.7890]) - print(idx.round(decimals=2)) # Output: [10.12, 20.57, 30.91, 40.46, 50.79] - -We hope these new methods enhance your experience with Pandas! - .. _whatsnew_210.enhancements.other: Other enhancements @@ -123,7 +89,6 @@ Other enhancements - Add dtype of categories to ``repr`` information of :class:`CategoricalDtype` (:issue:`52179`) - Adding ``engine_kwargs`` parameter to :meth:`DataFrame.read_excel` (:issue:`52214`) - - .. --------------------------------------------------------------------------- .. _whatsnew_210.notable_bug_fixes: From 600d46ba1b3f7b4666052616447096a80dae5a8e Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Thu, 13 Apr 2023 13:59:05 +0300 Subject: [PATCH 33/35] Add whatsnew entry --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 7b15aa9e0aaaf..ccc728ccfc211 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -88,6 +88,7 @@ Other enhancements - :meth:`arrays.SparseArray.map` now supports ``na_action`` (:issue:`52096`). - Add dtype of categories to ``repr`` information of :class:`CategoricalDtype` (:issue:`52179`) - Adding ``engine_kwargs`` parameter to :meth:`DataFrame.read_excel` (:issue:`52214`) +- Add :meth:`diff()` and :meth:`round()` for :class:`Index` (:issue:`19708`) - .. --------------------------------------------------------------------------- .. _whatsnew_210.notable_bug_fixes: From 431c4b25fad2259e35d4932db6b5ee6171e92ba1 Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Thu, 13 Apr 2023 14:05:35 +0300 Subject: [PATCH 34/35] Fix whatsnew entry --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index ccc728ccfc211..c7c7051914cc8 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -86,9 +86,9 @@ Other enhancements - Improved error message when creating a DataFrame with empty data (0 rows), no index and an incorrect number of columns. (:issue:`52084`) - :meth:`DataFrame.applymap` now uses the :meth:`~api.extensions.ExtensionArray.map` method of underlying :class:`api.extensions.ExtensionArray` instances (:issue:`52219`) - :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`) - Adding ``engine_kwargs`` parameter to :meth:`DataFrame.read_excel` (:issue:`52214`) -- Add :meth:`diff()` and :meth:`round()` for :class:`Index` (:issue:`19708`) - .. --------------------------------------------------------------------------- .. _whatsnew_210.notable_bug_fixes: From a428bc8ae8937009356314ee8c2507eb744d713a Mon Sep 17 00:00:00 2001 From: GrammatikakisDimitris Date: Sat, 15 Apr 2023 13:22:20 +0300 Subject: [PATCH 35/35] Add blank line in v2.1.0.rst --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index f80cd7d8e31c1..f32598e72685e 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -91,6 +91,7 @@ Other enhancements - Let :meth:`DataFrame.to_feather` accept a non-default :class:`Index` and non-string column names (:issue:`51787`) - Performance improvement in :func:`read_csv` (:issue:`52632`) with ``engine="c"`` - + .. --------------------------------------------------------------------------- .. _whatsnew_210.notable_bug_fixes: