Skip to content

Commit 13d651d

Browse files
authored
Agg functions for df not respecting numeric only with level (#40683)
1 parent 1f7dd7f commit 13d651d

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ Groupby/resample/rolling
754754
- Bug in :class:`core.window.ewm.ExponentialMovingWindow` when calling ``__getitem__`` would not retain ``com``, ``span``, ``alpha`` or ``halflife`` attributes (:issue:`40164`)
755755
- :class:`core.window.ewm.ExponentialMovingWindow` now raises a ``NotImplementedError`` when specifying ``times`` with ``adjust=False`` due to an incorrect calculation (:issue:`40098`)
756756
- Bug in :meth:`Series.asfreq` and :meth:`DataFrame.asfreq` dropping rows when the index is not sorted (:issue:`39805`)
757+
- Bug in aggregation functions for :class:`DataFrame` not respecting ``numeric_only`` argument when ``level`` keyword was given (:issue:`40660`)
757758

758759
Reshaping
759760
^^^^^^^^^
@@ -809,7 +810,6 @@ Other
809810
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
810811
- Bug in :meth:`DataFrame.convert_dtypes` incorrectly raised ValueError when called on an empty DataFrame (:issue:`40393`)
811812

812-
813813
.. ---------------------------------------------------------------------------
814814
815815
.. _whatsnew_130.contributors:

pandas/core/generic.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -10414,7 +10414,9 @@ def _stat_function(
1041410414
if axis is None:
1041510415
axis = self._stat_axis_number
1041610416
if level is not None:
10417-
return self._agg_by_level(name, axis=axis, level=level, skipna=skipna)
10417+
return self._agg_by_level(
10418+
name, axis=axis, level=level, skipna=skipna, numeric_only=numeric_only
10419+
)
1041810420
return self._reduce(
1041910421
func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
1042010422
)
@@ -10475,7 +10477,12 @@ def _min_count_stat_function(
1047510477
axis = self._stat_axis_number
1047610478
if level is not None:
1047710479
return self._agg_by_level(
10478-
name, axis=axis, level=level, skipna=skipna, min_count=min_count
10480+
name,
10481+
axis=axis,
10482+
level=level,
10483+
skipna=skipna,
10484+
min_count=min_count,
10485+
numeric_only=numeric_only,
1047910486
)
1048010487
return self._reduce(
1048110488
func,

pandas/tests/frame/test_reductions.py

+15
Original file line numberDiff line numberDiff line change
@@ -1546,3 +1546,18 @@ def test_minmax_extensionarray(method, numeric_only):
15461546
[getattr(int64_info, method)], index=Index(["Int64"], dtype="object")
15471547
)
15481548
tm.assert_series_equal(result, expected)
1549+
1550+
1551+
@pytest.mark.parametrize("meth", ["max", "min", "sum", "mean", "median"])
1552+
def test_groupy_regular_arithmetic_equivalent(meth):
1553+
# GH#40660
1554+
df = DataFrame(
1555+
{"a": [pd.Timedelta(hours=6), pd.Timedelta(hours=7)], "b": [12.1, 13.3]}
1556+
)
1557+
expected = df.copy()
1558+
1559+
result = getattr(df, meth)(level=0)
1560+
tm.assert_frame_equal(result, expected)
1561+
1562+
result = getattr(df.groupby(level=0), meth)(numeric_only=False)
1563+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)