BUG: df.rolling does not work with 'datetime64[us]' index type · Issue #55299 · pandas-dev/pandas (original) (raw)

Pandas version checks

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