Skip to content

Commit a8b9d72

Browse files
committed
Fixed additional passing args to resample().apply
1 parent 475e391 commit a8b9d72

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ Groupby/Resample/Rolling
650650
``SeriesGroupBy`` when the grouping variable only contains NaNs and numpy version < 1.13 (:issue:`21956`).
651651
- Multiple bugs in :func:`pandas.core.Rolling.min` with ``closed='left'` and a
652652
datetime-like index leading to incorrect results and also segfault. (:issue:`21704`)
653-
-
653+
- Bug in :meth:`Resampler.apply` when passing an additional argument to applied func (:issue:`14615`).
654654

655655
Sparse
656656
^^^^^^

pandas/core/resample.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ def aggregate(self, arg, *args, **kwargs):
239239
self._set_binner()
240240
result, how = self._aggregate(arg, *args, **kwargs)
241241
if result is None:
242+
grouper = None
242243
result = self._groupby_and_aggregate(arg,
244+
grouper,
243245
*args,
244246
**kwargs)
245247

@@ -852,7 +854,7 @@ def __init__(self, obj, *args, **kwargs):
852854
self._groupby.grouper.mutated = True
853855
self.groupby = copy.copy(parent.groupby)
854856

855-
def _apply(self, f, **kwargs):
857+
def _apply(self, f, *args, **kwargs):
856858
"""
857859
dispatch to _upsample; we are stripping all of the _upsample kwargs and
858860
performing the original function call on the grouped object

pandas/tests/test_resample.py

+14
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,20 @@ def test_resample_datetime_values(self):
21652165
res = df['timestamp'].resample('2D').first()
21662166
tm.assert_series_equal(res, exp)
21672167

2168+
def test_resample_apply_with_additional_args(self):
2169+
def f(data, add_arg):
2170+
return np.mean(data) * add_arg
2171+
2172+
multiplier = 10
2173+
result = self.series.resample('D').apply(f, multiplier)
2174+
expected = self.series.resample('D').mean().multiply(multiplier)
2175+
tm.assert_series_equal(result, expected)
2176+
2177+
# Testing as kwarg
2178+
result = self.series.resample('D').apply(f, add_arg=multiplier)
2179+
expected = self.series.resample('D').mean().multiply(multiplier)
2180+
tm.assert_series_equal(result, expected)
2181+
21682182

21692183
class TestPeriodIndex(Base):
21702184
_index_factory = lambda x: period_range

0 commit comments

Comments
 (0)