Skip to content

BUG: RollingGroupby.agg returns no columns when column selected in list is one of the groupby columns #56705

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
2 of 3 tasks
hsorsky opened this issue Jan 2, 2024 · 1 comment
Labels
Bug Window rolling, ewma, expanding

Comments

@hsorsky
Copy link

hsorsky commented Jan 2, 2024

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

import pandas as pd

X = pd.DataFrame({"group": [1, 2, 2, 3, 3, 3], "other": [1, 1, 1, 1, 1, 1]})
X.groupby(["group"])[["group"]].rolling(2, min_periods=1).agg("count")

Issue Description

A DataFrame with no columns is returned.

Empty DataFrame
Columns: []
Index: [(1, 0), (2, 1), (2, 2), (3, 3), (3, 4), (3, 5)]

As an FYI, X.groupby(["group"])["group"].rolling(2, min_periods=1).agg("count") seems fine

group   
1      0    1.0
2      1    1.0
       2    2.0
3      3    1.0
       4    2.0
       5    2.0
Name: group, dtype: float64

as does
X.groupby(["group"])[["other"]].rolling(2, min_periods=1).agg("count")

         other
group         
1     0    1.0
2     1    1.0
      2    2.0
3     3    1.0
      4    2.0
      5    2.0

as does
X.groupby(["group"])["other"].rolling(2, min_periods=1).agg("count")

group   
1      0    1.0
2      1    1.0
       2    2.0
3      3    1.0
       4    2.0
       5    2.0
Name: other, dtype: float64

Also, the problem does not seem to persist if we don't perform a non collapsing aggregation:

X.groupby(["group"])[["group"]].agg("count")

       group
group       
1          1
2          2
3          3

but does if we perform expanding aggregation:

X.groupby(["group"])[["group"]].expanding(min_periods=1).agg("count")

Empty DataFrame
Columns: []
Index: [(1, 0), (2, 1), (2, 2), (3, 3), (3, 4), (3, 5)]

Expected Behavior

It would output the same as when one does

X.groupby(["group"])[["other"]].rolling(2, min_periods=1).agg("count")

but with "group" as the column name.

Installed Versions

INSTALLED VERSIONS
------------------
commit              : a671b5a8bf5dd13fb19f0e88edc679bc9e15c673
python              : 3.10.11.final.0
python-bits         : 64
OS                  : Darwin
OS-release          : 22.4.0
Version             : Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000
machine             : arm64
processor           : arm
byteorder           : little
LC_ALL              : None
LANG                : en_US.UTF-8
LOCALE              : en_US.UTF-8

pandas              : 2.1.4
numpy               : 1.26.3
pytz                : 2023.3.post1
dateutil            : 2.8.2
setuptools          : 65.5.0
pip                 : 23.0.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             : 8.19.0
pandas_datareader   : None
bs4                 : None
bottleneck          : None
dataframe-api-compat: None
fastparquet         : None
fsspec              : None
gcsfs               : None
matplotlib          : None
numba               : None
numexpr             : None
odfpy               : None
openpyxl            : None
pandas_gbq          : None
pyarrow             : None
pyreadstat          : None
pyxlsb              : None
s3fs                : None
scipy               : None
sqlalchemy          : None
tables              : None
tabulate            : None
xarray              : None
xlrd                : None
zstandard           : None
tzdata              : 2023.4
qtpy                : None
pyqt5               : None

@hsorsky hsorsky added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 2, 2024
@rhshadrach
Copy link
Member

Thanks for the report, related to #40341 (comment) (cc @mroeschke, @jbrockmendel).

The rest of groupby will keep the grouping column if you select it, e.g.

df[["a", "b"]].groupby("a")[["a", "b"]].sum()

will sum both a and b in the result. I think we should agree with that here.

@rhshadrach rhshadrach added Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

2 participants