Skip to content

BUG: groupby results in index with duplicate levels even when group_keys=False #59881

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
fumoboy007 opened this issue Sep 23, 2024 · 4 comments
Closed
3 tasks done

Comments

@fumoboy007
Copy link

fumoboy007 commented Sep 23, 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

from pandas import DataFrame

df = DataFrame(
   data={
      'A': [1, 2, 1]
   },
   index=['one', 'two', 'one']
)

transformed_df = (
   df
   .groupby(level=0,
            group_keys=False)
   .rolling(window=2,
            min_periods=1)
   .sum()
)
print(transformed_df)

Issue Description

The example creates a DataFrame with duplicate index levels even though group_keys=False:

>>> print(transformed_df)
           A
one one  1.0
    one  2.0
two two  2.0

Expected Behavior

group_keys=False should produce a DataFrame with the same index as the original:

     A
one  1.0
one  2.0
two  2.0

Installed Versions

``` INSTALLED VERSIONS ------------------ commit : 0691c5c python : 3.11.10 python-bits : 64 OS : Darwin OS-release : 23.6.0 Version : Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:30 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6000 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0
pip : 24.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : 1.1
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

</details>
@fumoboy007 fumoboy007 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 23, 2024
@rhshadrach
Copy link
Member

Thanks for the report. As the API docs state, group_keys only impacts groupby.apply.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html

Related: #51751

Closing. If you believe I missed something, please 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 Sep 24, 2024
@fumoboy007
Copy link
Author

Ahhh thank you @rhshadrach!

I think we can reopen this issue to make the documentation a little clearer:

  1. Use the inline code style for the word apply.
  2. It seems this parameter also works for transform, so that should be documented too.

Moreover, do you think it would be reasonable for this parameter to be moved to the apply/transform methods themselves since it only applies to those methods? Maybe something to consider for Pandas 3.0 since it would be a breaking change.

@fumoboy007
Copy link
Author

It seems this parameter also works for transform, so that should be documented too.

Hmm not quite. It seems transform ignores group_keys and always treats it as False.

@crnolan
Copy link

crnolan commented Mar 5, 2025

It seems to me that GroupBy.rolling ignores group_keys and always treats it as True, meaning that if the groupby was performed on the index, the index gets doubled up.

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

3 participants