Skip to content

BUG: groupby.rolling drops MultiIndex level #40067

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
JanHomann opened this issue Feb 25, 2021 · 1 comment
Closed
2 of 3 tasks

BUG: groupby.rolling drops MultiIndex level #40067

JanHomann opened this issue Feb 25, 2021 · 1 comment
Labels
Bug Window rolling, ewma, expanding

Comments

@JanHomann
Copy link

JanHomann commented Feb 25, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# prepare a dataframe where problem becomes obvious (details not important)

df = pd.DataFrame({'A': [1] * 20 + [2] * 12 + [3] * 8,
                   'B': np.arange(40)})

df = df.reset_index().set_index(['index','A']).reorder_levels([1,0])
index_mapping = {a:(a-20)/10 for a in range(20,32)}
df = df.rename(index=index_mapping)
index_mapping = {a:(a-32)/10 for a in range(32,40)}
df = df.rename(index=index_mapping)
df = df.rename_axis(index={'index':'time'})



# groupby - rolling operation (here is the bug)

df.groupby('A').rolling(4).mean()

Problem description

Applying groupby.rolling on a multiindex dataframe should preserve the level that the rolling operation is applied to. Instead that index level is removed. This loses information and causes non-unique index values.

df

Screen Shot 2021-02-25 at 4 58 03 PM

df after applying groupby('A').rolling misses index level 'time'

Screen Shot 2021-02-25 at 4 58 09 PM

Expected Output

All index levels should be preserved. In older versions of pandas (1.1.2) the index levels were preserved, but there used to be a different bug: The whole multilevel index structure was duplicated, leading to twice the number of original levels. I guess that in an attempt to fix this, this new bug got introduced.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.8.1.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : None.UTF-8

pandas : 1.2.2
numpy : 1.20.1
pytz : 2020.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 41.2.0
Cython : 0.29.20
pytest : 5.4.2
hypothesis : None
sphinx : 3.0.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.14.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.1
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.16.0
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.16.0
xlrd : 1.2.0
xlwt : None
numba : 0.48.0

@JanHomann JanHomann added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 25, 2021
@phofl
Copy link
Member

phofl commented Feb 26, 2021

Duplicate of #38523

@phofl phofl marked this as a duplicate of #38523 Feb 26, 2021
@phofl phofl closed this as completed Feb 26, 2021
@phofl phofl added Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 26, 2021
@phofl phofl added this to the No action milestone Feb 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

2 participants