Skip to content

Grouper should support key from index name (just like groupby supports grouping by index names) #19542

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

Open
Dr-Irv opened this issue Feb 5, 2018 · 5 comments

Comments

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Feb 5, 2018

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

rng = pd.date_range('1/1/2018', periods=72, freq='H')
rngIndex = pd.Index(rng, name='DateTime')
tsdf = pd.DataFrame(np.random.randn(len(rng)), index=rngIndex, columns=['x'])


tsdf.groupby(pd.Grouper(freq='1D', key='DateTime')).mean()

Problem description

The above produces the following error:

KeyError: 'The grouper name DateTime is not found'

There is a workaround, which is to do reset_index():

tsdf.reset_index().groupby(pd.Grouper(freq='1D', key='DateTime')).mean()

Since v 0.20.0 introduced the support of grouping by index level names, I think that Grouper should do the same.

Expected Output

                   x
DateTime            
2018-01-01  0.250264
2018-01-02 -0.186042
2018-01-03  0.025520

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.22.0
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.13.1
scipy: 0.19.1
pyarrow: None
xarray: 0.10.0
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: None
numexpr: None
feather: None
matplotlib: 2.1.1
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.1.13
pymysql: None
psycopg2: 2.7.3.2 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: 0.1.3
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Feb 5, 2018

In [2]: rng = pd.date_range('1/1/2018', periods=72, freq='H')
   ...: rngIndex = pd.Index(rng, name='DateTime')
   ...: tsdf = pd.DataFrame(np.random.randn(len(rng)), index=rngIndex, columns=['x'])
   ...: 
   ...: 
   ...: tsdf.groupby(pd.Grouper(freq='1D', level='DateTime')).mean()
   ...: 
   ...: 
Out[2]: 
                   x
DateTime            
2018-01-01  0.089906
2018-01-02  0.257909
2018-01-03 -0.276228

@jreback
Copy link
Contributor

jreback commented Feb 5, 2018

you need to use level

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Feb 5, 2018

@jreback OK, using level is a better workaround. But my point here is that the API is not consistent. When doing a regular groupby, I can use a mix of names from the index and the columns and not have to worry about whether a name refers to the index or the column, and also not worry about which level number each index name is at. But in Grouper, I now need to know whether the name is in the index or in the columns, and if in the index, I need to know the position within that index.

So my suggestion is an enhancement, which is to allow using index names in the key argument of Grouper.

@thierryzoller
Copy link

+1

@adrienpacifico
Copy link
Contributor

There is a workaround, which is to do reset_index():
tsdf.reset_index().groupby(pd.Grouper(freq='1D', key='DateTime')).mean()

You can also use .resample instead: tsdf.resample("1D").mean()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants