Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[x] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
pd.Series([1, 2, 3]).diff("hello")
Raises:
AttributeError: 'str' object has no attribute 'is_integer'
Issue Description
When passing non-numeric types (like strings, None, or other objects) to the diff
function in pandas/core/algorithms.py
, it raises an AttributeError
instead of the expected ValueError
. This affects any code that uses Series.diff()
, DataFrame.diff()
, or calls the diff
function directly.
The issue occurs because the validation logic tries to call n.is_integer()
on non-float objects that don't have this method, resulting in an AttributeError
.
Expected Behavior
import pandas as pd
pd.Series([1, 2, 3]).diff("hello")
Should raise:
ValueError: periods must be an integer
Installed Versions
python : 3.12.10
OS : Linux
OS-release : 4.18.0-553.56.1.el8_10.x86_64
Comment From: mroeschke
Thanks I don't see this error on main
In [1]: import pandas as pd
...: pd.Series([1, 2, 3]).diff("hello")
File ~/pandas/core/series.py:2837, in Series.diff(self, periods)
2835 if not lib.is_integer(periods):
2836 if not (is_float(periods) and periods.is_integer()):
-> 2837 raise ValueError("periods must be an integer")
2838 result = algorithms.diff(self._values, periods)
2839 return self._constructor(result, index=self.index, copy=False).__finalize__(
2840 self, method="diff"
2841 )
ValueError: periods must be an integer
so closing this PR