Skip to content
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

BUG: df.groupby().resample()[[cols]] raise KeyError when resampling on index #50840

Closed
3 tasks done
czroth opened this issue Jan 18, 2023 · 0 comments · Fixed by #50876
Closed
3 tasks done

BUG: df.groupby().resample()[[cols]] raise KeyError when resampling on index #50840

czroth opened this issue Jan 18, 2023 · 0 comments · Fixed by #50876
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@czroth
Copy link
Contributor

czroth commented Jan 18, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

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

@czroth czroth added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 18, 2023
czroth added a commit to czroth/pandas that referenced this issue Jan 18, 2023
czroth added a commit to czroth/pandas that referenced this issue Jan 19, 2023
czroth added a commit to czroth/pandas that referenced this issue Jan 19, 2023
czroth added a commit to czroth/pandas that referenced this issue Jan 20, 2023
mroeschke pushed a commit that referenced this issue Jan 23, 2023
…licit list of columns. (#50876)

* Add failing test reproducing groupby-resample KeyError (#50840)

* Fix groupby-resample KeyError (#50840) by adding None check.

* Update whatsnew for #50840

* Improve coverage with multi and missing column groupby-resample tests.

* Refactor groupby-resample tests via TestCase to remove duplicate code.

* Revert "Refactor groupby-resample tests via TestCase to remove duplicate code."

This reverts commit d522606.

* Fix typo in bug fix entry for #50840 in doc/source/whatsnew/v2.0.0.rst
pooja-subramaniam pushed a commit to pooja-subramaniam/pandas that referenced this issue Jan 25, 2023
…licit list of columns. (pandas-dev#50876)

* Add failing test reproducing groupby-resample KeyError (pandas-dev#50840)

* Fix groupby-resample KeyError (pandas-dev#50840) by adding None check.

* Update whatsnew for pandas-dev#50840

* Improve coverage with multi and missing column groupby-resample tests.

* Refactor groupby-resample tests via TestCase to remove duplicate code.

* Revert "Refactor groupby-resample tests via TestCase to remove duplicate code."

This reverts commit d522606.

* Fix typo in bug fix entry for pandas-dev#50840 in doc/source/whatsnew/v2.0.0.rst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
1 participant