Skip to content

Commit 7d61e32

Browse files
pmhatre1lithomas1
andauthored
Added validation check for integer value for series.df (#56688)
* Adding check on integer value of periods issue#56607 * Adding check on integer value of the periods issue#56607 * Adding check on integer value of the periods issue#56607 * Added validation check to Series.diff,updated testcase and whatsnew * Updated whatsnew * Updated whatsnew * Update doc/source/whatsnew/v2.3.0.rst Co-authored-by: Thomas Li <[email protected]> * Updated whatsnew with new comment * Update doc/source/whatsnew/v2.3.0.rst Co-authored-by: Thomas Li <[email protected]> * Update doc/source/whatsnew/v2.3.0.rst Co-authored-by: Thomas Li <[email protected]> --------- Co-authored-by: Thomas Li <[email protected]>
1 parent d711b1d commit 7d61e32

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

doc/source/whatsnew/v2.3.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ Performance improvements
108108

109109
Bug fixes
110110
~~~~~~~~~
111+
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
112+
111113

112114
Categorical
113115
^^^^^^^^^^^

pandas/core/algorithms.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
is_complex_dtype,
4848
is_dict_like,
4949
is_extension_array_dtype,
50+
is_float,
5051
is_float_dtype,
5152
is_integer,
5253
is_integer_dtype,
@@ -1361,7 +1362,12 @@ def diff(arr, n: int, axis: AxisInt = 0):
13611362
shifted
13621363
"""
13631364

1364-
n = int(n)
1365+
# added a check on the integer value of period
1366+
# see https://github.com/pandas-dev/pandas/issues/56607
1367+
if not lib.is_integer(n):
1368+
if not (is_float(n) and n.is_integer()):
1369+
raise ValueError("periods must be an integer")
1370+
n = int(n)
13651371
na = np.nan
13661372
dtype = arr.dtype
13671373

pandas/core/series.py

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
)
7373
from pandas.core.dtypes.common import (
7474
is_dict_like,
75+
is_float,
7576
is_integer,
7677
is_iterator,
7778
is_list_like,
@@ -3102,6 +3103,9 @@ def diff(self, periods: int = 1) -> Series:
31023103
--------
31033104
{examples}
31043105
"""
3106+
if not lib.is_integer(periods):
3107+
if not (is_float(periods) and periods.is_integer()):
3108+
raise ValueError("periods must be an integer")
31053109
result = algorithms.diff(self._values, periods)
31063110
return self._constructor(result, index=self.index, copy=False).__finalize__(
31073111
self, method="diff"

pandas/tests/series/methods/test_diff.py

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111

1212
class TestSeriesDiff:
13+
def test_diff_series_requires_integer(self):
14+
series = Series(np.random.default_rng(2).standard_normal(2))
15+
with pytest.raises(ValueError, match="periods must be an integer"):
16+
series.diff(1.5)
17+
1318
def test_diff_np(self):
1419
# TODO(__array_function__): could make np.diff return a Series
1520
# matching ser.diff()

0 commit comments

Comments
 (0)