Skip to content

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

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

Closed
sivieri opened this issue Nov 8, 2016 · 2 comments · Fixed by #22261
Closed

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

sivieri opened this issue Nov 8, 2016 · 2 comments · Fixed by #22261
Labels
Milestone

Comments

@sivieri
Copy link

sivieri commented Nov 8, 2016

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

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
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'

@jreback
Copy link
Contributor

jreback commented Nov 8, 2016

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

@PyJay
Copy link

PyJay commented May 15, 2018

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

@jreback jreback modified the milestones: Contributions Welcome, 0.24.0 Aug 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants