Skip to content

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

Open
@Dr-Irv

Description

@Dr-Irv

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions