Skip to content

BUG: Groupby followed by calculations like rolling, ewm, expanding should not duplicate the group keys index #57690

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 Mar 1, 2024 · 4 comments
Labels
API Design Bug Duplicate Report Duplicate issue or pull request Window rolling, ewma, expanding

Comments

@furechan
Copy link

furechan commented Mar 1, 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 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").rolling(2).mean()

Issue Description

The result series has a duplicate index column 'ticker' as in ["ticker, "ticker", "date"]

Expected Behavior

Some calculations like shift, pct_change, etc can be applied to a series groupby and they will not change the resulting index. This makes sense because these functions are "one-to-one", they keep the series index intact. However other calculations like rolling, ewm, expanding which are also one-to-one, introduce a duplicate index when applied to a groupby.

As a side note, this problem is already addressed for the apply method with the group_keys=False option. There is also an as_index=False option at the DataFrame level that seems to fix the problem, but it is available only at DataFrame level.

It would make sense that rolling, ewm, expanding behave the same way as shift, pct_change, etc. Short of changing the behavior there should be an option like as_index=False or group_keys=False to fix the duplicate index when working with series.

Thanks

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 Mar 1, 2024
@rhshadrach
Copy link
Member

Related: #51751 and #36507

@rhshadrach rhshadrach added API Design Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 1, 2024
@rhshadrach
Copy link
Member

It seems to me this issue is captured by the two I linked above, would you agree @furechan?

@furechan
Copy link
Author

furechan commented Mar 1, 2024

Hi @rhshadrach,

I agree that #51751 seems to be pointing to the same issue! rolling should be a transform in the same way that shift, pct_change are. The second issue #36507 is only a partial work around because from my experience using as_index=False does not add group keys to the index but it still adds them as data columns. If there had to be a work around, I would say the most logical would be to extend group_keys=False to rolling etc ...

Thank you

@rhshadrach
Copy link
Member

group_keys was added specifically to give users some flexibility with apply. I would be very hesitant to expanding it to other methods, especially because it is very similar to as_index.

Closing as a duplicate of #51751.

@rhshadrach rhshadrach added the Duplicate Report Duplicate issue or pull request label Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Bug Duplicate Report Duplicate issue or pull request Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

2 participants