Skip to content

Commit c7d98e1

Browse files
authored
REGR: Resampler.aggregate fails when used with column selection (#43410)
1 parent 40274ac commit c7d98e1

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

2829
.. ---------------------------------------------------------------------------

pandas/core/resample.py

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
from pandas._typing import (
2727
FrameOrSeries,
28+
IndexLabel,
2829
T,
2930
TimedeltaConvertibleTypes,
3031
TimestampConvertibleTypes,
@@ -1032,6 +1033,7 @@ class _GroupByMixin(PandasObject):
10321033
"""
10331034

10341035
_attributes: list[str] # in practice the same as Resampler._attributes
1036+
_selection: IndexLabel | None = None
10351037

10361038
def __init__(self, obj, parent=None, groupby=None, **kwargs):
10371039
# reached via ._gotitem and _get_resampler_for_grouping
@@ -1043,6 +1045,7 @@ def __init__(self, obj, parent=None, groupby=None, **kwargs):
10431045
# the resampler attributes
10441046
for attr in self._attributes:
10451047
setattr(self, attr, kwargs.get(attr, getattr(parent, attr)))
1048+
self._selection = kwargs.get("selection")
10461049

10471050
self.binner = parent.binner
10481051

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,
@@ -406,6 +407,20 @@ def test_resample_groupby_agg():
406407
tm.assert_frame_equal(result, expected)
407408

408409

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

0 commit comments

Comments
 (0)