Skip to content

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

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
foolcage opened this issue Dec 23, 2020 · 3 comments
Closed
2 of 3 tasks
Labels
Bug Duplicate Report Duplicate issue or pull request

Comments

@foolcage
Copy link

foolcage commented Dec 23, 2020

  • 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

@foolcage foolcage added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 23, 2020
@foolcage
Copy link
Author

the result of pandas<1.1.5

In [7]:  arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']), np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
   
In [8]: df = pd.DataFrame(np.random.randn(8, 4), index=arrays)

In [9]: df.groupby(level=0).rolling(window=2).mean()
Out[9]:
                    0         1         2         3
bar bar one       NaN       NaN       NaN       NaN
        two  0.093151 -0.867964  0.036143 -0.806151
baz baz one       NaN       NaN       NaN       NaN
        two  0.061005  0.151558  0.639276 -0.057748
foo foo one       NaN       NaN       NaN       NaN
        two -0.622565  0.253112  0.678680  0.149919
qux qux one       NaN       NaN       NaN       NaN
        two -0.672658 -0.938227  0.178649  0.293549
In [12]: df.index.names=['a','b']

In [13]: df.groupby(level=0).rolling(window=2).mean()
Out[13]:
                    0         1         2         3
a   a   b
bar bar one       NaN       NaN       NaN       NaN
        two  0.093151 -0.867964  0.036143 -0.806151
baz baz one       NaN       NaN       NaN       NaN
        two  0.061005  0.151558  0.639276 -0.057748
foo foo one       NaN       NaN       NaN       NaN
        two -0.622565  0.253112  0.678680  0.149919
qux qux one       NaN       NaN       NaN       NaN
        two -0.672658 -0.938227  0.178649  0.293549

@jreback
Copy link
Contributor

jreback commented Dec 23, 2020

pls check in 1.2.0rc0
cc @mroeschke

@simonjayhawkins
Copy link
Member

duplicate of #38523?

@simonjayhawkins simonjayhawkins added Duplicate Report Duplicate issue or pull request and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request
Projects
None yet
Development

No branches or pull requests

3 participants