Skip to content

Commit 7697711

Browse files
committed
BUG: Fix dt64[non_nano] + offset rounding
1 parent e54c162 commit 7697711

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

pandas/core/arrays/datetimes.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,10 +826,22 @@ def _add_offset(self, offset: BaseOffset) -> Self:
826826
res_unit = self.unit
827827
if hasattr(offset, "offset"):
828828
offset_td = Timedelta(offset.offset)
829-
offset_unit = offset_td.unit
829+
ns = abs(int(offset_td.value)) # Timedelta.value is in nanoseconds
830+
if ns == 0:
831+
eff_unit = self.unit
832+
elif ns % 1_000_000_000 == 0:
833+
eff_unit = "s"
834+
elif ns % 1_000_000 == 0:
835+
eff_unit = "ms"
836+
elif ns % 1_000 == 0:
837+
eff_unit = "us"
838+
else:
839+
eff_unit = "ns"
840+
830841
idx_self = units.index(self.unit)
831-
idx_offset = units.index(offset_unit)
832-
res_unit = units[min(idx_self, idx_offset)]
842+
idx_eff = units.index(eff_unit)
843+
res_unit = units[min(idx_self, idx_eff)]
844+
833845
result = type(self)._simple_new(res_values, dtype=res_values.dtype)
834846
result = result.as_unit(res_unit)
835847

0 commit comments

Comments
 (0)