@@ -59,6 +59,7 @@ from pandas._libs.tslibs.conversion cimport (
59
59
maybe_localize_tso,
60
60
)
61
61
from pandas._libs.tslibs.dtypes cimport (
62
+ abbrev_to_npy_unit,
62
63
npy_unit_to_abbrev,
63
64
npy_unit_to_attrname,
64
65
periods_per_day,
@@ -2439,10 +2440,12 @@ default 'raise'
2439
2440
datetime ts_input
2440
2441
tzinfo_type tzobj
2441
2442
_TSObject ts
2443
+ NPY_DATETIMEUNIT rep_reso
2442
2444
2443
2445
# set to naive if needed
2444
2446
tzobj = self .tzinfo
2445
2447
value = self ._value
2448
+ rep_reso = self ._creso
2446
2449
2447
2450
# GH 37610. Preserve fold when replacing.
2448
2451
if fold is None :
@@ -2466,40 +2469,54 @@ default 'raise'
2466
2469
2467
2470
if year is not None :
2468
2471
dts.year = validate(" year" , year)
2472
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_Y
2469
2473
if month is not None :
2470
2474
dts.month = validate(" month" , month)
2475
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_M
2471
2476
if day is not None :
2472
2477
dts.day = validate(" day" , day)
2478
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_D
2473
2479
if hour is not None :
2474
2480
dts.hour = validate(" hour" , hour)
2481
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_h
2475
2482
if minute is not None :
2476
2483
dts.min = validate(" minute" , minute)
2484
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_m
2477
2485
if second is not None :
2478
2486
dts.sec = validate(" second" , second)
2487
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_s
2479
2488
if microsecond is not None :
2480
2489
dts.us = validate(" microsecond" , microsecond)
2490
+ if microsecond > 999 :
2491
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_us
2492
+ else :
2493
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_ms
2481
2494
if nanosecond is not None :
2482
2495
dts.ps = validate(" nanosecond" , nanosecond) * 1000
2496
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_ns
2483
2497
if tzinfo is not object :
2484
2498
tzobj = tzinfo
2499
+
2500
+ if rep_reso < self ._creso:
2501
+ rep_reso = self ._creso
2485
2502
2486
2503
# reconstruct & check bounds
2487
2504
if tzobj is None :
2488
2505
# We can avoid going through pydatetime paths, which is robust
2489
2506
# to datetimes outside of pydatetime range.
2490
2507
ts = _TSObject()
2491
2508
try :
2492
- ts.value = npy_datetimestruct_to_datetime(self ._creso , & dts)
2509
+ ts.value = npy_datetimestruct_to_datetime(rep_reso , & dts)
2493
2510
except OverflowError as err:
2494
2511
fmt = dts_to_iso_string(& dts)
2495
2512
raise OutOfBoundsDatetime(
2496
2513
f" Out of bounds timestamp: {fmt} with frequency '{self.unit}'"
2497
2514
) from err
2498
2515
ts.dts = dts
2499
- ts.creso = self ._creso
2516
+ ts.creso = rep_reso
2500
2517
ts.fold = fold
2501
2518
return create_timestamp_from_ts(
2502
- ts.value, dts, tzobj, fold, reso = self ._creso
2519
+ ts.value, dts, tzobj, fold, reso = rep_reso
2503
2520
)
2504
2521
2505
2522
elif tzobj is not None and treat_tz_as_pytz(tzobj):
@@ -2518,10 +2535,10 @@ default 'raise'
2518
2535
ts_input = datetime(** kwargs)
2519
2536
2520
2537
ts = convert_datetime_to_tsobject(
2521
- ts_input, tzobj, nanos = dts.ps // 1000 , reso = self ._creso
2538
+ ts_input, tzobj, nanos = dts.ps // 1000 , reso = rep_reso
2522
2539
)
2523
2540
return create_timestamp_from_ts(
2524
- ts.value, dts, tzobj, fold, reso = self ._creso
2541
+ ts.value, dts, tzobj, fold, reso = rep_reso
2525
2542
)
2526
2543
2527
2544
def to_julian_date (self ) -> np.float64:
0 commit comments