Skip to content
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

BUG: RollingGroupby duplicates columns in index even with group_keys=False #37641

Closed
2 of 3 tasks
venaturum opened this issue Nov 5, 2020 · 2 comments · Fixed by #37661
Closed
2 of 3 tasks

BUG: RollingGroupby duplicates columns in index even with group_keys=False #37641

venaturum opened this issue Nov 5, 2020 · 2 comments · Fixed by #37661
Labels
Bug Regression Functionality that used to work in a prior pandas version Window rolling, ewma, expanding
Milestone

Comments

@venaturum
Copy link
Contributor

venaturum commented Nov 5, 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.


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

Code Sample, a copy-pastable example

import pandas as pd

arrays = [['val1', 'val1', 'val2'], ['val1', 'val1', 'val2']]
index = pd.MultiIndex.from_arrays(arrays, names=('idx1', 'idx2'))

s = pd.Series([1, 2, 3], index=index)
print(
    s.groupby(["idx1", "idx2"], group_keys=False).rolling(1).mean()
)

Problem description

the group_keys parameter in Seried.groupby() is broken since 1.1.0 when using .rolling()

It does not seem to be an issue if another function instead of rolling is used, eg

    s.groupby(["idx1", "idx2"], group_keys=False).mean()

In pandas=1.1.{0,1} the output is

idx1 idx2  
val1 val1  (val1, val1)   1.0
     val1  (val1, val1)   2.0
val2 val2  (val2, val1)   3.0

In pandas=1.1.{2,3,4} the output is

idx1 idx2  idx1 idx2     
val1 val1  val1 val1   1.0
                val1   2.0
val2 val2  val2 val2   3.0

Expected Output (as per pandas 1.0.*)

idx1 idx2       
val1 val1   1.0
     val1   2.0
val2 val2   3.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2ca0a2
python : 3.6.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.17134
machine : AMD64
processor : Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.1.1
numpy : 1.16.1
pytz : 2018.9
dateutil : 2.8.0
pip : 10.0.1
setuptools : 39.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.3.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10
IPython : 7.2.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 2.6.0
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@venaturum venaturum added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 5, 2020
@venaturum venaturum changed the title BUG: BUG: RollingGroupby duplicates columns in index even with group_keys=False Nov 5, 2020
@taozuoqiao
Copy link

I have the same problem (pd.__version__: 1.1.3), but the alternative approach

s.groupby(["idx1", "idx2"], group_keys=False).apply(lambda x: x.rolling(1).mean())

produces the right expected output:

idx1  idx2
val1  val1    1.0
      val1    2.0
val2  val2    3.0
dtype: float64

This is documented in pandas.DataFrame.groupby.html

group_keys: bool, default True
When calling apply, add group keys to index to identify pieces.

@jreback jreback added Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 9, 2020
@jreback jreback added this to the 1.1.5 milestone Nov 9, 2020
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Nov 9, 2020
@simonjayhawkins
Copy link
Member

first bad commit: [bad52a9] PERF: Use Indexers to implement groupby rolling (#34052)

@simonjayhawkins simonjayhawkins added the Regression Functionality that used to work in a prior pandas version label Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Regression Functionality that used to work in a prior pandas version Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants