-
[X] I have checked that this issue has not already been reported.
-
[X] 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:
-
22864
-
35219
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()
Comment From: simonjayhawkins
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
>>>
Comment From: Flix6x
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?
Comment From: mroeschke
Yeah this could be better documented. in the user docs. This has been the behavior for a while and probably unlikely to change. xref https://github.com/pandas-dev/pandas/issues/22864
Comment From: jbrockmendel
possibly closed by #61985?