Skip to content

BUG: groupby.rolling drops MultiIndex level #40067

Closed
@JanHomann

Description

@JanHomann
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions