Skip to content

Commit 36b9bcc

Browse files
authored
BUG: Allow named aggregation with Resampler.agg (#43064)
1 parent 3d770d3 commit 36b9bcc

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ Groupby/resample/rolling
329329
- Bug in :meth:`SeriesGroupBy.nlargest` and :meth:`SeriesGroupBy.nsmallest` would have an inconsistent index when the input Series was sorted and ``n`` was greater than or equal to all group sizes (:issue:`15272`, :issue:`16345`, :issue:`29129`)
330330
- Bug in :meth:`pandas.DataFrame.ewm`, where non-float64 dtypes were silently failing (:issue:`42452`)
331331
- Bug in :meth:`pandas.DataFrame.rolling` operation along rows (``axis=1``) incorrectly omits columns containing ``float16`` and ``float32`` (:issue:`41779`)
332+
- Bug in :meth:`Resampler.aggregate` did not allow the use of Named Aggregation (:issue:`32803`)
333+
-
332334

333335
Reshaping
334336
^^^^^^^^^

pandas/core/resample.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ def pipe(
316316
2013-01-01 00:00:00 2.121320 3
317317
2013-01-01 00:00:02 4.949747 7
318318
2013-01-01 00:00:04 NaN 5
319+
320+
>>> r.agg(average="mean", total="sum")
321+
average total
322+
2013-01-01 00:00:00 1.5 3
323+
2013-01-01 00:00:02 3.5 7
324+
2013-01-01 00:00:04 5.0 5
319325
"""
320326
)
321327

@@ -326,7 +332,7 @@ def pipe(
326332
klass="DataFrame",
327333
axis="",
328334
)
329-
def aggregate(self, func, *args, **kwargs):
335+
def aggregate(self, func=None, *args, **kwargs):
330336

331337
result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
332338
if result is None:

pandas/tests/resample/test_resample_api.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas as pd
77
from pandas import (
88
DataFrame,
9+
NamedAgg,
910
Series,
1011
)
1112
import pandas._testing as tm
@@ -362,6 +363,12 @@ def test_agg():
362363
result = t.aggregate({"A": np.mean, "B": np.std})
363364
tm.assert_frame_equal(result, expected, check_like=True)
364365

366+
result = t.aggregate(A=("A", np.mean), B=("B", np.std))
367+
tm.assert_frame_equal(result, expected, check_like=True)
368+
369+
result = t.aggregate(A=NamedAgg("A", np.mean), B=NamedAgg("B", np.std))
370+
tm.assert_frame_equal(result, expected, check_like=True)
371+
365372
expected = pd.concat([a_mean, a_std], axis=1)
366373
expected.columns = pd.MultiIndex.from_tuples([("A", "mean"), ("A", "std")])
367374
for t in cases:
@@ -372,7 +379,10 @@ def test_agg():
372379
expected.columns = ["mean", "sum"]
373380
for t in cases:
374381
result = t["A"].aggregate(["mean", "sum"])
375-
tm.assert_frame_equal(result, expected)
382+
tm.assert_frame_equal(result, expected)
383+
384+
result = t["A"].aggregate(mean="mean", sum="sum")
385+
tm.assert_frame_equal(result, expected)
376386

377387
msg = "nested renamer is not supported"
378388
for t in cases:
@@ -439,6 +449,14 @@ def test_agg_misc():
439449
expected = pd.concat([r["A"].sum(), rcustom], axis=1)
440450
tm.assert_frame_equal(result, expected, check_like=True)
441451

452+
result = t.agg(A=("A", np.sum), B=("B", lambda x: np.std(x, ddof=1)))
453+
tm.assert_frame_equal(result, expected, check_like=True)
454+
455+
result = t.agg(
456+
A=NamedAgg("A", np.sum), B=NamedAgg("B", lambda x: np.std(x, ddof=1))
457+
)
458+
tm.assert_frame_equal(result, expected, check_like=True)
459+
442460
# agg with renamers
443461
expected = pd.concat(
444462
[t["A"].sum(), t["B"].sum(), t["A"].mean(), t["B"].mean()], axis=1
@@ -452,6 +470,14 @@ def test_agg_misc():
452470
with pytest.raises(KeyError, match=msg):
453471
t[["A", "B"]].agg({"result1": np.sum, "result2": np.mean})
454472

473+
with pytest.raises(KeyError, match=msg):
474+
t[["A", "B"]].agg(A=("result1", np.sum), B=("result2", np.mean))
475+
476+
with pytest.raises(KeyError, match=msg):
477+
t[["A", "B"]].agg(
478+
A=NamedAgg("result1", np.sum), B=NamedAgg("result2", np.mean)
479+
)
480+
455481
# agg with different hows
456482
expected = pd.concat(
457483
[t["A"].sum(), t["A"].std(), t["B"].mean(), t["B"].std()], axis=1

0 commit comments

Comments
 (0)