From 85737a73ead1f6af81bbf87e4c21cd040399411b Mon Sep 17 00:00:00 2001 From: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com> Date: Mon, 6 Sep 2021 16:37:19 -0400 Subject: [PATCH] Backport PR #43410: REGR: Resampler.aggregate fails when used with column selection --- doc/source/whatsnew/v1.3.3.rst | 1 + pandas/core/resample.py | 3 +++ pandas/tests/resample/test_resampler_grouper.py | 15 +++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/doc/source/whatsnew/v1.3.3.rst b/doc/source/whatsnew/v1.3.3.rst index 70d8728a67695..1fbe91727c847 100644 --- a/doc/source/whatsnew/v1.3.3.rst +++ b/doc/source/whatsnew/v1.3.3.rst @@ -22,6 +22,7 @@ Fixed regressions - Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`) - Fixed regression in :meth:`read_parquet` where the ``fastparquet`` engine would not work properly with fastparquet 0.7.0 (:issue:`43075`) - Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`) +- Fixed regression in :meth:`.Resampler.aggregate` when used after column selection would raise if ``func`` is a list of aggregation functions (:issue:`42905`) - Fixed regression in :meth:`DataFrame.corr` where Kendall correlation would produce incorrect results for columns with repeated values (:issue:`43401`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 76e23f1bf77e0..4aa4072e9de00 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -24,6 +24,7 @@ ) from pandas._typing import ( FrameOrSeries, + IndexLabel, T, TimedeltaConvertibleTypes, TimestampConvertibleTypes, @@ -1016,6 +1017,7 @@ class _GroupByMixin(PandasObject): """ _attributes: list[str] # in practice the same as Resampler._attributes + _selection: IndexLabel | None = None def __init__(self, obj, parent=None, groupby=None, **kwargs): # reached via ._gotitem and _get_resampler_for_grouping @@ -1027,6 +1029,7 @@ def __init__(self, obj, parent=None, groupby=None, **kwargs): # the resampler attributes for attr in self._attributes: setattr(self, attr, kwargs.get(attr, getattr(parent, attr))) + self._selection = kwargs.get("selection") self.binner = parent.binner diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 3e78d6ebf4c0c..204efa0e5670f 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -9,6 +9,7 @@ import pandas as pd from pandas import ( DataFrame, + Index, Series, TimedeltaIndex, Timestamp, @@ -405,6 +406,20 @@ def test_resample_groupby_agg(): tm.assert_frame_equal(result, expected) +def test_resample_groupby_agg_listlike(): + # GH 42905 + ts = Timestamp("2021-02-28 00:00:00") + df = DataFrame({"class": ["beta"], "value": [69]}, index=Index([ts], name="date")) + resampled = df.groupby("class").resample("M")["value"] + result = resampled.agg(["sum", "size"]) + expected = DataFrame( + [[69, 1]], + index=pd.MultiIndex.from_tuples([("beta", ts)], names=["class", "date"]), + columns=["sum", "size"], + ) + tm.assert_frame_equal(result, expected) + + @pytest.mark.parametrize("keys", [["a"], ["a", "b"]]) def test_empty(keys): # GH 26411