Skip to content

BUG: Groupby ignores group_keys=False when followed by a rolling calculation #57686

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

Closed
3 tasks done
furechan opened this issue Feb 29, 2024 · 4 comments
Closed
3 tasks done

Comments

@furechan
Copy link

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 io
import pandas as pd

DATA = """
ticker,date,price,volume
AAA,2021-01-01,10.0,1000
AAA,2021-01-02,11.0,1000
AAA,2021-01-03,12.0,1000
AAA,2021-01-04,11.0,1000
BBB,2021-01-01,20.0,1000
BBB,2021-01-02,21.0,1000
BBB,2021-01-03,22.0,1000
BBB,2021-01-04,21.0,1000
"""

f = io.StringIO(DATA)
df = pd.read_csv(f, parse_dates=['date'], index_col=[0, 1])

df.price.groupby("ticker", group_keys=False).rolling(2).mean()

Issue Description

A groupby followed by a rolling calculation ignores group_keys=False.

In the example below the column ticker appears twice in the result even though we have group_keys=False

df.price.groupby("ticker", group_keys=False).rolling(2).mean()

Expected Behavior

The groupby column 'ticker' should not repeat since we have group_keys=False.

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.9.18.final.0
python-bits : 64
OS : Darwin
OS-release : 23.3.0
Version : Darwin Kernel Version 23.3.0: Wed Dec 20 21:31:00 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 58.1.0
pip : 24.0
Cython : None
pytest : 8.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.1.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.18.1
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.3
numba : 0.59.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@furechan furechan added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 29, 2024
@rhshadrach
Copy link
Member

Thanks for the report. The groupby docs state:

When calling apply and...

the group_keys argument is only to impact .groupby(...).apply and not other methods. Closing for now - if you think I've missed something, comment here and we can reopen.

@rhshadrach rhshadrach added Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 1, 2024
@furechan
Copy link
Author

furechan commented Mar 1, 2024

Indeed, you are right, the docs specifically mention the group_keys for the apply method.

To give some context to my inquiry, when calling say shift(1) on the groupby there is no repetition of the index and you get a result with the same index as the original data.

df.price.groupby("ticker").shift(1)

However when calling rolling(2).mean() there is repetition of the index. How do we know which method duplicates the index and which does not? And how do we fix the duplicate index ?

So here, I was hoping that group_keys=False would do the trick.

As a side note, I noticed that using as_index=False at the dataframe level fixes the issue.

df.groupby("ticker", as_index=False).price.rolling(2).mean()

However as_index is applicable only at dataframe level, so it seems that I am left short of a solution when working at the series level...

Thanks for the consideration.

@furechan
Copy link
Author

furechan commented Mar 1, 2024

I still think the result in my example is wrong, even if the problem is not with the group_keys flag. So if you don't mind, I will create a different issue with a rephrased explanation.
Thank you

@rhshadrach
Copy link
Member

Related: #36507

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants