Skip to content

BUG: Duplicate index level in pd.DataFrame.groupby.expanding #47181

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
2 of 3 tasks
TomasTurlik opened this issue May 31, 2022 · 3 comments
Closed
2 of 3 tasks

BUG: Duplicate index level in pd.DataFrame.groupby.expanding #47181

TomasTurlik opened this issue May 31, 2022 · 3 comments
Labels
Usage Question Window rolling, ewma, expanding

Comments

@TomasTurlik
Copy link

TomasTurlik commented May 31, 2022

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 
df = pd.DataFrame([1,2,3,4,5,6],index=pd.MultiIndex.from_tuples([('A','a'),('A','b'),('A','c'),('B','a'),('B','b'),('B','c')]))
df.index.rename(('level_0','level_1'),inplace=True)

df.groupby("level_1").mean()

#           0
# level_1     
# a        2.5
# b        3.5
# c        4.5

df.groupby("level_1").expanding().mean()

#                           0
# level_1 level_0 level_1     
# a       A       a        1.0
#         B       a        2.5
# b       A       b        2.0
#         B       b        3.5
# c       A       c        3.0
#         B       c        4.5

Issue Description

I am not sure that this is the expected behavior as intuitively I would expect the index of the returning dataframe to be the same when using groupby.expanding.mean and groupby.mean.
EDIT: I don't mean literally the same but similar, keeping in mind the nature of 1st one returning vector and second one returning scalar

Expected Behavior

I would expect the previous code snippet to produce

print(df.groupby("level_1").expanding().mean())

                  0
level_1 level_0     
a       A       1.0
        B       2.5
b       A       2.0
        B       3.5
c       A       3.0
        B       4.5

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-110-generic
Version : #124-Ubuntu SMP Thu Apr 14 19:46:19 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.2
numpy : 1.21.5
pytz : 2021.3
dateutil : 2.8.2
pip : 22.1
setuptools : 44.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.02.0
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.5.1
numba : 0.55.1
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : 1.4.31
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
zstandard : None

@TomasTurlik TomasTurlik added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 31, 2022
@phofl
Copy link
Member

phofl commented May 31, 2022

@phofl phofl closed this as completed May 31, 2022
@phofl phofl added Usage Question Window rolling, ewma, expanding and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 31, 2022
@TomasTurlik
Copy link
Author

@phofl I guess If I want to discuss/ask about reasons behind this functionality I should open a new Feature Request/Question issue, right?

@phofl
Copy link
Member

phofl commented Jun 1, 2022

Could you check the linked issue first? I think there was already some discussion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Usage Question Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

2 participants