BUG: df.groupby().resample()[[cols]] raise KeyError when resampling on index · Issue #50840 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

from pandas import ( DataFrame, Index, Series, Timestamp, ) from pandas.core.indexes.datetimes import date_range

df = DataFrame( data={ "group": [0, 0, 0, 0, 1, 1, 1, 1], "val": [3, 1, 4, 1, 5, 9, 2, 6], }, index=Series( date_range(start="2016-01-01", periods=8), name="date", ), ) result = df.groupby("group").resample("2D")[["val"]].mean()

Issue Description

If I try and do a GroupBy and Resample on a DataFrame, asking for only a subset of the columns before the aggregate, I get a KeyError.

Expected Behavior

I expect to see that the following DataFrame where the aggregate has been performed on the requested column(s)

expected = DataFrame(
    data={
        "val": [2.0, 2.5, 7.0, 4.0],
    },
    index=Index(
        data=[
            (0, Timestamp("2016-01-01")),
            (0, Timestamp("2016-01-03")),
            (1, Timestamp("2016-01-05")),
            (1, Timestamp("2016-01-07")),
        ],
        name=("group", "date"),
    ),
)

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.9.15.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-66-generic
Version : #75-Ubuntu SMP Tue Oct 1 05:24:09 UTC 2019
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.2
numpy : 1.24.1
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 66.0.0
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None