Skip to content

BUG: fix float-to-int64 OOB handling on ARM in to_datetime/to_timedelta#64643

Merged
mroeschke merged 2 commits intopandas-dev:mainfrom
jbrockmendel:fix-arm-float-to-int64-oob
Mar 20, 2026
Merged

BUG: fix float-to-int64 OOB handling on ARM in to_datetime/to_timedelta#64643
mroeschke merged 2 commits intopandas-dev:mainfrom
jbrockmendel:fix-arm-float-to-int64-oob

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

Summary

Follow-up to #64619. On ARM, float-to-int64 overflow saturates to INT64_MAX instead of wrapping to INT64_MIN (as on x86). This caused the integer shortcut in _to_datetime_with_unit and sequence_to_td64ns to misfire for OOB float values like float(2**63), because np.float64(2**63) == np.int64(INT64_MAX) evaluates to True on ARM (INT64_MAX rounds up to 2**63 in float64).

#64619 fixed the datetime case with a separate OOB pre-check in Python, but left a latent bug in the timedelta path for non-ns units. This PR replaces the pre-check with a tighter fix: an in_int64_range guard on the shortcut condition itself, applied consistently to both paths.

Changes:

  • datetimes.py: Replace the 12-line OOB pre-check with a 4-line in_int64_range guard on the integer shortcut condition. OOB values now fall through to cast_from_unit_vectorized which already has its own OOB check, and the existing try/except handles the errors parameter. Removes the now-unused iNaT import.
  • timedeltas.py: Add the same in_int64_range guard to the timedelta integer shortcut, fixing the latent ARM bug for non-ns units.
  • test_to_timedelta.py: Add test_float_to_timedelta_raise_oob_non_ns covering the timedelta shortcut path with unit="s".

Test plan

  • Existing test_float_to_datetime_raise_oob_ns passes
  • Existing test_float_to_timedelta_raise_oob_ns passes
  • New test_float_to_timedelta_raise_oob_non_ns passes
  • Full test_to_datetime.py and test_to_timedelta.py suites pass (1024 passed)
  • ARM CI (ubuntu-24.04-arm, macos-15) passes

🤖 Generated with Claude Code

On ARM, float-to-int64 overflow saturates to INT64_MAX instead of
wrapping to INT64_MIN (as on x86). This caused the integer shortcut
in _to_datetime_with_unit and sequence_to_td64ns to misfire for OOB
float values like float(2**63), silently producing incorrect results.

Fix by adding an in_int64_range guard to the shortcut condition,
replacing the separate OOB pre-check added in GH#64619. This also
removes duplicated OOB logic between Python and Cython, and fixes a
latent bug in the timedelta path for non-ns units.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jbrockmendel jbrockmendel marked this pull request as ready for review March 19, 2026 23:45
@jbrockmendel jbrockmendel added Bug Datetime Datetime data dtype labels Mar 20, 2026
Comment thread pandas/core/arrays/timedeltas.py Outdated
# instead of wrapping, which makes the data == int_data
# check pass incorrectly for OOB values like float(2**63).
# Exclude values outside the int64 domain from the check.
in_int64_range = (data >= np.float64(-(2**63))) & (data < np.float64(2**63))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use np.float64(np.iinfo(np.int64).max/min) instead of hardcoding the 2**63?

Comment thread pandas/core/tools/datetimes.py Outdated
# instead of wrapping, which makes the arg == int_values
# check pass incorrectly for OOB values like float(2**63).
# Exclude values outside the int64 domain from the check.
in_int64_range = (arg >= np.float64(-(2**63))) & (arg < np.float64(2**63))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (and ideally the test)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated here, forgot to do it in the test. for worktree-related reasons updating again is a bit inconvenient ATM

…e check

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mroeschke mroeschke added this to the 3.1 milestone Mar 20, 2026
@mroeschke mroeschke merged commit 78760ed into pandas-dev:main Mar 20, 2026
47 checks passed
@mroeschke
Copy link
Copy Markdown
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the fix-arm-float-to-int64-oob branch March 20, 2026 23:54
Sharl0tteIsTaken added a commit to Sharl0tteIsTaken/pandas that referenced this pull request Mar 22, 2026
…h-origin

* upstream/main: (199 commits)
  DEPR: Change assert_index_equal(exact=) default from equiv to True (pandas-dev#64542)
  DOC: Fix invalid Sphinx cross-references in older whatsnew RST files (pandas-dev#64459)
  TYP: Allow type[csv.Dialect] to be passed to read_csv() (pandas-dev#64559)
  TST: add regression tests for Needs Tests issues (pandas-dev#64745)
  TST: add regression test for loc setitem with DatetimeIndex and str column name (pandas-dev#64743)
  BUG: fix convert_dtypes dropping values from sliced mixed-dtype DataFrames (pandas-dev#64712)
  BUG: Fix get_level_values for boolean, NA-like, and integer index names (pandas-dev#63514)
  BUG: Fix 2D ExtensionArray handling in construction and CSV serialization (pandas-dev#64634)
  BUG: fix float-to-int64 OOB handling on ARM in to_datetime/to_timedelta (pandas-dev#64643)
  CLN: Make _CustomBusinessMonth.cbday_roll and month_roll private (pandas-dev#64739)
  DEPR: ExcelFile.parse (pandas-dev#64724)
  DEPR: unnecessary *args/**kwargs in groupby ops (pandas-dev#64720)
  DIST: include vendored licenses in wheel (pandas-dev#64709)
  DOC: Fix numpydoc validation warnings for DataFrame.plot.__call__ (pandas-dev#64736)
  CLN: Make offsets.BusinessHour.next_bday private (pandas-dev#64715)
  CLN: make _CustomBusinessMonth.cbday_roll private (pandas-dev#64734)
  ENH: Support axis parameter in BaseMaskedArray.any/all for 2D (pandas-dev#64510)
  PERF: Micro-optimizations in C extension source files (pandas-dev#64669)
  DEPR: Deprecate xlrd and pyxlsb Excel engines in favor of calamine (pandas-dev#64640)
  DOC: fix incomplete sentence in Styler docstring (pandas-dev#64682)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Datetime Datetime data dtype

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants