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