Pandas version checks
-
[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.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
from datetime import datetime
offset = pd.offsets.BusinessHour()
dt = datetime(2020, 1, 1, 10, 00)
dt - offset
# Expected Timestamp('2020-01-01 09:00:00') but got Timestamp('2019-12-31 17:00:00')
Issue Description
Subtracting BusinessHour
offsets from datetime
returns wrong results if the datetime
is not an opening time.
Rather than returning the hour before, it returns the opening time of the next day.
This seems to be caused by the following code in offsets.pyx BusinessHour._apply
:
if (
bhour_remain > bhour
or bhour_remain == bhour
and nanosecond != 0
):
According to the comments nanosecond != 0
serves to detect edge cases but incorrectly also detects this typical case.
Removing the nanosecond != 0
from the if condition fixes this issue, but causes several other tests (presumably those checking for edge cases) to fail.
Expected Behavior
datetime(2020, 1, 1, 10, 00) - BusinessHour()
should return Timestamp('2020-01-01 09:00:00')
and not Timestamp('2019-12-31 17:00:00')
Installed Versions
Comment From: dicristina
I could not reproduce the issue with the example given. I got Timestamp('2019-12-31 17:00:00')
in 1.5.2 and 1.5.0, as well as in master.
Comment From: DavidKleindienst
Oh, probably I copied result from a branch I was working on, sorry for that! I've changed the result in the post above to Timestamp('2019-12-31 17:00:00')
, thanks for pointing that mistake out.
Regardless of that, Timestamp('2019-12-31 17:00:00')
is still the wrong result, the correct result would be Timestamp('2020-01-01 9:00:00')
Comment From: dicristina
With respect to arithmetic with BusinessHour
offsets, the end time of a day, the start time of the following day and the times in between those two times are equivalent. According to the user guide:
Different from other offsets, BusinessHour.rollforward may output different results from apply by definition.
This is because one day’s business hour end is equal to next day’s business hour start. For example, under the default business hours (9:00 - 17:00), there is no gap (0 minutes) between 2014-08-01 17:00 and 2014-08-04 09:00.
Consider the following examples:
t1 = pd.Timestamp("2023-01-03T10:00") - pd.offsets.BusinessHour() # End time of 2023-01-02
t2 = pd.Timestamp("2023-01-02T16:00") + pd.offsets.BusinessHour() # Start time of 2023-01-03
t1, t2
# We can see that they were equivalent for our arithmetic
o = 2 * pd.offsets.BusinessHour()
t1 + o, t2 + o
This is counterintuitive because t1
and t2
are very different in everyday (calendar) usage. Here is another example:
timestamps = pd.DatetimeIndex(["2023-01-02T17:00", "2023-01-02T19:00", "2023-01-02T20:47:23", "2023-01-03T09:00"])
for t in timestamps:
print(f"Add 1H to {t}:", t + pd.offsets.BusinessHour())
Comment From: DavidKleindienst
Oh, that makes sense. Thank you so much for the thorough explanation! Everything working as intended, so I'm closing this.