Skip to content

BUG: groupby rolling apply giving strange index #49878

Open
@fakio

Description

@fakio

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import datetime

d = pd.DataFrame([
    (datetime.date(2022, 1, 1), 'A', 10),
    (datetime.date(2022, 1, 2), 'B', 20),
    (datetime.date(2022, 1, 3), 'A', 30),
    (datetime.date(2022, 1, 4), 'B', 40),
],
columns=['date','group','value'])

d['date'] = pd.to_datetime(d['date'])

def func(s):
    print(s)
    return s.index.max().day

d.set_index(d.date).groupby('group').rolling('7d').value.apply(func)

Issue Description

I'm trying to apply a custom function using rolling apply and using the date index of the window, but the index of the data passed to the apply function seems incorrect

output of above rolling apply:

group  date      
A      2022-01-01    1.0
       2022-01-03    2.0
B      2022-01-02    3.0
       2022-01-04    4.0
Name: value, dtype: float64

Expected Behavior

group  date      
A      2022-01-01    1.0
       2022-01-03    3.0
B      2022-01-02    2.0
       2022-01-04    4.0
Name: value, dtype: float64

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-52-generic
Version : #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : pt_BR.UTF-8

pandas : 1.5.2
numpy : 1.22.3
pytz : 2021.3
dateutil : 2.8.2
setuptools : 45.2.0
pip : 22.3.1
Cython : None
pytest : 4.6.9
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.0.1
pymysql : 1.0.2
psycopg2 : None
jinja2 : 3.1.2
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : 2022.11.0
gcsfs : None
matplotlib : 3.6.0
numba : 0.56.4
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.3.3
snappy : None
sqlalchemy : None
tables : 3.6.1
tabulate : 0.8.9
xarray : None
xlrd : 1.1.0
xlwt : 1.3.0
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugGroupbyWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions