From a083532f3e82376dbf15b8813c623b0533fd86b1 Mon Sep 17 00:00:00 2001 From: phofl Date: Mon, 29 Mar 2021 23:54:00 +0200 Subject: [PATCH 1/2] Agg functions for df not respecting numeric only with level --- doc/source/whatsnew/v1.3.0.rst | 2 +- pandas/core/generic.py | 11 +++++++++-- pandas/tests/groupby/test_groupby.py | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 1e723493a4cc8..9338e672a06c6 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -720,7 +720,7 @@ Other - Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`) - 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`) - +- Bug in aggregation functions for :class:`DataFrame` not respecting ``numeric_only`` argument when ``level`` keyword was given (:issue:`40660`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a9afd6a5b7550..a71e31fb3c66c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -10500,7 +10500,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 ) @@ -10561,7 +10563,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, diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 12247e2445295..22fe392fe98f5 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2190,3 +2190,18 @@ def test_groupby_mean_duplicate_index(rand_series_with_duplicate_datetimeindex): result = dups.groupby(level=0).mean() expected = dups.groupby(dups.index).mean() 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) From 92c1a52c2f29b9ac7af01ec53ecfc68b43d78011 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 2 Apr 2021 22:35:27 +0200 Subject: [PATCH 2/2] Move whatsnew and test --- doc/source/whatsnew/v1.3.0.rst | 2 +- pandas/tests/frame/test_reductions.py | 15 +++++++++++++++ pandas/tests/groupby/test_groupby.py | 15 --------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 9338e672a06c6..7754cc7b8cc08 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -666,6 +666,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 ^^^^^^^^^ @@ -720,7 +721,6 @@ Other - Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`) - 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`) -- Bug in aggregation functions for :class:`DataFrame` not respecting ``numeric_only`` argument when ``level`` keyword was given (:issue:`40660`) .. --------------------------------------------------------------------------- diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index b294c97409951..e46804bbf45ae 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -1509,3 +1509,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) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 22fe392fe98f5..12247e2445295 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2190,18 +2190,3 @@ def test_groupby_mean_duplicate_index(rand_series_with_duplicate_datetimeindex): result = dups.groupby(level=0).mean() expected = dups.groupby(dups.index).mean() 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)