Skip to content

Commit f8e31d4

Browse files
committed
BUG: Fix dt64[non_nano] + offset rounding
1 parent 0f088f5 commit f8e31d4

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

pandas/core/arrays/datetimes.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
)
6565
from pandas.core.dtypes.missing import isna
6666

67+
from pandas import Timedelta
6768
from pandas.core.arrays import datetimelike as dtl
6869
from pandas.core.arrays._ranges import generate_regular_range
6970
import pandas.core.common as com
@@ -93,7 +94,6 @@
9394

9495
from pandas import (
9596
DataFrame,
96-
Timedelta,
9797
)
9898
from pandas.core.arrays import PeriodArray
9999

@@ -824,27 +824,27 @@ def _add_offset(self, offset: BaseOffset) -> Self:
824824
"s",
825825
]
826826
res_unit = self.unit
827-
# Only try to adjust unit if both units are recognized
828-
try:
829-
if hasattr(offset, "offset"):
830-
offset_td = Timedelta(offset.offset)
831-
offset_unit = offset_td.unit
832-
if self.unit in units and offset_unit in units:
833-
idx_self = units.index(self.unit)
834-
idx_offset = units.index(offset_unit)
835-
res_unit = units[min(idx_self, idx_offset)]
836-
except Exception:
837-
res_unit = self.unit
838-
dtype = tz_to_dtype(self.tz, unit=res_unit)
839-
if res_values.dtype != f"datetime64[{res_unit}]":
840-
res_values = res_values.astype(f"datetime64[{res_unit}]")
841-
result = type(self)._simple_new(res_values, dtype=dtype)
827+
if hasattr(offset, "offset"):
828+
offset_td = Timedelta(offset.offset)
829+
offset_unit = offset_td.unit
830+
if self.unit in units and offset_unit in units:
831+
idx_self = units.index(self.unit)
832+
idx_offset = units.index(offset_unit)
833+
res_unit = units[min(idx_self, idx_offset)]
834+
dtype_naive = np.dtype(f"datetime64[{res_unit}]")
835+
if res_values.dtype != dtype_naive:
836+
res_values = res_values.astype(dtype_naive)
837+
result = type(self)._simple_new(res_values, dtype=dtype_naive)
842838

843839
if offset.normalize:
844840
result = result.normalize()
845841
result._freq = None
846842

847-
if self.tz is not None:
843+
if (
844+
self.tz is not None
845+
and getattr(result.dtype, "tz", None) is None
846+
and res_unit == "ns"
847+
):
848848
result = result.tz_localize(self.tz)
849849

850850
return result

0 commit comments

Comments
 (0)