Skip to content

BUG: Resampling a Series with a DST to 24 hours gives a different result depending on whether timedelta(hours=24) or "24H" was used. #35248

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

Open
2 of 3 tasks
Flix6x opened this issue Jul 12, 2020 · 3 comments
Labels
Docs Resample resample method

Comments

@Flix6x
Copy link
Contributor

Flix6x commented Jul 12, 2020

  • 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.


Code Sample, a copy-pastable example

>>> import pandas as pd
>>> from datetime import timedelta

>>> # works as expected given a DST (second index is 24 hours after the first index)
>>> pd.Series(1, index=pd.date_range("2020-03-29", "2020-03-30 01:00", freq="H", tz="Europe/Amsterdam")).resample("24H").asfreq()

2020-03-29 00:00:00+01:00    1
2020-03-30 01:00:00+02:00    1
Freq: 24H, dtype: int64

>>> # works, but not as expected (second index is 23 hours after the first index)
>>> pd.Series(1, index=pd.date_range("2020-03-29", "2020-03-30 01:00", freq="H", tz="Europe/Amsterdam")).resample(timedelta(hours=24)).asfreq()

2020-03-29 00:00:00+01:00    1
2020-03-30 00:00:00+02:00    1
Freq: D, dtype: int64

Problem description

Resampling to a timedelta of 24 hours gives a result as if it's resampling to a timedelta of 1 calendar day. Using a freq_str of 24H does give the expected behaviour.

Possibly related issues:

Expected Output

>>> pd.Series(1, index=pd.date_range("2020-03-29", "2020-03-30 01:00", freq="H", tz="Europe/Amsterdam")).resample(timedelta(hours=24)).asfreq()

2020-03-29 00:00:00+01:00    1
2020-03-30 01:00:00+02:00    1
Freq: 24H, dtype: int64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Linux
OS-release : 4.9.0-11-amd64
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.0.5
numpy : 1.19.0
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.3.1.post20200622
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@Flix6x Flix6x added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 12, 2020
@simonjayhawkins
Copy link
Member

Resampling to a timedelta of 24 hours gives a result as if it's resampling to a timedelta of 1 calendar day.

from https://docs.python.org/3/library/datetime.html#timedelta-objects

Only days, seconds and microseconds are stored internally. Arguments are converted to those units

so timedelta(hours=24) is equivalent to timedelta(days=1)

>>> datetime.timedelta(hours=24)
datetime.timedelta(days=1)

and gives the same result as resample("1D")

>>>
>>> pd.Series(
...     1,
...     index=pd.date_range(
...         "2020-03-29", "2020-03-30 01:00", freq="H", tz="Europe/Amsterdam"
...     ),
... ).resample("1D").asfreq()
2020-03-29 00:00:00+01:00    1
2020-03-30 00:00:00+02:00    1
Freq: D, dtype: int64
>>>

@Flix6x
Copy link
Contributor Author

Flix6x commented Jul 12, 2020

Thanks, that explains it. To me then the interpretation of a datetime.timedelta(days=1), or equivalently a datetime.timedelta(hours=24) as a calendar day seems odd. If a datetime.timedelta day is defined as 24 hours long, shouldn't it be interpreted as such?

@mroeschke
Copy link
Member

Yeah this could be better documented. in the user docs. This has been the behavior for a while and probably unlikely to change. xref #22864

@mroeschke mroeschke added Docs Resample resample method and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Resample resample method
Projects
None yet
Development

No branches or pull requests

3 participants