Skip to content

Commit

Permalink
Agg functions for df not respecting numeric only with level (pandas-d…
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Apr 8, 2021
1 parent 1f7dd7f commit 13d651d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ Groupby/resample/rolling
- Bug in :class:`core.window.ewm.ExponentialMovingWindow` when calling ``__getitem__`` would not retain ``com``, ``span``, ``alpha`` or ``halflife`` attributes (:issue:`40164`)
- :class:`core.window.ewm.ExponentialMovingWindow` now raises a ``NotImplementedError`` when specifying ``times`` with ``adjust=False`` due to an incorrect calculation (:issue:`40098`)
- Bug in :meth:`Series.asfreq` and :meth:`DataFrame.asfreq` dropping rows when the index is not sorted (:issue:`39805`)
- Bug in aggregation functions for :class:`DataFrame` not respecting ``numeric_only`` argument when ``level`` keyword was given (:issue:`40660`)

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


.. ---------------------------------------------------------------------------
.. _whatsnew_130.contributors:
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10414,7 +10414,9 @@ def _stat_function(
if axis is None:
axis = self._stat_axis_number
if level is not None:
return self._agg_by_level(name, axis=axis, level=level, skipna=skipna)
return self._agg_by_level(
name, axis=axis, level=level, skipna=skipna, numeric_only=numeric_only
)
return self._reduce(
func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
)
Expand Down Expand Up @@ -10475,7 +10477,12 @@ def _min_count_stat_function(
axis = self._stat_axis_number
if level is not None:
return self._agg_by_level(
name, axis=axis, level=level, skipna=skipna, min_count=min_count
name,
axis=axis,
level=level,
skipna=skipna,
min_count=min_count,
numeric_only=numeric_only,
)
return self._reduce(
func,
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,3 +1546,18 @@ def test_minmax_extensionarray(method, numeric_only):
[getattr(int64_info, method)], index=Index(["Int64"], dtype="object")
)
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("meth", ["max", "min", "sum", "mean", "median"])
def test_groupy_regular_arithmetic_equivalent(meth):
# GH#40660
df = DataFrame(
{"a": [pd.Timedelta(hours=6), pd.Timedelta(hours=7)], "b": [12.1, 13.3]}
)
expected = df.copy()

result = getattr(df, meth)(level=0)
tm.assert_frame_equal(result, expected)

result = getattr(df.groupby(level=0), meth)(numeric_only=False)
tm.assert_frame_equal(result, expected)

0 comments on commit 13d651d

Please sign in to comment.