Skip to content

Unable to pass additional arguments to resample().apply() #22261

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

Merged
merged 2 commits into from
Aug 22, 2018
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: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^
Expand Down
13 changes: 8 additions & 5 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test both as an arg and as a kwargs

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
Expand Down