Repository: pandas-dev/pandas

Issue:
Unable to pass additional arguments to resample().apply()
```python
import pandas as pd
import numpy as np

def stuff(vals, th):
    return np.mean(vals)

rng = pd.date_range('1/1/2011', periods=72, freq='H')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
df_res = ts.resample("D").apply(stuff, 10)

```

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Darwin
OS-release: 16.1.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.19.1
nose: 1.3.0
pip: 9.0.0
setuptools: 28.7.1
Cython: 0.20.2
numpy: 1.11.2
scipy: 0.13.2
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: 1.4.8
patsy: None
dateutil: 2.5.3
pytz: 2016.7
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.5.2
matplotlib: 1.3.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.12
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None
<details>
In this version, it seems to be impossible to apply a function with an additional argument (which is not used in the example above): checking the Pandas 0.19.1 code, what happens is that the grouped argument of the function _groupby_and_aggregate gets the first value of the *args argument passed from the function aggregate, which is clearly wrong.

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    df_res = ts.resample("D").apply(stuff, 10)
  File "/usr/local/lib/python2.7/site-packages/pandas/tseries/resample.py", line 324, in aggregate
    **kwargs)
  File "/usr/local/lib/python2.7/site-packages/pandas/tseries/resample.py", line 405, in _groupby_and_aggregate
    result = grouped.apply(how, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/groupby.py", line 694, in apply
    return self._python_apply_general(f)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/groupby.py", line 697, in _python_apply_general
    keys, values, mutated = self.grouper.apply(f, self._selected_obj,
AttributeError: 'int' object has no attribute 'apply'
</details>



Discussion:
hmm thought we had an issue for this already - yep looks like a bug
if you'd like to submit a PR to fix would be great

I will look at this today if no one else working on it?

Resolving patch:
diff --git a/pandas/core/resample.py b/pandas/core/resample.py
--- 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)


Test patch:
diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py
--- 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