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