Skip to content
Prev Previous commit
Next Next commit
fix near-minimum timestamp overflow when scaling from microseconds to…
… nanoseconds
  • Loading branch information
Robert Schmidtke committed Feb 16, 2024
commit 58bc627994196ebf82e1b21bcdd1750d72d797bd
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed memory leak in :func:`read_csv` (:issue:`57039`)
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression causing overflow for near-minimum timestamps (:issue:`57150`)
- Fixed regression in :func:`concat` changing long-standing behavior that always sorted the non-concatenation axis when the axis was a :class:`DatetimeIndex` (:issue:`57006`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :func:`pandas.testing.assert_series_equal` defaulting to ``check_exact=True`` when checking the :class:`Index` (:issue:`57067`)
Expand Down
11 changes: 11 additions & 0 deletions pandas/_libs/src/vendored/numpy/datetime/np_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,17 @@ npy_datetime npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT base,
}

if (base == NPY_FR_ns) {
// for near-minimum timestamps, scaling microseconds to nanoseconds
// overflows but adding nanoseconds puts the timestamp back in a valid range
const int64_t min_nanoseconds = NPY_MIN_INT64 + 1;
if (microseconds == min_nanoseconds / 1000 - 1) {
// calculate final nanoseconds from minimum without scaling microseconds
int64_t nanoseconds = min_nanoseconds;
PD_CHECK_OVERFLOW(checked_int64_add(
nanoseconds, (dts->ps - _NS_MIN_DTS.ps) / 1000, &nanoseconds));
return nanoseconds;
}

int64_t nanoseconds;
PD_CHECK_OVERFLOW(
scaleMicrosecondsToNanoseconds(microseconds, &nanoseconds));
Expand Down