Skip to content

Commit 11b25ab

Browse files
committed
BUG: 61728: AttributeError in pandas.core.algorithms.diff when passing non-numeric types
1 parent 35b0d1d commit 11b25ab

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pandas/core/algorithms.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,9 +1334,15 @@ def diff(arr, n: int, axis: AxisInt = 0):
13341334
# added a check on the integer value of period
13351335
# see https://github.com/pandas-dev/pandas/issues/56607
13361336
if not lib.is_integer(n):
1337-
if not (is_float(n) and n.is_integer()):
1337+
try:
1338+
if is_float(n) and n.is_integer():
1339+
n = int(n)
1340+
else:
1341+
raise ValueError("periods must be an integer")
1342+
except (AttributeError, TypeError):
1343+
# Handle cases where n doesn't have is_integer method
1344+
# or other type-related errors
13381345
raise ValueError("periods must be an integer")
1339-
n = int(n)
13401346
na = np.nan
13411347
dtype = arr.dtype
13421348

0 commit comments

Comments
 (0)