From a992bd5065fa827d1af674f797d4215cb968711f Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Sat, 30 Dec 2023 12:57:18 -0700 Subject: [PATCH 1/9] 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 2/9] 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 3/9] 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 4/9] 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 5/9] 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 6/9] 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 71263efc88ed148be78c194f9aa53b6db1097f87 Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Wed, 3 Jan 2024 15:33:10 -0700 Subject: [PATCH 7/9] Replaced pytest.raises with tm.assert_produces_warning --- pandas/tests/io/xml/test_xml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/xml/test_xml.py b/pandas/tests/io/xml/test_xml.py index 6f429c1ecbf8a..f005c56e4f766 100644 --- a/pandas/tests/io/xml/test_xml.py +++ b/pandas/tests/io/xml/test_xml.py @@ -1367,7 +1367,7 @@ def test_empty_stylesheet(val): ) kml = os.path.join("data", "xml", "cta_rail_lines.kml") - with pytest.raises(FutureWarning, match=msg): + with tm.assert_produces_warning(FutureWarning, match=msg): read_xml(kml, stylesheet=val) From 22d1711b5d45b94f0520cce5416ebab3d7e7eaa1 Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Wed, 3 Jan 2024 17:26:55 -0700 Subject: [PATCH 8/9] Fix for bug56716 --- pandas/tests/io/xml/test_xml.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/io/xml/test_xml.py b/pandas/tests/io/xml/test_xml.py index f005c56e4f766..64da2907dc468 100644 --- a/pandas/tests/io/xml/test_xml.py +++ b/pandas/tests/io/xml/test_xml.py @@ -1367,6 +1367,7 @@ def test_empty_stylesheet(val): ) kml = os.path.join("data", "xml", "cta_rail_lines.kml") + # 56716: Use assert_produces_warning instead of pytest.raises to show FutureWarning with tm.assert_produces_warning(FutureWarning, match=msg): read_xml(kml, stylesheet=val) From 30c6d99cdd1cf7cc3714c49563b1e7e4fe83246f Mon Sep 17 00:00:00 2001 From: pmhatre1 Date: Sat, 6 Jan 2024 15:07:26 -0700 Subject: [PATCH 9/9] Updated the pytest error code --- pandas/tests/io/xml/test_xml.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/xml/test_xml.py b/pandas/tests/io/xml/test_xml.py index f38cee14393d5..2e1197cff8875 100644 --- a/pandas/tests/io/xml/test_xml.py +++ b/pandas/tests/io/xml/test_xml.py @@ -497,10 +497,9 @@ def test_wrong_file_path(parser): ) filename = os.path.join("data", "html", "books.xml") - with pytest.raises( - FutureWarning, - match=msg, - ): + with tm.assert_produces_warning( + FutureWarning, + match=msg): read_xml(filename, parser=parser)