Skip to content

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

Closed
@behrenhoff

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Windowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions