Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
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
28 changes: 25 additions & 3 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
)
from pandas.core.dtypes.missing import isna

from pandas import Timedelta
from pandas.core.arrays import datetimelike as dtl
from pandas.core.arrays._ranges import generate_regular_range
import pandas.core.common as com
Expand Down Expand Up @@ -93,7 +94,6 @@

from pandas import (
DataFrame,
Timedelta,
)
from pandas.core.arrays import PeriodArray

Expand Down Expand Up @@ -817,12 +817,34 @@ 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",
]
res_unit = self.unit
if hasattr(offset, "offset"):
offset_td = Timedelta(offset.offset)
offset_unit = offset_td.unit
if self.unit in units and offset_unit in units:
Copy link
Member

Choose a reason for hiding this comment

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

is there ever False?

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

Copy link
Member

Choose a reason for hiding this comment

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

Then the check is not necessary

idx_self = units.index(self.unit)
idx_offset = units.index(offset_unit)
res_unit = units[min(idx_self, idx_offset)]
dtype_naive = np.dtype(f"datetime64[{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.

instead of defining dtype_naive and doing astype, could you do .as_unit(res_unit) after simple_new?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks!

if res_values.dtype != dtype_naive:
res_values = res_values.astype(dtype_naive)
result = type(self)._simple_new(res_values, dtype=dtype_naive)

if offset.normalize:
result = result.normalize()
result._freq = None

if self.tz is not None:
if (
self.tz is not None
and getattr(result.dtype, "tz", None) is None
and res_unit == "ns"
):
Copy link
Member

Choose a reason for hiding this comment

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

why does this codnition need tom change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it prevents errors by making sure we only localize when it's valid, please correct me

Copy link
Member

Choose a reason for hiding this comment

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

Well result.dtype.tz is always None, so that part of the check is unnecessary. And I suspect the u it part is just wrong.

result = result.tz_localize(self.tz)

return result
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"),
]
)
tm.assert_index_equal(result, expected)
Loading