Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Allow named aggregation with Resampler.agg #43064

Merged
merged 3 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ Groupby/resample/rolling
- 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`)
- Bug in :meth:`pandas.DataFrame.ewm`, where non-float64 dtypes were silently failing (:issue:`42452`)
- Bug in :meth:`pandas.DataFrame.rolling` operation along rows (``axis=1``) incorrectly omits columns containing ``float16`` and ``float32`` (:issue:`41779`)
- Bug in :meth:`Resampler.aggregate` did not allow the use of Named Aggregation (:issue:`32803`)
-

Reshaping
^^^^^^^^^
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ def pipe(
2013-01-01 00:00:00 2.121320 3
2013-01-01 00:00:02 4.949747 7
2013-01-01 00:00:04 NaN 5

>>> r.agg(average="mean", total="sum")
average total
2013-01-01 00:00:00 1.5 3
2013-01-01 00:00:02 3.5 7
2013-01-01 00:00:04 5.0 5
"""
)

Expand All @@ -326,7 +332,7 @@ def pipe(
klass="DataFrame",
axis="",
)
def aggregate(self, func, *args, **kwargs):
def aggregate(self, func=None, *args, **kwargs):

result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
if result is None:
Expand Down
28 changes: 27 additions & 1 deletion pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd
from pandas import (
DataFrame,
NamedAgg,
Series,
)
import pandas._testing as tm
Expand Down Expand Up @@ -362,6 +363,12 @@ def test_agg():
result = t.aggregate({"A": np.mean, "B": np.std})
tm.assert_frame_equal(result, expected, check_like=True)

result = t.aggregate(A=("A", np.mean), B=("B", np.std))
tm.assert_frame_equal(result, expected, check_like=True)

result = t.aggregate(A=NamedAgg("A", np.mean), B=NamedAgg("B", np.std))
tm.assert_frame_equal(result, expected, check_like=True)

expected = pd.concat([a_mean, a_std], axis=1)
expected.columns = pd.MultiIndex.from_tuples([("A", "mean"), ("A", "std")])
for t in cases:
Expand All @@ -372,7 +379,10 @@ def test_agg():
expected.columns = ["mean", "sum"]
for t in cases:
result = t["A"].aggregate(["mean", "sum"])
tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(result, expected)

result = t["A"].aggregate(mean="mean", sum="sum")
tm.assert_frame_equal(result, expected)

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

result = t.agg(A=("A", np.sum), B=("B", lambda x: np.std(x, ddof=1)))
tm.assert_frame_equal(result, expected, check_like=True)

result = t.agg(
A=NamedAgg("A", np.sum), B=NamedAgg("B", lambda x: np.std(x, ddof=1))
)
tm.assert_frame_equal(result, expected, check_like=True)

# agg with renamers
expected = pd.concat(
[t["A"].sum(), t["B"].sum(), t["A"].mean(), t["B"].mean()], axis=1
Expand All @@ -452,6 +470,14 @@ def test_agg_misc():
with pytest.raises(KeyError, match=msg):
t[["A", "B"]].agg({"result1": np.sum, "result2": np.mean})

with pytest.raises(KeyError, match=msg):
t[["A", "B"]].agg(A=("result1", np.sum), B=("result2", np.mean))

with pytest.raises(KeyError, match=msg):
t[["A", "B"]].agg(
A=NamedAgg("result1", np.sum), B=NamedAgg("result2", np.mean)
)

# agg with different hows
expected = pd.concat(
[t["A"].sum(), t["A"].std(), t["B"].mean(), t["B"].std()], axis=1
Expand Down