Skip to content

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

@fumoboy007

Description

@fumoboy007

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>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions