File tree Expand file tree Collapse file tree 1 file changed +23
-2
lines changed
pandas/_libs/src/vendored/numpy/datetime Expand file tree Collapse file tree 1 file changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -482,8 +482,29 @@ npy_datetime npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT base,
482
482
483
483
if (base == NPY_FR_ns ) {
484
484
int64_t nanoseconds ;
485
- PD_CHECK_OVERFLOW (
486
- scaleMicrosecondsToNanoseconds (microseconds , & nanoseconds ));
485
+
486
+ // GH-57150: handle near-minimum valid timestamps
487
+ const int overflow = scaleMicrosecondsToNanoseconds (microseconds , & nanoseconds );
488
+ if (overflow != 0 ) {
489
+ // scaling overflows for pd.Timestamp.min (1677-09-21 00:12:43.145224193)
490
+ const int64_t near_min_scaled_microseconds = -9223372036854776LL ;
491
+ if (microseconds == near_min_scaled_microseconds ) {
492
+ // reverse calculation to avoid overflow
493
+ const int near_min_nanoseconds = 193 ;
494
+ int64_t nanoseconds_adder ;
495
+ PD_CHECK_OVERFLOW (
496
+ checked_int64_sub (dts -> ps / 1000 , near_min_nanoseconds , & nanoseconds_adder ));
497
+
498
+ // pd.Timestamp.min
499
+ nanoseconds = NPY_MIN_INT64 + 1 ;
500
+ PD_CHECK_OVERFLOW (
501
+ checked_int64_add (nanoseconds , nanoseconds_adder , & nanoseconds ));
502
+
503
+ return nanoseconds ;
504
+ }
505
+ }
506
+
507
+ PD_CHECK_OVERFLOW (overflow );
487
508
PD_CHECK_OVERFLOW (
488
509
checked_int64_add (nanoseconds , dts -> ps / 1000 , & nanoseconds ));
489
510
You can’t perform that action at this time.
0 commit comments