Skip to content

BUG: df.rolling does not work with 'datetime64[us]' index type #55299

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
figuetbe opened this issue Sep 26, 2023 · 4 comments
Closed
3 tasks done

BUG: df.rolling does not work with 'datetime64[us]' index type #55299

figuetbe opened this issue Sep 26, 2023 · 4 comments
Assignees
Labels
Bug Window rolling, ewma, expanding

Comments

@figuetbe
Copy link

figuetbe commented Sep 26, 2023

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
index = ['2019-01-03 05:11', '2019-01-03 05:23',
               '2019-01-03 05:28', '2019-01-03 05:32',
               '2019-01-03 05:36']
arr= [True, False, False, True, True]
df = pd.DataFrame({'arr': arr}, index=pd.to_datetime(index).astype('datetime64[us]'))
print(df.rolling('5min').sum())

                    arr
2019-01-03 05:11:00  1.0
2019-01-03 05:23:00  1.0
2019-01-03 05:28:00  1.0
2019-01-03 05:32:00  2.0
2019-01-03 05:36:00  3.0
df = pd.DataFrame({'arr': arr}, index=pd.to_datetime(index).astype('datetime64[ms]'))
print(df.rolling('5min').sum())

                    arr
2019-01-03 05:11:00  1.0
2019-01-03 05:23:00  1.0
2019-01-03 05:28:00  1.0
2019-01-03 05:32:00  2.0
2019-01-03 05:36:00  3.0

Issue Description

Output of rolling window operations for timeseries such as sum or count is wrong when the index is of type is not 'datetime64[ns]'.

Expected Behavior

df = pd.DataFrame({'arr': arr}, index=pd.to_datetime(index).astype('datetime64[ns]'))
print(df.rolling('5min').sum())

                     arr
2019-01-03 05:11:00  1.0
2019-01-03 05:23:00  0.0
2019-01-03 05:28:00  0.0
2019-01-03 05:32:00  1.0
2019-01-03 05:36:00  2.0

Installed Versions

INSTALLED VERSIONS

commit : e86ed37
python : 3.10.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.120+
Version : #1 SMP Wed Aug 30 11:19:59 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.1
numpy : 1.23.5
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : 3.0.2
pytest : 7.4.1
hypothesis : None
sphinx : 5.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : 1.1
pymysql : None
psycopg2 : 2.9.7
jinja2 : 3.1.2
IPython : 7.34.0
pandas_datareader : 0.10.0
bs4 : 4.11.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : 2023.6.0
gcsfs : 2023.6.0
matplotlib : 3.7.1
numba : 0.56.4
numexpr : 2.8.5
odfpy : None
openpyxl : 3.1.2
pandas_gbq : 0.17.9
pyarrow : 9.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.2
sqlalchemy : 2.0.20
tables : 3.8.0
tabulate : 0.9.0
xarray : 2023.7.0
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@figuetbe figuetbe added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 26, 2023
@paulreece
Copy link
Contributor

paulreece commented Sep 27, 2023

I can confirm that the bug exists on the main development branch of pandas.

Would you mind updating your test case to use 5min in rolling instead of 5T since T is being deprecated?

It would also be helpful if you added the current output for the datetime64[us] version:

>>> print(df.rolling('5min').sum())
                     arr
2019-01-03 05:11:00  1.0
2019-01-03 05:23:00  1.0
2019-01-03 05:28:00  1.0
2019-01-03 05:32:00  2.0
2019-01-03 05:36:00  3.0

@paulreece paulreece added Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 27, 2023
@banorton
Copy link
Contributor

take

@banorton
Copy link
Contributor

I've reproduced this locally and have a fix. I'll submit a PR after some cleanup and testing.

@mroeschke
Copy link
Member

Thanks for the report. Closing as duplicate of #55106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants