Skip to content

BUG: DataFrame.diff(np_int_obj) #44577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ Other
- Bug in :meth:`Series.to_frame` and :meth:`Index.to_frame` ignoring the ``name`` argument when ``name=None`` is explicitly passed (:issue:`44212`)
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` with ``value=None`` and ExtensionDtypes (:issue:`44270`)
- Bug in :meth:`FloatingArray.equals` failing to consider two arrays equal if they contain ``np.nan`` values (:issue:`44382`)
- Bug in :meth:`DataFrame.diff` when passing a NumPy integer object instead of an ``int`` object (:issue:`44572`)
-

.. ***DO NOT USE THIS SECTION***
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8612,8 +8612,12 @@ def melt(
),
)
def diff(self, periods: int = 1, axis: Axis = 0) -> DataFrame:
if not isinstance(periods, int):
if not (is_float(periods) and periods.is_integer()):
if not lib.is_integer(periods):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pandas/core/frame.py:8616: error: "int" has no attribute "is_integer" [attr-defined]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's pretty weird, looks like a mypy problem to me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, this actually refers to the line below if not (is_float(periods) and periods.is_integer()):.

if not (
is_float(periods)
# error: "int" has no attribute "is_integer"
and periods.is_integer() # type: ignore[attr-defined]
):
raise ValueError("periods must be an integer")
periods = int(periods)

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/methods/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def test_diff_requires_integer(self):
with pytest.raises(ValueError, match="periods must be an integer"):
df.diff(1.5)

def test_diff_allows_np_integer(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine, could just parameterize the below one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not wild about this as it is, but it looks like the next test could use a good splitting, after which combining might make more sense

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk add to list :->

# np.int64 is OK GH#44572
df = DataFrame(np.random.randn(2, 2))
res = df.diff(np.int64(1))
expected = df.diff(1)
tm.assert_frame_equal(res, expected)

def test_diff(self, datetime_frame):
the_diff = datetime_frame.diff(1)

Expand Down