Skip to content

Commit 7d99eda

Browse files
discortvictor
authored and
victor
committed
BUG: Pass through arguments to resample().apply() (pandas-dev#22261)
1 parent ea4431c commit 7d99eda

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ Groupby/Resample/Rolling
696696
``SeriesGroupBy`` when the grouping variable only contains NaNs and numpy version < 1.13 (:issue:`21956`).
697697
- Multiple bugs in :func:`pandas.core.Rolling.min` with ``closed='left'` and a
698698
datetime-like index leading to incorrect results and also segfault. (:issue:`21704`)
699-
-
699+
- Bug in :meth:`Resampler.apply` when passing postiional arguments to applied func (:issue:`14615`).
700700

701701
Sparse
702702
^^^^^^

pandas/core/resample.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,15 @@ def pipe(self, func, *args, **kwargs):
251251
klass='DataFrame',
252252
versionadded='',
253253
axis=''))
254-
def aggregate(self, arg, *args, **kwargs):
254+
def aggregate(self, func, *args, **kwargs):
255255

256256
self._set_binner()
257-
result, how = self._aggregate(arg, *args, **kwargs)
257+
result, how = self._aggregate(func, *args, **kwargs)
258258
if result is None:
259-
result = self._groupby_and_aggregate(arg,
259+
how = func
260+
grouper = None
261+
result = self._groupby_and_aggregate(how,
262+
grouper,
260263
*args,
261264
**kwargs)
262265

@@ -869,7 +872,7 @@ def __init__(self, obj, *args, **kwargs):
869872
self._groupby.grouper.mutated = True
870873
self.groupby = copy.copy(parent.groupby)
871874

872-
def _apply(self, f, **kwargs):
875+
def _apply(self, f, grouper=None, *args, **kwargs):
873876
"""
874877
dispatch to _upsample; we are stripping all of the _upsample kwargs and
875878
performing the original function call on the grouped object
@@ -881,7 +884,7 @@ def func(x):
881884
if isinstance(f, compat.string_types):
882885
return getattr(x, f)(**kwargs)
883886

884-
return x.apply(f, **kwargs)
887+
return x.apply(f, *args, **kwargs)
885888

886889
result = self._groupby.apply(func)
887890
return self._wrap_result(result)

pandas/tests/test_resample.py

+22
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,28 @@ def test_resample_datetime_values(self):
21762176
res = df['timestamp'].resample('2D').first()
21772177
tm.assert_series_equal(res, exp)
21782178

2179+
def test_resample_apply_with_additional_args(self):
2180+
# GH 14615
2181+
def f(data, add_arg):
2182+
return np.mean(data) * add_arg
2183+
2184+
multiplier = 10
2185+
result = self.series.resample('D').apply(f, multiplier)
2186+
expected = self.series.resample('D').mean().multiply(multiplier)
2187+
tm.assert_series_equal(result, expected)
2188+
2189+
# Testing as kwarg
2190+
result = self.series.resample('D').apply(f, add_arg=multiplier)
2191+
expected = self.series.resample('D').mean().multiply(multiplier)
2192+
tm.assert_series_equal(result, expected)
2193+
2194+
# Testing dataframe
2195+
df = pd.DataFrame({"A": 1, "B": 2},
2196+
index=pd.date_range('2017', periods=10))
2197+
result = df.groupby("A").resample("D").agg(f, multiplier)
2198+
expected = df.groupby("A").resample('D').mean().multiply(multiplier)
2199+
assert_frame_equal(result, expected)
2200+
21792201

21802202
class TestPeriodIndex(Base):
21812203
_index_factory = lambda x: period_range

0 commit comments

Comments
 (0)