Skip to content

BUG: DataFrame.diff(axis=1) with mixed (or EA) dtypes #32995

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 12 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ Numeric
- Bug in :meth:`to_numeric` with string argument ``"uint64"`` and ``errors="coerce"`` silently fails (:issue:`32394`)
- Bug in :meth:`to_numeric` with ``downcast="unsigned"`` fails for empty data (:issue:`32493`)
- Bug in :meth:`DataFrame.mean` with ``numeric_only=False`` and either ``datetime64`` dtype or ``PeriodDtype`` column incorrectly raising ``TypeError`` (:issue:`32426`)
- Bug in :meth:`DataFrame.diff` with ``axis=1`` returning incorrect results with mixed dtypes; this will now raise ``NotImplementedError`` (:issue:`32995`)
-

Conversion
Expand Down
24 changes: 24 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6667,6 +6667,30 @@ def diff(self, periods: int = 1, axis: Axis = 0) -> "DataFrame":
5 NaN NaN NaN
"""
bm_axis = self._get_block_manager_axis(axis)
self._consolidate_inplace()
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not pretty. why are we not simply transposing and calling .diff()?

Copy link
Member Author

Choose a reason for hiding this comment

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

why are we not simply transposing and calling .diff()?

I'd be fine with that, but it is potentially costly

Copy link
Member

Choose a reason for hiding this comment

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

I personally think the column-wise approach could be fine

Copy link
Contributor

Choose a reason for hiding this comment

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

this should be co-located with BlockManager.diff. but to be honest I think transposing is just fine here. The block type is already inferred and handled. This is just adding a lot of complexity.

Copy link
Contributor

Choose a reason for hiding this comment

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

if you really want to do this column based approach them move this with the other .diff methods (I actually prefer just a transpose here, its totally fine for now)

if bm_axis == 0 and len(self._data.blocks) > 1 and periods != 0:
# i.e. axis == 1 and we have multiple blocks
# We need to consolidate before this check otherwise we risk
# getting unexpected dtypes in our result.

def get_na_ser(ser):
# get a Series with an appropriate diff dtype
return ser.to_frame().diff(1, axis=1).iloc[:, 0]

if periods < 0:
return self.iloc[:, ::-1].diff(-periods, axis).iloc[:, ::-1]

ncols = min(periods, len(self.columns))
na_results = [get_na_ser(self.iloc[:, n]) for n in range(ncols)]
results = [
self.iloc[:, n] - self.iloc[:, n - periods]
for n in range(ncols, len(self.columns))
]

all_results = na_results + results
dresults = {n: all_results[n] for n in range(len(all_results))}
return self._construct_result(dresults)

new_data = self._data.diff(n=periods, axis=bm_axis)
return self._constructor(new_data)

Expand Down
7 changes: 7 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,14 @@ def interpolate(
)

def diff(self, n: int, axis: int = 1) -> List["Block"]:
if axis == 0 and n != 0:
# n==0 case will be a no-op so let is fall through
# Since we only have one column, the result will be all-NA.
# Create this result by shifting along axis=0 past the length of
# our values.
return super().diff(len(self.values), axis=0)
if axis == 1:
# TODO(EA2D): unnecessary with 2D EAs
# we are by definition 1D.
axis = 0
return super().diff(n, axis)
Expand Down
39 changes: 39 additions & 0 deletions pandas/tests/frame/methods/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,42 @@ def test_diff_axis(self):
tm.assert_frame_equal(
df.diff(axis=0), DataFrame([[np.nan, np.nan], [2.0, 2.0]])
)

def test_diff_period(self):
# GH#32995 Don't pass an incorrect axis
# TODO(EA2D): this bug wouldn't have happened with 2D EA
pi = pd.date_range("2016-01-01", periods=3).to_period("D")
df = pd.DataFrame({"A": pi})

result = df.diff(1, axis=1)

# TODO: should we make Block.diff do type inference? or maybe algos.diff?
expected = (df - pd.NaT).astype(object)
tm.assert_frame_equal(result, expected)

def test_diff_axis1_mixed_dtypes(self):
# GH#32995 operate column-wise when we have mixed dtypes and axis=1
df = pd.DataFrame({"A": range(3), "B": 2 * np.arange(3, dtype=np.float64)})

expected = pd.DataFrame({"A": [np.nan, np.nan, np.nan], "B": df["B"] / 2})

result = df.diff(axis=1)
tm.assert_frame_equal(result, expected)

def test_diff_axis1_mixed_dtypes_large_periods(self):
# GH#32995 operate column-wise when we have mixed dtypes and axis=1
df = pd.DataFrame({"A": range(3), "B": 2 * np.arange(3, dtype=np.float64)})

expected = df * np.nan

result = df.diff(axis=1, periods=3)
tm.assert_frame_equal(result, expected)

def test_diff_axis1_mixed_dtypes_negative_periods(self):
# GH#32995 operate column-wise when we have mixed dtypes and axis=1
df = pd.DataFrame({"A": range(3), "B": 2 * np.arange(3, dtype=np.float64)})

expected = pd.DataFrame({"A": -1.0 * df["A"], "B": df["B"] * np.nan})

result = df.diff(axis=1, periods=-1)
tm.assert_frame_equal(result, expected)