Skip to content

BUG: multiple index dataframe with index names groupy rolling computing reture wrong index #38655

Closed
@foolcage

Description

@foolcage
  • 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.


Code Sample

import pandas as pd
arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']), np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
df.groupby(level=0).rolling(window=2).mean()

the result:

                    0         1         2         3
bar bar one       NaN       NaN       NaN       NaN
        two -0.361421 -0.696686 -0.746137 -0.053621
baz baz one       NaN       NaN       NaN       NaN
        two  1.014213  0.575803  0.199990 -0.596995
foo foo one       NaN       NaN       NaN       NaN
        two -0.337821  0.921825  0.256034 -0.572588
qux qux one       NaN       NaN       NaN       NaN
        two -0.404824 -1.209763  0.581025 -0.741480
#now set the name to the index
df.index.names=['a','b']
df.groupby(level=0).rolling(window=2).mean()

the result:

            0         1         2         3
a
bar       NaN       NaN       NaN       NaN
bar -0.361421 -0.696686 -0.746137 -0.053621
baz       NaN       NaN       NaN       NaN
baz  1.014213  0.575803  0.199990 -0.596995
foo       NaN       NaN       NaN       NaN
foo -0.337821  0.921825  0.256034 -0.572588
qux       NaN       NaN       NaN       NaN
qux -0.404824 -1.209763  0.581025 -0.741480

Problem description

multiple index dataframe with index names groupy rolling computing reture wrong index

Expected Output

the return result should keep the original index

Output of pd.show_versions()

In [36]: pd.show_versions()

INSTALLED VERSIONS

commit : b5958ee
python : 3.8.2.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 : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8

pandas : 1.1.5
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.2.4
setuptools : 50.3.2
Cython : None
pytest : 6.2.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.2.14
tables : None
tabulate : None
xarray : None
xlrd : 1.1.0
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions