Skip to content

BUG: Addition/subtracting pd.Timedelta constructed from hour offset gives wrong results #62310

@christianfosli

Description

@christianfosli

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

from datetime import datetime, timedelta, timezone

import pandas as pd

dt = datetime(2025, 9, 10, 23, 0, 0, tzinfo=timezone.utc)

print(dt - pd.Timedelta(hours=1))  # 2025-09-10 22:00:00+00:00
print(dt - pd.Timedelta(pd.tseries.offsets.Hour(1)))  # 2025-09-10 23:00:00+00:00


exp_result = datetime(2025, 9, 10, 22, 0, 0, tzinfo=timezone.utc)

assert dt - timedelta(hours=1) == exp_result  # works
assert dt - pd.Timedelta(hours=1) == exp_result  # works
assert pd.Timedelta(pd.tseries.offsets.Hour(1)) == timedelta(hours=1)  # works
assert dt - pd.Timedelta(pd.tseries.offsets.Hour(1)) == exp_result  # assertion error

Issue Description

Adding/subtracting pd.Timedelta constructed from an hour offset gives the wrong result.
It works as expected when constructing the pd.Timedelta object using e.g. pd.Timedelta(hours=1) instead of pd.Timedelta(pd.tseries.offsets.Hour(1)).

I initially noticed this constructing a pd.Timedelta from an datetimeindex freq, e.g. pd.Timedelta(df.index.freq)

Expected Behavior

Adding/subtracting pd.Timedelta should give the same results whether the timedelta is constructed from an hour offset or using hours={int}.

Installed Versions

INSTALLED VERSIONS

commit : 4665c10
python : 3.12.3
python-bits : 64
OS : Linux
OS-release : 6.14.0-29-generic
Version : #29~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Aug 14 16:52:50 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 2.3.2
numpy : 2.3.2
pytz : 2025.2
dateutil : 2.9.0.post0
pip : None
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 21.0.0
pyreadstat : None
pytest : 8.4.1
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions