Skip to content

BUG: groupby.rolling.mean seems to roll over different groups when center=True #37141

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
behrenhoff opened this issue Oct 15, 2020 · 3 comments · Fixed by #37154
Closed
2 of 3 tasks

BUG: groupby.rolling.mean seems to roll over different groups when center=True #37141

behrenhoff opened this issue Oct 15, 2020 · 3 comments · Fixed by #37154
Labels
Window rolling, ewma, expanding
Milestone

Comments

@behrenhoff
Copy link
Contributor

behrenhoff commented Oct 15, 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.


To reproduce:

import pandas as pd

# Create a DF with a group column and more than 1 group (10 rows toy data)
dfBug = pd.DataFrame(data={
    'Date': pd.date_range('2020-01-01', '2020-01-10'),
    'gb': ['group_1'] * 6 + ['group_2'] * 4,
    'value': range(10),
})

center = True  # Problem only occurs when center=True
# the apply produces the expected result
via_apply = dfBug.groupby('gb').apply(lambda df: \
    df.set_index('Date').rolling(6, center=center, min_periods=1).value.mean())
# the groupby.rolling... produces a different result
via_groupbyrolling = dfBug.groupby('gb') \
    .rolling(6, on='Date', center=center, min_periods=1).value.mean()

print(pd.concat([via_apply.rename('apply'), 
                 via_groupbyrolling.rename('groupbyrolling'),
                 (via_apply == via_groupbyrolling).rename('is-equal'),
                ], axis=1))

assert (abs(via_apply - via_groupbyrolling) < 1e-4).all()

Problem description

The code via apply and via groupbyrolling should produce identical results, yet the groupbyrolling version seems to ignore the grouping for the mean. Strange enough, when setting center to False, both variants produce identical results.

Output is the following (I expect the is-equal column to be True in all rows):

                    apply  groupbyrolling  is-equal
gb      Date                                       
group_1 2020-01-01    1.0             1.0      True
        2020-01-02    1.5             1.5      True
        2020-01-03    2.0             2.0      True
        2020-01-04    2.5             2.5      True
        2020-01-05    3.0             6.0     False
        2020-01-06    3.5             6.5     False
group_2 2020-01-07    7.0             7.0      True
        2020-01-08    7.5             7.5      True
        2020-01-09    7.5             NaN     False
        2020-01-10    7.5             NaN     False

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-51-generic
Version : #56-Ubuntu SMP Mon Oct 5 14:28:49 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8

pandas : 1.1.3
numpy : 1.17.4
pytz : 2020.1
dateutil : 2.7.3
pip : 20.0.2
setuptools : 45.2.0
Cython : 0.29.21
pytest : 4.6.9
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.3.3
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.1
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 1.0.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.1.0
xlwt : None
numba : None

@behrenhoff behrenhoff added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 15, 2020
@phofl
Copy link
Member

phofl commented Oct 15, 2020

Hi, thanks for your report. This works on master.

df = pd.DataFrame(data={
    'Date': pd.date_range('2020-01-01', '2020-01-10'),
    'gb': ['group_1'] * 6 + ['group_2'] * 4,
    'value': range(10),
})

result = df.groupby('gb') \
    .rolling(6, on='Date', center=True, min_periods=1).value.mean()

returns

gb       Date      
group_1  2020-01-01    1.0
         2020-01-02    1.5
         2020-01-03    2.0
         2020-01-04    2.5
         2020-01-05    3.0
         2020-01-06    3.5
group_2  2020-01-07    7.0
         2020-01-08    7.5
         2020-01-09    7.5
         2020-01-10    7.5
Name: value, dtype: float64

@behrenhoff
Copy link
Contributor Author

For reference (release notes maybe?), I have tested a few more versions:
0.25.x OK
1.0.x. OK
1.1.0, 1.1.1, 1.1.2, 1.1.3 BROKEN
master (2020-10-16) OK

So I can confirm it is working in master, thanks for adding the test!

@jreback
Copy link
Contributor

jreback commented Oct 16, 2020

@mroeschke hmm might be worth backporting the patch though not sure if easy to do this

@jreback jreback added this to the 1.2 milestone Oct 16, 2020
@jreback jreback added Window rolling, ewma, expanding and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants