Skip to content

BUG: rolling aggregations not checking monotonic index when grouping by #34617

Closed
@frd-glovo

Description

@frd-glovo
  • 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
import numpy as np

df = pd.DataFrame({'a':['2020-06-01 12:00',
                       '2020-06-01 12:30',
                       '2020-06-01 13:00',
                       '2020-06-01 14:00',
                       np.nan],
                   'b':[1,2,3,4,5],
                   'c':[1,1,1,1,1]})
df.a = pd.to_datetime(df.a)
df.set_index('a',inplace=True)
df.groupby('c').b.rolling('60min').sum() 

''' 

Note that if there's no group by, ie:
df.b.rolling('60min').sum() 
 index monotonicity is checked and an error is reported due to the NaT.
''''

Problem description

When using a rolling window on a timestamp index + a group by, there is no validation about the monotonicity of the index. This can lead to unexpected behaviours, as the value of the calculation can change if the index is not monotonic.

Expected Output

Output:
c a
1 2020-06-01 12:00:00 1.0
2020-06-01 12:30:00 3.0
2020-06-01 13:00:00 6.0
2020-06-01 14:00:00 10.0
NaT 15.0

Expected (correct) output:

c a
1 2020-06-01 12:00:00 1.0
2020-06-01 12:30:00 3.0
2020-06-01 13:00:00 5.0
2020-06-01 14:00:00 4.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.9.184-linuxkit
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.4
numpy : 1.18.0
pytz : 2019.1
dateutil : 2.7.5
pip : 19.3.1
setuptools : 41.4.0
Cython : None
pytest : 4.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : 0.9.3
psycopg2 : 2.8.2 (dt dec pq3 ext lo64)
jinja2 : 2.10.1
IPython : 7.11.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 4.1.1
pyxlsb : None
s3fs : None
scipy : 1.2.0
sqlalchemy : 1.2.16
tables : None
tabulate : 0.8.3
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugError ReportingIncorrect or improved errors from pandasMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions