-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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: Wrong Result when subtracting BusinessHour offset #50531
Comments
I could not reproduce the issue with the example given. I got INSTALLED VERSIONScommit : 8dab54d pandas : 1.5.2 |
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 |
With respect to arithmetic with
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 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()) |
Oh, that makes sense. Thank you so much for the thorough explanation! |
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
Issue Description
Subtracting
BusinessHour
offsets fromdatetime
returns wrong results if thedatetime
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
: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 returnTimestamp('2020-01-01 09:00:00')
and notTimestamp('2019-12-31 17:00:00')
Installed Versions
INSTALLED VERSIONS
commit : 8dab54d
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
The text was updated successfully, but these errors were encountered: