Skip to content

Improve to_datetime bounds checking #50183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,22 @@ def array_with_unit_to_datetime(
# if we have nulls that are not type-compat
# then need to iterate

if values.dtype.kind in ["i", "f", "u"]:
if values.dtype.kind in ["i", "u"]:
iresult = values.astype("i8", copy=False)
# fill missing values by comparing to NPY_NAT
mask = iresult == NPY_NAT
iresult[mask] = 0
# for bounds checking, which can't use (integer) iresult * mult
# because it needs arithmetic overflow to not wrap around
fvalues = iresult.astype("f8") * mult
need_to_iterate = False

if values.dtype.kind in ["f",]:
mask = (values != values) | (values == NPY_NAT) # first is NaNs
fvalues = (values * mult).astype("f8")
fvalues[mask] = 0
need_to_iterate = False

if not need_to_iterate:
# check the bounds
if (fvalues < Timestamp.min.value).any() or (
Expand All @@ -313,11 +321,9 @@ def array_with_unit_to_datetime(
result = (iresult * mult).astype("M8[ns]")

elif values.dtype.kind == "f":
fresult = (values * mult).astype("f8")
fresult[mask] = 0
if prec:
fresult = round(fresult, prec)
result = fresult.astype("M8[ns]", copy=False)
fvalues = round(fvalues, prec)
result = fvalues.astype("M8[ns]", copy=False)

iresult = result.view("i8")
iresult[mask] = NPY_NAT
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,26 @@ def test_to_timestamp_unit_coerce(self, bad_val):
result = to_datetime([1, 2, bad_val], unit="D", errors="coerce")
tm.assert_index_equal(result, expected)

def test_float_to_datetime_raise_near_bounds(self):
msg = "cannot convert input with unit 'Y'"
oneyear_in_ns = 1e9 * 60 * 60 * 24 * 365.2425
tsmax_in_years = 2**63 / oneyear_in_ns # 2**63 ns, in years
# just in bounds
should_succeed = Series(
[0, tsmax_in_years - 0.05, -tsmax_in_years + 0.05], dtype=float
)
expected = (should_succeed * oneyear_in_ns).astype(np.int64)
for error_mode in ["raise", "coerce", "ignore"]:
result1 = to_datetime(should_succeed, unit="Y", errors=error_mode)
tm.assert_almost_equal(result1.astype(np.int64), expected, rtol=1e-10)
# just out of bounds
should_fail1 = Series([0, tsmax_in_years + 0.05], dtype=float)
should_fail2 = Series([0, -tsmax_in_years - 0.05], dtype=float)
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime(should_fail1, unit="Y", errors="raise")
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime(should_fail2, unit="Y", errors="raise")


class TestToDatetimeDataFrame:
@pytest.fixture
Expand Down