Skip to content

Commit 93f2de9

Browse files
Backport PR pandas-dev#43410: REGR: Resampler.aggregate fails when used with column selection (pandas-dev#43444)
Co-authored-by: Richard Shadrach <[email protected]>
1 parent b7082dd commit 93f2de9

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v1.3.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`)
2323
- Fixed regression in :meth:`read_parquet` where the ``fastparquet`` engine would not work properly with fastparquet 0.7.0 (:issue:`43075`)
2424
- Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`)
25+
- Fixed regression in :meth:`.Resampler.aggregate` when used after column selection would raise if ``func`` is a list of aggregation functions (:issue:`42905`)
2526
- Fixed regression in :meth:`DataFrame.corr` where Kendall correlation would produce incorrect results for columns with repeated values (:issue:`43401`)
2627

2728
.. ---------------------------------------------------------------------------

pandas/core/resample.py

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
)
2525
from pandas._typing import (
2626
FrameOrSeries,
27+
IndexLabel,
2728
T,
2829
TimedeltaConvertibleTypes,
2930
TimestampConvertibleTypes,
@@ -1016,6 +1017,7 @@ class _GroupByMixin(PandasObject):
10161017
"""
10171018

10181019
_attributes: list[str] # in practice the same as Resampler._attributes
1020+
_selection: IndexLabel | None = None
10191021

10201022
def __init__(self, obj, parent=None, groupby=None, **kwargs):
10211023
# reached via ._gotitem and _get_resampler_for_grouping
@@ -1027,6 +1029,7 @@ def __init__(self, obj, parent=None, groupby=None, **kwargs):
10271029
# the resampler attributes
10281030
for attr in self._attributes:
10291031
setattr(self, attr, kwargs.get(attr, getattr(parent, attr)))
1032+
self._selection = kwargs.get("selection")
10301033

10311034
self.binner = parent.binner
10321035

pandas/tests/resample/test_resampler_grouper.py

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pandas as pd
1010
from pandas import (
1111
DataFrame,
12+
Index,
1213
Series,
1314
TimedeltaIndex,
1415
Timestamp,
@@ -405,6 +406,20 @@ def test_resample_groupby_agg():
405406
tm.assert_frame_equal(result, expected)
406407

407408

409+
def test_resample_groupby_agg_listlike():
410+
# GH 42905
411+
ts = Timestamp("2021-02-28 00:00:00")
412+
df = DataFrame({"class": ["beta"], "value": [69]}, index=Index([ts], name="date"))
413+
resampled = df.groupby("class").resample("M")["value"]
414+
result = resampled.agg(["sum", "size"])
415+
expected = DataFrame(
416+
[[69, 1]],
417+
index=pd.MultiIndex.from_tuples([("beta", ts)], names=["class", "date"]),
418+
columns=["sum", "size"],
419+
)
420+
tm.assert_frame_equal(result, expected)
421+
422+
408423
@pytest.mark.parametrize("keys", [["a"], ["a", "b"]])
409424
def test_empty(keys):
410425
# GH 26411

0 commit comments

Comments
 (0)