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

Comment From: jreback

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

Comment From: jreback

you need to use level

Comment From: Dr-Irv

@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.

Comment From: thierryzoller

+1

Comment From: adrienpacifico

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()