Skip to content

BUG: groupby().rolling("freq") validation raises even if dates are sorted within each group #46061

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
3 tasks done
JILPulvino opened this issue Feb 18, 2022 · 4 comments · Fixed by #46065 or #46567
Closed
3 tasks done
Labels
Bug Groupby Regression Functionality that used to work in a prior pandas version Window rolling, ewma, expanding
Milestone

Comments

@JILPulvino
Copy link

JILPulvino commented Feb 18, 2022

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
print(pd.__version__)
test_df = pd.DataFrame({
    'Name':['L', 'L', 'L',
                    'M', 'M', 'M', 'M', 'M', 'M'],
    'ID': ['L1', 'L2', 'L3',
              'M1', 'M2', 'M3', 'M4', 'M5', 'M6'],
    'Date': [
        '2005-01-01', '2006-01-01', '2013-01-01', 
        '2005-01-01', '2006-01-01', '2006-01-01', '2017-01-01', '2006-02-01', '2006-03-01'],
    'indicator': [0, 1, 0,
                          0, 1, 0, 0, 1, 0],
    'rolling_sum':[0, 0, 1,
                              0, 0, 0, 0, 1, 2]})
test_df['Date'] = pd.to_datetime(test_df['Date'], errors='coerce')
test_df.groupby('Name').rolling('731D', on='Date')['indicator'].sum().reset_index()

Issue Description

Grouping by a column and then rolling over the grouped dataframe results in [ValueError: Date must be monotonic](). Ultimately, the operation should be able to produce the rolling_sum column in the example.

Expected Behavior

This is not an issue in pandas 1.3.5 but is an issue in 1.4 and higher. I expect this function to create a grouped dataframe and to then perform the rolling summation on the provided column using the date window provided.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.8.12.final.0
python-bits : 64
OS : Darwin
OS-release : 21.1.0
Version : Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:24 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T8101
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.4.1
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 60.6.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@JILPulvino JILPulvino added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 18, 2022
@phofl
Copy link
Member

phofl commented Feb 18, 2022

Hi, thanks for your report. Could you please trim your example down a bit? It should not contain more values than strictly necessary.

@mroeschke
Copy link
Member

Thanks for the report.

This behavior was a bug fix in 1.4 as seen in https://pandas.pydata.org/docs/whatsnew/v1.4.0.html#groupby-resample-rolling

Bug in Groupby.rolling() when non-monotonic data passed, fails to correctly raise ValueError (GH43909)

But even if the dates are sorted with the groups this error still raised which seems like a bug

In [2]: test_df.sort_values(["Name", "Date"]).groupby('Name').rolling('2000D', on='Date')
ValueError: Date must be monotonic

@mroeschke mroeschke added Groupby Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 18, 2022
@mroeschke mroeschke changed the title BUG: rolling with groupby gives 'date must be monotonic' error BUG: groupby().rolling("freq") validation raises even if dates are sorted within each group Feb 18, 2022
@simonjayhawkins
Copy link
Member

#46065 reverted, #46078

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Feb 20, 2022
@simonjayhawkins
Copy link
Member

simonjayhawkins commented Feb 20, 2022

But even if the dates are sorted with the groups this error still raised which seems like a bug

since this worked (for the sorted groups case) in 1.3.5, labelling as a regression.

first bad commit: [b1827ea] BUG: 43909 - check monoticity of rolling groupby (#44068)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Regression Functionality that used to work in a prior pandas version Window rolling, ewma, expanding
Projects
None yet
4 participants