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

INSTALLED VERSIONS ------------------ commit : 8dab54d6573f7186ff0c3b6364d5e4dd635ff3e7 python : 3.10.8.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19045 machine : AMD64 processor : Intel64 Family 6 Model 167 Stepping 1, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : German_Austria.1252 pandas : 1.5.2 numpy : 1.23.5 pytz : 2022.7 dateutil : 2.8.2 setuptools : 65.5.0 pip : 22.3.1 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 : 1.3.5 brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : 2.8.4 odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None tzdata : None

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.

INSTALLED VERSIONS ------------------ commit : 8dab54d6573f7186ff0c3b6364d5e4dd635ff3e7 python : 3.10.6.final.0 python-bits : 64 OS : Linux OS-release : 5.15.0-25-generic Version : #25-Ubuntu SMP Wed Mar 30 15:54:22 UTC 2022 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 1.5.2 numpy : 1.24.1 pytz : 2022.7 dateutil : 2.8.2 setuptools : 59.6.0 pip : 22.0.2 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 : 8.7.0 pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None tzdata : None

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.