Skip to content
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

BUG: .rolling() returns incorrect values when ts index is not nano seconds #55106

Closed
3 tasks done
Tracked by #5
haim0n opened this issue Sep 12, 2023 · 3 comments · Fixed by #55173
Closed
3 tasks done
Tracked by #5

BUG: .rolling() returns incorrect values when ts index is not nano seconds #55106

haim0n opened this issue Sep 12, 2023 · 3 comments · Fixed by #55173
Labels
Bug Non-Nano datetime64/timedelta64 with non-nanosecond resolution
Milestone

Comments

@haim0n
Copy link

haim0n commented Sep 12, 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

df_time = pd.DataFrame({'B': [0, 1, 2, 4, 5, 6]},
                       index=[pd.Timestamp('20130101 09:00:00'),
                              pd.Timestamp('20130101 09:00:02'),
                              pd.Timestamp('20130101 09:00:03'),
                              pd.Timestamp('20130101 09:00:06'),
                              pd.Timestamp('20130101 09:00:07'),
                              pd.Timestamp('20130101 09:00:08')])
sum1 = df_time.rolling('1s').sum()
# micro seconds / milliseconds breaks the correct rolling!
df_time.index = df_time.index.as_unit('us')
sum2 = df_time.rolling('1s').sum()

pd.testing.assert_frame_equal(sum1, sum2)

Issue Description

The rolling function returns incorrect values in case of the index isn't nano seconds.

Expected Behavior

Both sums should be identical

Installed Versions

INSTALLED VERSIONS

commit : 37ea63d
python : 3.10.9.final.0
python-bits : 64
OS : Linux
OS-release : 6.2.0-31-generic
Version : #31~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Aug 16 13:45:26 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_IL
LOCALE : en_IL.UTF-8
pandas : 2.0.1
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 57.5.0
pip : 22.3.1
Cython : 3.0.0
pytest : 7.3.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.1.2
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : 1.3.7
brotli : 1.0.9
fastparquet : 2023.8.0
fsspec : 2023.6.0
gcsfs : 2023.6.0
matplotlib : 3.7.2
numba : 0.57.1
numexpr : 2.8.4
odfpy : None
openpyxl : 3.1.2
pandas_gbq : 0.19.2
pyarrow : 12.0.1
pyreadstat : None
pyxlsb : 1.0.10
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : 3.8.0
tabulate : None
xarray : 2023.7.0
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

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

I tested this locally on 2.1.0 and can confirm it is a bug.

@haimjether
Copy link

Note that on main branch, the usecase should be changed to:

import pandas as pd

df_time = pd.DataFrame({'B': [0, 1, 2, 4, 5 ,6]},
                       index=[pd.Timestamp('20130101 09:00:00'),
                              pd.Timestamp('20130101 09:00:02'),
                              pd.Timestamp('20130101 09:00:03'),
                              pd.Timestamp('20130101 09:00:06'),
                              pd.Timestamp('20130101 09:00:07'),
                              pd.Timestamp('20130101 09:00:08')])
sum1 = df_time.rolling('1s').sum()
print(sum1)
# micro seconds / milliseconds breaks the correct rolling!
df_time.index = df_time.index.as_unit('us')
sum2 = df_time.rolling('1s').sum()
print(sum2)

assert sum1.values == sum2.values

@jorisvandenbossche jorisvandenbossche added this to the 2.1.1 milestone Sep 15, 2023
@jorisvandenbossche jorisvandenbossche added Non-Nano datetime64/timedelta64 with non-nanosecond resolution and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 15, 2023
@hkhojasteh
Copy link
Contributor

I'm going to fix it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Non-Nano datetime64/timedelta64 with non-nanosecond resolution
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants