diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index d7feb6e547b22..83f88e2c23f62 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -693,7 +693,7 @@ Groupby/Resample/Rolling ``SeriesGroupBy`` when the grouping variable only contains NaNs and numpy version < 1.13 (:issue:`21956`). - Multiple bugs in :func:`pandas.core.Rolling.min` with ``closed='left'` and a datetime-like index leading to incorrect results and also segfault. (:issue:`21704`) -- +- Bug in :meth:`Resampler.apply` when passing postiional arguments to applied func (:issue:`14615`). Sparse ^^^^^^ diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 4a3625b2d7a2d..3b70112b8ad00 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -234,12 +234,15 @@ def pipe(self, func, *args, **kwargs): klass='DataFrame', versionadded='', axis='')) - def aggregate(self, arg, *args, **kwargs): + def aggregate(self, func, *args, **kwargs): self._set_binner() - result, how = self._aggregate(arg, *args, **kwargs) + result, how = self._aggregate(func, *args, **kwargs) if result is None: - result = self._groupby_and_aggregate(arg, + how = func + grouper = None + result = self._groupby_and_aggregate(how, + grouper, *args, **kwargs) @@ -852,7 +855,7 @@ def __init__(self, obj, *args, **kwargs): self._groupby.grouper.mutated = True self.groupby = copy.copy(parent.groupby) - def _apply(self, f, **kwargs): + def _apply(self, f, grouper=None, *args, **kwargs): """ dispatch to _upsample; we are stripping all of the _upsample kwargs and performing the original function call on the grouped object @@ -864,7 +867,7 @@ def func(x): if isinstance(f, compat.string_types): return getattr(x, f)(**kwargs) - return x.apply(f, **kwargs) + return x.apply(f, *args, **kwargs) result = self._groupby.apply(func) return self._wrap_result(result) diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py index d11077668359b..3310c97e06e3d 100644 --- a/pandas/tests/test_resample.py +++ b/pandas/tests/test_resample.py @@ -2165,6 +2165,28 @@ def test_resample_datetime_values(self): res = df['timestamp'].resample('2D').first() tm.assert_series_equal(res, exp) + def test_resample_apply_with_additional_args(self): + # GH 14615 + def f(data, add_arg): + return np.mean(data) * add_arg + + multiplier = 10 + result = self.series.resample('D').apply(f, multiplier) + expected = self.series.resample('D').mean().multiply(multiplier) + tm.assert_series_equal(result, expected) + + # Testing as kwarg + result = self.series.resample('D').apply(f, add_arg=multiplier) + expected = self.series.resample('D').mean().multiply(multiplier) + tm.assert_series_equal(result, expected) + + # Testing dataframe + df = pd.DataFrame({"A": 1, "B": 2}, + index=pd.date_range('2017', periods=10)) + result = df.groupby("A").resample("D").agg(f, multiplier) + expected = df.groupby("A").resample('D').mean().multiply(multiplier) + assert_frame_equal(result, expected) + class TestPeriodIndex(Base): _index_factory = lambda x: period_range