Skip to content

Commit 8c17c1b

Browse files
authored
BUG: DataFrame.diff(np_int_obj) (#44577)
1 parent d5f7723 commit 8c17c1b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ Other
741741
- Bug in :meth:`Series.to_frame` and :meth:`Index.to_frame` ignoring the ``name`` argument when ``name=None`` is explicitly passed (:issue:`44212`)
742742
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` with ``value=None`` and ExtensionDtypes (:issue:`44270`)
743743
- Bug in :meth:`FloatingArray.equals` failing to consider two arrays equal if they contain ``np.nan`` values (:issue:`44382`)
744+
- Bug in :meth:`DataFrame.diff` when passing a NumPy integer object instead of an ``int`` object (:issue:`44572`)
744745
-
745746

746747
.. ***DO NOT USE THIS SECTION***

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -8612,8 +8612,12 @@ def melt(
86128612
),
86138613
)
86148614
def diff(self, periods: int = 1, axis: Axis = 0) -> DataFrame:
8615-
if not isinstance(periods, int):
8616-
if not (is_float(periods) and periods.is_integer()):
8615+
if not lib.is_integer(periods):
8616+
if not (
8617+
is_float(periods)
8618+
# error: "int" has no attribute "is_integer"
8619+
and periods.is_integer() # type: ignore[attr-defined]
8620+
):
86178621
raise ValueError("periods must be an integer")
86188622
periods = int(periods)
86198623

pandas/tests/frame/methods/test_diff.py

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ def test_diff_requires_integer(self):
1717
with pytest.raises(ValueError, match="periods must be an integer"):
1818
df.diff(1.5)
1919

20+
def test_diff_allows_np_integer(self):
21+
# np.int64 is OK GH#44572
22+
df = DataFrame(np.random.randn(2, 2))
23+
res = df.diff(np.int64(1))
24+
expected = df.diff(1)
25+
tm.assert_frame_equal(res, expected)
26+
2027
def test_diff(self, datetime_frame):
2128
the_diff = datetime_frame.diff(1)
2229

0 commit comments

Comments
 (0)