Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,16 @@ def _add_offset(self, offset: BaseOffset) -> Self:
result = type(self)._from_sequence(res_values, dtype=self.dtype)

else:
result = type(self)._simple_new(res_values, dtype=res_values.dtype)
units = ["ns", "us", "ms", "s", "m", "h", "D"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are m, h, D possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm wrong I will update this .

res_unit = self.unit
if hasattr(offset, "offset"):
offset_unit = Timedelta(offset.offset).unit
idx_self = units.index(self.unit)
idx_offset = units.index(offset_unit)
res_unit = units[min(idx_self, idx_offset)]
dtype = tz_to_dtype(self.tz, unit=res_unit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wont you need to cast res_values before calling simple_new?

Copy link
Contributor Author

@Aniketsy Aniketsy Oct 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, thanks! . Should we wrap this in try/except block?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i suspect that The Right Way to do this would be to find the correct dtype before calling _from_sequence on L817 and pass it there

result = type(self)._simple_new(res_values, dtype=dtype)

if offset.normalize:
result = result.normalize()
result._freq = None
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,3 +844,20 @@ def test_factorize_sort_without_freq():
tda = dta - dta[0]
with pytest.raises(NotImplementedError, match=msg):
tda.factorize(sort=True)


def test_dt64_non_nano_offset_no_rounding():
# GH#56586
dti = pd.date_range("2016-01-01", periods=3, unit="s")
offset = pd.offsets.CustomBusinessDay(offset=pd.Timedelta("1ms"))
result = dti + offset

assert result.dtype == np.dtype("datetime64[ms]")
expected = pd.DatetimeIndex(
[
pd.Timestamp("2016-01-02 00:00:00.001"),
pd.Timestamp("2016-01-03 00:00:00.001"),
pd.Timestamp("2016-01-04 00:00:00.001"),
]
)
assert all(result == expected)
Loading