Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added validation check to Series.diff,updated testcase and whatsnew
  • Loading branch information
pmhatre1 committed Jan 1, 2024
commit aa6de47cdd2d4ca9e6990fa86380b1b60d97782c
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
)
from pandas.core.dtypes.common import (
is_dict_like,
is_float,
is_integer,
is_iterator,
is_list_like,
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/series/methods/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down