Skip to content

Using rolling method call on a pandas.core.groupby.DataFrameGroupBy object results in an AttributeError, previous rolling methods are deprecated #12782

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
gregsifr opened this issue Apr 3, 2016 · 1 comment
Labels
Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode Usage Question

Comments

@gregsifr
Copy link

gregsifr commented Apr 3, 2016

Code Sample

df.groupby(level=['first_name','last_name']).rolling(window=5)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-35-1ad06da0e074> in <module>()
----> 1 df.groupby(level=['first_name','last_name']).rolling(window=5)#.corr(df['px_last'])

/home/code/anaconda2/lib/python2.7/site-packages/pandas/core/groupby.pyc in __getattr__(self, attr)
    482             return self[attr]
    483         if hasattr(self.obj, attr):
--> 484             return self._make_wrapper(attr)
    485 
    486         raise AttributeError("%r object has no attribute %r" %

/home/code/anaconda2/lib/python2.7/site-packages/pandas/core/groupby.pyc in _make_wrapper(self, name)
    496                    "using the 'apply' method".format(kind, name,
    497                                                      type(self).__name__))
--> 498             raise AttributeError(msg)
    499 
    500         # need to setup the selection

AttributeError: Cannot access callable attribute 'rolling' of 'DataFrameGroupBy' objects, try using the 'apply' method

Copy and paste runnable code sample

import pandas as pd
import numpy as np

dates = pd.date_range('20130101',periods=6).append(pd.date_range('20130101',periods=6))
df = pd.DataFrame(np.random.randn(12,2),index=dates,columns=list('AB'))
df.groupby(df.index).rolling(window=5)

Expected Output

The code should result in a pandas.core.window.Rolling object.

df.groupby(level=['first_name','last_name'])['column'].apply(pd.rolling_mean, 5) works however according to the documentation this functionality has been deprecated?

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.11.final.0
python-bits: 64
OS: Linux
OS-release: 3.19.0-56-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_AU.UTF-8

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.1
setuptools: 20.3
Cython: 0.23.5
numpy: 1.10.4
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.1.2
sphinx: 1.3.5
patsy: 0.4.0
dateutil: 2.5.1
pytz: 2016.3
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.5.1
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.4
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.12
pymysql: None
psycopg2: 2.6.1 (dt dec pq3 ext)
jinja2: 2.8
boto: 2.39.0
@jreback
Copy link
Contributor

jreback commented Apr 3, 2016

well you have 2 issues.

  1. groupby(..).rolling(..) is not implemented (it will be in 0.18.1, but is not in 0.18.0), see ENH: allow .rolling / .expanding as groupby methods #12743
  2. pd.rolling_corr is deprecated as the message says, see the whatsnew

so as the mesasge says

df.groupby(..).apply(lambda x: x.rolling(...).corr()) is the soln.

@jreback jreback closed this as completed Apr 3, 2016
@jreback jreback added Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode Usage Question labels Apr 3, 2016
@jreback jreback added this to the No action milestone Apr 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode Usage Question
Projects
None yet
Development

No branches or pull requests

2 participants