From a992bd5065fa827d1af674f797d4215cb968711f Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Sat, 30 Dec 2023 12:57:18 -0700 Subject: [PATCH 01/10] Adding check on integer value of periods issue#56607 --- pandas/core/algorithms.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 15a07da76d2f7..11d850a758de5 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -46,6 +46,7 @@ is_complex_dtype, is_dict_like, is_extension_array_dtype, + is_float, is_float_dtype, is_integer, is_integer_dtype, @@ -1356,7 +1357,12 @@ def diff(arr, n: int, axis: AxisInt = 0): shifted """ - n = int(n) + # added a check on the integer value of periods + # see https://github.com/pandas-dev/pandas/issues/56607 + if not lib.is_integer(n): + if not (is_float(n) and n.is_integer()): + raise ValueError("periods must be an integer") + n = int(n) na = np.nan dtype = arr.dtype From 62c52477b8f95967acf139abcce823b3a2a2686d Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Sat, 30 Dec 2023 13:04:14 -0700 Subject: [PATCH 02/10] Adding check on integer value of the periods issue#56607 --- pandas/core/algorithms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 11d850a758de5..353f30481f973 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1357,7 +1357,7 @@ def diff(arr, n: int, axis: AxisInt = 0): shifted """ - # added a check on the integer value of periods + # added a check on the integer value of period. # see https://github.com/pandas-dev/pandas/issues/56607 if not lib.is_integer(n): if not (is_float(n) and n.is_integer()): From 99235e5d167b0469151e132f216f2c214a00af10 Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Sat, 30 Dec 2023 13:08:33 -0700 Subject: [PATCH 03/10] Adding check on integer value of the periods issue#56607 --- pandas/core/algorithms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 353f30481f973..e6fd15ecf1350 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1357,7 +1357,7 @@ def diff(arr, n: int, axis: AxisInt = 0): shifted """ - # added a check on the integer value of period. + # added a check on the integer value of period # see https://github.com/pandas-dev/pandas/issues/56607 if not lib.is_integer(n): if not (is_float(n) and n.is_integer()): From aa6de47cdd2d4ca9e6990fa86380b1b60d97782c Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Sun, 31 Dec 2023 19:11:21 -0700 Subject: [PATCH 04/10] Added validation check to Series.diff,updated testcase and whatsnew --- doc/source/whatsnew/v2.3.0.rst | 1 + pandas/core/series.py | 4 ++++ pandas/tests/series/methods/test_diff.py | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index 1f1b0c7d7195a..f1a8313a02874 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -108,6 +108,7 @@ Performance improvements Bug fixes ~~~~~~~~~ +Fixed bug in :core:`Series.diff` and :core:`algorithms.diff` for no arg validation unlike df.diff for periods. (:issue:`56607`) Categorical ^^^^^^^^^^^ diff --git a/pandas/core/series.py b/pandas/core/series.py index 487f57b7390a8..578fcdfd9f0a1 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -72,6 +72,7 @@ ) from pandas.core.dtypes.common import ( is_dict_like, + is_float, is_integer, is_iterator, is_list_like, @@ -3103,6 +3104,9 @@ def diff(self, periods: int = 1) -> Series: -------- {examples} """ + if not lib.is_integer(periods): + if not (is_float(periods) and periods.is_integer()): + raise ValueError("periods must be an integer") result = algorithms.diff(self._values, periods) return self._constructor(result, index=self.index, copy=False).__finalize__( self, method="diff" diff --git a/pandas/tests/series/methods/test_diff.py b/pandas/tests/series/methods/test_diff.py index 18de81a927c3a..a46389087f87b 100644 --- a/pandas/tests/series/methods/test_diff.py +++ b/pandas/tests/series/methods/test_diff.py @@ -10,6 +10,11 @@ class TestSeriesDiff: + def test_diff_series_requires_integer(self): + series = Series(np.random.default_rng(2).standard_normal(2)) + with pytest.raises(ValueError, match="periods must be an integer"): + series.diff(1.5) + def test_diff_np(self): # TODO(__array_function__): could make np.diff return a Series # matching ser.diff() From 5063d15e76e6c3f3c8c15563d7082bbc4ea22997 Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Mon, 1 Jan 2024 13:18:14 -0700 Subject: [PATCH 05/10] Updated whatsnew --- doc/source/whatsnew/v2.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index f1a8313a02874..d638797cfff1d 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -108,7 +108,7 @@ Performance improvements Bug fixes ~~~~~~~~~ -Fixed bug in :core:`Series.diff` and :core:`algorithms.diff` for no arg validation unlike df.diff for periods. (:issue:`56607`) +Fixed bug in :core:`Series.diff` and :core:`algorithms.diff` for no arg validation unlike :core:`df.diff` for periods. (:issue:`56607`) Categorical ^^^^^^^^^^^ From f6f6befd60c3e0efb240785c6da0d8313a7725a2 Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Mon, 1 Jan 2024 14:03:40 -0700 Subject: [PATCH 06/10] Updated whatsnew --- doc/source/whatsnew/v2.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index d638797cfff1d..b6de33002251b 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -108,7 +108,7 @@ Performance improvements Bug fixes ~~~~~~~~~ -Fixed bug in :core:`Series.diff` and :core:`algorithms.diff` for no arg validation unlike :core:`df.diff` for periods. (:issue:`56607`) +Fixed bug in :meth:`Series.diff` and :meth:`algorithms.diff` for no arg validation unlike :meth:`Dataframe.diff` for periods. (:issue:`56607`) Categorical ^^^^^^^^^^^ From 17ef92adbb35f73fdd386ccc8e20098b4d759b9a Mon Sep 17 00:00:00 2001 From: Prathamesh Date: Fri, 5 Jan 2024 17:01:56 -0700 Subject: [PATCH 07/10] Update doc/source/whatsnew/v2.3.0.rst Co-authored-by: Thomas Li <47963215+lithomas1@users.noreply.github.com> --- doc/source/whatsnew/v2.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index b6de33002251b..d70458fd94940 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -108,7 +108,7 @@ Performance improvements Bug fixes ~~~~~~~~~ -Fixed bug in :meth:`Series.diff` and :meth:`algorithms.diff` for no arg validation unlike :meth:`Dataframe.diff` for periods. (:issue:`56607`) +Fixed bug in :meth:`Series.diff` and :meth:`algorithms.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`) Categorical ^^^^^^^^^^^ From c03f3f6384fe3f6f942e53af1433095a9dcd72dc Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Fri, 5 Jan 2024 17:18:37 -0700 Subject: [PATCH 08/10] Updated whatsnew with new comment --- doc/source/whatsnew/v2.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index b6de33002251b..002054d2624f2 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -108,7 +108,7 @@ Performance improvements Bug fixes ~~~~~~~~~ -Fixed bug in :meth:`Series.diff` and :meth:`algorithms.diff` for no arg validation unlike :meth:`Dataframe.diff` for periods. (:issue:`56607`) +Fixed bug in :meth:`Series.diff` and :meth:`algorithms.diff` allowing non-integer values for the ``periods`` argument (:issue:`56607`) Categorical ^^^^^^^^^^^ From 46ca82d189d8d40306f80744eb8b29327d15296b Mon Sep 17 00:00:00 2001 From: Prathamesh Date: Fri, 5 Jan 2024 18:25:48 -0700 Subject: [PATCH 09/10] Update doc/source/whatsnew/v2.3.0.rst Co-authored-by: Thomas Li <47963215+lithomas1@users.noreply.github.com> --- doc/source/whatsnew/v2.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index fdaa6f89c30a9..d82f8c9f59fcf 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -108,7 +108,7 @@ Performance improvements Bug fixes ~~~~~~~~~ -Fixed bug in :meth:`Series.diff` and :meth:`algorithms.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`) +- Fixed bug in :meth:`Series.diff` and :meth:`algorithms.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`) Categorical From 82835510108d93e2351e7485b1e511c457b1ac7c Mon Sep 17 00:00:00 2001 From: Prathamesh Date: Sat, 6 Jan 2024 14:51:48 -0700 Subject: [PATCH 10/10] Update doc/source/whatsnew/v2.3.0.rst Co-authored-by: Thomas Li <47963215+lithomas1@users.noreply.github.com> --- doc/source/whatsnew/v2.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index d82f8c9f59fcf..c0692dba32a72 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -108,7 +108,7 @@ Performance improvements Bug fixes ~~~~~~~~~ -- Fixed bug in :meth:`Series.diff` and :meth:`algorithms.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`) +- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`) Categorical