Skip to content

Commit 9a8cb81

Browse files
fix: replace function now also replaces unit parameter if applicable
1 parent 73fd026 commit 9a8cb81

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

pandas/_libs/tslibs/timestamps.pyx

+19-5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ from pandas._libs.tslibs.conversion cimport (
5959
maybe_localize_tso,
6060
)
6161
from pandas._libs.tslibs.dtypes cimport (
62+
abbrev_to_npy_unit,
6263
npy_unit_to_abbrev,
6364
npy_unit_to_attrname,
6465
periods_per_day,
@@ -2439,10 +2440,12 @@ default 'raise'
24392440
datetime ts_input
24402441
tzinfo_type tzobj
24412442
_TSObject ts
2443+
NPY_DATETIMEUNIT rep_reso
24422444

24432445
# set to naive if needed
24442446
tzobj = self.tzinfo
24452447
value = self._value
2448+
rep_reso = self._creso
24462449

24472450
# GH 37610. Preserve fold when replacing.
24482451
if fold is None:
@@ -2465,41 +2468,52 @@ default 'raise'
24652468
return v
24662469

24672470
if year is not None:
2471+
rep_reso = NPY_DATETIMEUNIT.NPY_FR_Y
24682472
dts.year = validate("year", year)
24692473
if month is not None:
2474+
rep_reso = NPY_DATETIMEUNIT.NPY_FR_M
24702475
dts.month = validate("month", month)
24712476
if day is not None:
2477+
rep_reso = NPY_DATETIMEUNIT.NPY_FR_D
24722478
dts.day = validate("day", day)
24732479
if hour is not None:
2480+
rep_reso = NPY_DATETIMEUNIT.NPY_FR_h
24742481
dts.hour = validate("hour", hour)
24752482
if minute is not None:
2483+
rep_reso = NPY_DATETIMEUNIT.NPY_FR_m
24762484
dts.min = validate("minute", minute)
24772485
if second is not None:
2486+
rep_reso = NPY_DATETIMEUNIT.NPY_FR_s
24782487
dts.sec = validate("second", second)
24792488
if microsecond is not None:
2489+
rep_reso = NPY_DATETIMEUNIT.NPY_FR_ms
24802490
dts.us = validate("microsecond", microsecond)
24812491
if nanosecond is not None:
2492+
rep_reso = NPY_DATETIMEUNIT.NPY_FR_ns
24822493
dts.ps = validate("nanosecond", nanosecond) * 1000
24832494
if tzinfo is not object:
24842495
tzobj = tzinfo
2496+
2497+
if rep_reso < self._creso:
2498+
rep_reso = self._creso
24852499

24862500
# reconstruct & check bounds
24872501
if tzobj is None:
24882502
# We can avoid going through pydatetime paths, which is robust
24892503
# to datetimes outside of pydatetime range.
24902504
ts = _TSObject()
24912505
try:
2492-
ts.value = npy_datetimestruct_to_datetime(self._creso, &dts)
2506+
ts.value = npy_datetimestruct_to_datetime(rep_reso, &dts)
24932507
except OverflowError as err:
24942508
fmt = dts_to_iso_string(&dts)
24952509
raise OutOfBoundsDatetime(
24962510
f"Out of bounds timestamp: {fmt} with frequency '{self.unit}'"
24972511
) from err
24982512
ts.dts = dts
2499-
ts.creso = self._creso
2513+
ts.creso = rep_reso
25002514
ts.fold = fold
25012515
return create_timestamp_from_ts(
2502-
ts.value, dts, tzobj, fold, reso=self._creso
2516+
ts.value, dts, tzobj, fold, reso=rep_reso
25032517
)
25042518

25052519
elif tzobj is not None and treat_tz_as_pytz(tzobj):
@@ -2518,10 +2532,10 @@ default 'raise'
25182532
ts_input = datetime(**kwargs)
25192533

25202534
ts = convert_datetime_to_tsobject(
2521-
ts_input, tzobj, nanos=dts.ps // 1000, reso=self._creso
2535+
ts_input, tzobj, nanos=dts.ps // 1000, reso=rep_reso
25222536
)
25232537
return create_timestamp_from_ts(
2524-
ts.value, dts, tzobj, fold, reso=self._creso
2538+
ts.value, dts, tzobj, fold, reso=rep_reso
25252539
)
25262540

25272541
def to_julian_date(self) -> np.float64:

0 commit comments

Comments
 (0)