Skip to content
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

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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 @@ -346,6 +346,7 @@ Bug fixes
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
- Fixed bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
- Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` inconsistently replacing matching instances when ``regex=True`` and missing values are present. (:issue:`56599`)
- Fixed bug in :meth:`Timestamp.replace` where it would not reflect changes into :meth:`Timestamp.unit`. (:issue:`57749`)
Copy link
Member

Choose a reason for hiding this comment

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

This should be in the Datetimelike section


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 @@ -2439,10 +2439,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 @@ -2466,40 +2468,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 @@ -2518,10 +2534,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
10 changes: 10 additions & 0 deletions pandas/tests/scalar/timestamp/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,13 @@ 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=ts1.microsecond)
assert ts == ts1
ts = ts.replace(nanosecond=ts2.nanosecond)
assert ts == ts2
diogomsmiranda marked this conversation as resolved.
Show resolved Hide resolved