Skip to content

REGR: fix DataFrame reduction with EA columns and numeric_only=True #33761

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
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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ Numeric
- 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.count` with ``level="foo"`` and index level ``"foo"`` containing NaNs causes segmentation fault (:issue:`21824`)
- Bug in :meth:`DataFrame.diff` with ``axis=1`` returning incorrect results with mixed dtypes (:issue:`32995`)
- Bug in DataFrame reductions using ``numeric_only=True`` and ExtensionArrays (:issue:`33256`).
- Bug in :meth:`DataFrame.corr` and :meth:`DataFrame.cov` raising when handling nullable integer columns with ``pandas.NA`` (:issue:`33803`)
- Bug in :class:`DataFrame` and :class:`Series` addition and subtraction between object-dtype objects and ``datetime64`` dtype objects (:issue:`33824`)

Expand Down
8 changes: 4 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8325,10 +8325,10 @@ def _get_data(axis_matters):
out_dtype = "bool" if filter_type == "bool" else None

def blk_func(values):
if values.ndim == 1 and not isinstance(values, np.ndarray):
# we can't pass axis=1
return op(values, axis=0, skipna=skipna, **kwds)
return op(values, axis=1, skipna=skipna, **kwds)
if isinstance(values, ExtensionArray):
return values._reduce(name, skipna=skipna, **kwds)
Copy link
Member

Choose a reason for hiding this comment

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

is _reduce part of the interface?

if we're avoiding nanops, can/should we make those functions either a) not handle EA cases or b) dispatch to _reduce?

else:
return op(values, axis=1, skipna=skipna, **kwds)

# After possibly _get_data and transposing, we are now in the
# simple case where we can use BlockManager._reduce
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,17 @@ def test_mean_datetimelike_numeric_only_false(self):
# mean of period is not allowed
df["D"] = pd.period_range("2016", periods=3, freq="A")

with pytest.raises(TypeError, match="reduction operation 'mean' not allowed"):
with pytest.raises(TypeError, match="mean is not implemented for Period"):
df.mean(numeric_only=False)

def test_mean_extensionarray_numeric_only_true(self):
# https://github.com/pandas-dev/pandas/issues/33256
arr = np.random.randint(1000, size=(10, 5))
df = pd.DataFrame(arr, dtype="Int64")
result = df.mean(numeric_only=True)
expected = pd.DataFrame(arr).mean()
tm.assert_series_equal(result, expected)

def test_stats_mixed_type(self, float_string_frame):
# don't blow up
float_string_frame.std(1)
Expand Down