Skip to content

BUG: Timestamp.unit now reflects changes in components after Timestamp.replace #58121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ Performance improvements

Bug fixes
~~~~~~~~~
- Fixed bug in :meth:`Timestamp.replace` where it would not reflect changes into :meth:`Timestamp.unit`. (:issue:`57749`)

Categorical
^^^^^^^^^^^
Expand Down
26 changes: 21 additions & 5 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2615,10 +2615,12 @@ default 'raise'
datetime ts_input
tzinfo_type tzobj
_TSObject ts
NPY_DATETIMEUNIT rep_reso

# set to naive if needed
tzobj = self.tzinfo
value = self._value
rep_reso = self._creso

# GH 37610. Preserve fold when replacing.
if fold is None:
Expand All @@ -2642,40 +2644,54 @@ default 'raise'

if year is not None:
dts.year = validate("year", year)
rep_reso = NPY_DATETIMEUNIT.NPY_FR_Y
if month is not None:
dts.month = validate("month", month)
rep_reso = NPY_DATETIMEUNIT.NPY_FR_M
if day is not None:
dts.day = validate("day", day)
rep_reso = NPY_DATETIMEUNIT.NPY_FR_D
if hour is not None:
dts.hour = validate("hour", hour)
rep_reso = NPY_DATETIMEUNIT.NPY_FR_h
if minute is not None:
dts.min = validate("minute", minute)
rep_reso = NPY_DATETIMEUNIT.NPY_FR_m
if second is not None:
dts.sec = validate("second", second)
rep_reso = NPY_DATETIMEUNIT.NPY_FR_s
if microsecond is not None:
dts.us = validate("microsecond", microsecond)
if microsecond > 999:
rep_reso = NPY_DATETIMEUNIT.NPY_FR_us
else:
rep_reso = NPY_DATETIMEUNIT.NPY_FR_ms
if nanosecond is not None:
dts.ps = validate("nanosecond", nanosecond) * 1000
rep_reso = NPY_DATETIMEUNIT.NPY_FR_ns
if tzinfo is not object:
tzobj = tzinfo

if rep_reso < self._creso:
rep_reso = self._creso

# reconstruct & check bounds
if tzobj is None:
# We can avoid going through pydatetime paths, which is robust
# to datetimes outside of pydatetime range.
ts = _TSObject()
try:
ts.value = npy_datetimestruct_to_datetime(self._creso, &dts)
ts.value = npy_datetimestruct_to_datetime(rep_reso, &dts)
except OverflowError as err:
fmt = dts_to_iso_string(&dts)
raise OutOfBoundsDatetime(
f"Out of bounds timestamp: {fmt} with frequency '{self.unit}'"
) from err
ts.dts = dts
ts.creso = self._creso
ts.creso = rep_reso
ts.fold = fold
return create_timestamp_from_ts(
ts.value, dts, tzobj, fold, reso=self._creso
ts.value, dts, tzobj, fold, reso=rep_reso
)

elif tzobj is not None and treat_tz_as_pytz(tzobj):
Expand All @@ -2694,10 +2710,10 @@ default 'raise'
ts_input = datetime(**kwargs)

ts = convert_datetime_to_tsobject(
ts_input, tzobj, nanos=dts.ps // 1000, reso=self._creso
ts_input, tzobj, nanos=dts.ps // 1000, reso=rep_reso
)
return create_timestamp_from_ts(
ts.value, dts, tzobj, fold, reso=self._creso
ts.value, dts, tzobj, fold, reso=rep_reso
)

def to_julian_date(self) -> np.float64:
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/scalar/timestamp/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,17 @@ def test_replace_preserves_fold(self, fold):
ts_replaced = ts.replace(second=1)

assert ts_replaced.fold == fold

def test_replace_unit(self):
# GH#57749
ts = Timestamp("2023-07-15 23:08:12")
ts1 = Timestamp("2023-07-15 23:08:12.134567")
ts2 = Timestamp("2023-07-15 23:08:12.134567123")
ts = ts.replace(microsecond=999)
assert ts.unit == "ms"
ts = ts.replace(microsecond=ts1.microsecond)
assert ts.unit == "us"
assert ts == ts1
ts = ts.replace(nanosecond=ts2.nanosecond)
assert ts.unit == "ns"
assert ts == ts2
Loading