Skip to content

Commit 32dd55c

Browse files
REGR: fix DataFrame reduction with EA columns and numeric_only=True (#33761)
1 parent b7f061c commit 32dd55c

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ Numeric
572572
- Bug in :meth:`DataFrame.mean` with ``numeric_only=False`` and either ``datetime64`` dtype or ``PeriodDtype`` column incorrectly raising ``TypeError`` (:issue:`32426`)
573573
- Bug in :meth:`DataFrame.count` with ``level="foo"`` and index level ``"foo"`` containing NaNs causes segmentation fault (:issue:`21824`)
574574
- Bug in :meth:`DataFrame.diff` with ``axis=1`` returning incorrect results with mixed dtypes (:issue:`32995`)
575+
- Bug in DataFrame reductions using ``numeric_only=True`` and ExtensionArrays (:issue:`33256`).
575576
- Bug in :meth:`DataFrame.corr` and :meth:`DataFrame.cov` raising when handling nullable integer columns with ``pandas.NA`` (:issue:`33803`)
576577
- Bug in :class:`DataFrame` and :class:`Series` addition and subtraction between object-dtype objects and ``datetime64`` dtype objects (:issue:`33824`)
577578

pandas/core/frame.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -8325,10 +8325,10 @@ def _get_data(axis_matters):
83258325
out_dtype = "bool" if filter_type == "bool" else None
83268326

83278327
def blk_func(values):
8328-
if values.ndim == 1 and not isinstance(values, np.ndarray):
8329-
# we can't pass axis=1
8330-
return op(values, axis=0, skipna=skipna, **kwds)
8331-
return op(values, axis=1, skipna=skipna, **kwds)
8328+
if isinstance(values, ExtensionArray):
8329+
return values._reduce(name, skipna=skipna, **kwds)
8330+
else:
8331+
return op(values, axis=1, skipna=skipna, **kwds)
83328332

83338333
# After possibly _get_data and transposing, we are now in the
83348334
# simple case where we can use BlockManager._reduce

pandas/tests/frame/test_analytics.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,17 @@ def test_mean_datetimelike_numeric_only_false(self):
896896
# mean of period is not allowed
897897
df["D"] = pd.period_range("2016", periods=3, freq="A")
898898

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

902+
def test_mean_extensionarray_numeric_only_true(self):
903+
# https://github.com/pandas-dev/pandas/issues/33256
904+
arr = np.random.randint(1000, size=(10, 5))
905+
df = pd.DataFrame(arr, dtype="Int64")
906+
result = df.mean(numeric_only=True)
907+
expected = pd.DataFrame(arr).mean()
908+
tm.assert_series_equal(result, expected)
909+
902910
def test_stats_mixed_type(self, float_string_frame):
903911
# don't blow up
904912
float_string_frame.std(1)

0 commit comments

Comments
 (0)