From fb3f8963f29f7eb2d4465ec11e9b5fb70aadbb79 Mon Sep 17 00:00:00 2001 From: Alessandro Sivieri Date: Thu, 10 Nov 2016 17:18:15 +0100 Subject: [PATCH] Patching issue number 14615 - optional arguments for functions in resample. --- pandas/tseries/resample.py | 16 +++++++++++++--- pandas/tseries/tests/test_resample.py | 11 +++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/pandas/tseries/resample.py b/pandas/tseries/resample.py index d02c403cb3c66..c2cb637708287 100644 --- a/pandas/tseries/resample.py +++ b/pandas/tseries/resample.py @@ -319,9 +319,19 @@ def aggregate(self, arg, *args, **kwargs): self._set_binner() result, how = self._aggregate(arg, *args, **kwargs) if result is None: - return self._groupby_and_aggregate(arg, - *args, - **kwargs) + # It seems there is an ambiguity in having both named args + # and variadic in the same function, so we need to distinguish here, + # otherwise the first element of args is "eaten" by grouper in + # _groupby_and_aggregate. + if len(args) == 0: + return self._groupby_and_aggregate(arg, + *args, + **kwargs) + else: + return self._groupby_and_aggregate(arg, + None, + *args, + **kwargs) return result diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index 9d3d27f3224b4..19ed97c4a9079 100644 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -2039,6 +2039,17 @@ def test_resample_datetime_values(self): res = df['timestamp'].resample('2D').first() tm.assert_series_equal(res, exp) + def test_resample_additional_args(self): + # Issue 14615 + + rng = date_range('1/1/2011', periods = 72, freq = 'H') + rng_exp = date_range('1/1/2011', periods = 3, freq = 'D') + vals = np.tile(np.arange(0, 24), 3) + ts = Series(vals, index = rng) + ts_exp = Series([286, 286, 286], index = rng_exp) + res = ts.resample("D").apply(lambda x, y: np.sum(x) + y, 10) + tm.assert_series_equal(res, ts_exp) + class TestPeriodIndex(Base, tm.TestCase): _multiprocess_can_split_ = True