@@ -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 :
@@ -2465,41 +2468,52 @@ default 'raise'
2465
2468
return v
2466
2469
2467
2470
if year is not None :
2471
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_Y
2468
2472
dts.year = validate(" year" , year)
2469
2473
if month is not None :
2474
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_M
2470
2475
dts.month = validate(" month" , month)
2471
2476
if day is not None :
2477
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_D
2472
2478
dts.day = validate(" day" , day)
2473
2479
if hour is not None :
2480
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_h
2474
2481
dts.hour = validate(" hour" , hour)
2475
2482
if minute is not None :
2483
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_m
2476
2484
dts.min = validate(" minute" , minute)
2477
2485
if second is not None :
2486
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_s
2478
2487
dts.sec = validate(" second" , second)
2479
2488
if microsecond is not None :
2489
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_ms
2480
2490
dts.us = validate(" microsecond" , microsecond)
2481
2491
if nanosecond is not None :
2492
+ rep_reso = NPY_DATETIMEUNIT.NPY_FR_ns
2482
2493
dts.ps = validate(" nanosecond" , nanosecond) * 1000
2483
2494
if tzinfo is not object :
2484
2495
tzobj = tzinfo
2496
+
2497
+ if rep_reso < self ._creso:
2498
+ rep_reso = self ._creso
2485
2499
2486
2500
# reconstruct & check bounds
2487
2501
if tzobj is None :
2488
2502
# We can avoid going through pydatetime paths, which is robust
2489
2503
# to datetimes outside of pydatetime range.
2490
2504
ts = _TSObject()
2491
2505
try :
2492
- ts.value = npy_datetimestruct_to_datetime(self ._creso , & dts)
2506
+ ts.value = npy_datetimestruct_to_datetime(rep_reso , & dts)
2493
2507
except OverflowError as err:
2494
2508
fmt = dts_to_iso_string(& dts)
2495
2509
raise OutOfBoundsDatetime(
2496
2510
f" Out of bounds timestamp: {fmt} with frequency '{self.unit}'"
2497
2511
) from err
2498
2512
ts.dts = dts
2499
- ts.creso = self ._creso
2513
+ ts.creso = rep_reso
2500
2514
ts.fold = fold
2501
2515
return create_timestamp_from_ts(
2502
- ts.value, dts, tzobj, fold, reso = self ._creso
2516
+ ts.value, dts, tzobj, fold, reso = rep_reso
2503
2517
)
2504
2518
2505
2519
elif tzobj is not None and treat_tz_as_pytz(tzobj):
@@ -2518,10 +2532,10 @@ default 'raise'
2518
2532
ts_input = datetime(** kwargs)
2519
2533
2520
2534
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
2522
2536
)
2523
2537
return create_timestamp_from_ts(
2524
- ts.value, dts, tzobj, fold, reso = self ._creso
2538
+ ts.value, dts, tzobj, fold, reso = rep_reso
2525
2539
)
2526
2540
2527
2541
def to_julian_date (self ) -> np.float64:
0 commit comments