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 12 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
12 changes: 3 additions & 9 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,9 @@ def _to_datetime_with_unit(arg, unit, name, utc: bool, errors: str) -> Index:
elif arg.dtype.kind == "f":
mult, _ = precision_from_unit(unit)

iresult = arg.astype("i8")
mask = np.isnan(arg) | (arg == iNaT)
iresult[mask] = 0

fvalues = iresult.astype("f8") * mult
fvalues = (arg * mult).astype("f8")
fvalues[mask] = 0

if (fvalues < Timestamp.min.value).any() or (
fvalues > Timestamp.max.value
Expand All @@ -528,11 +526,7 @@ def _to_datetime_with_unit(arg, unit, name, utc: bool, errors: str) -> Index:
return _to_datetime_with_unit(arg, unit, name, utc, errors)
raise OutOfBoundsDatetime(f"cannot convert input with unit '{unit}'")

# TODO: is fresult meaningfully different from fvalues?
fresult = (arg * mult).astype("f8")
fresult[mask] = 0

arr = fresult.astype("M8[ns]", copy=False)
arr = fvalues.astype("M8[ns]", copy=False)
arr[mask] = np.datetime64("NaT", "ns")

tz_parsed = None
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 @@ -1935,6 +1935,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 'D'"
oneday_in_ns = 1e9 * 60 * 60 * 24
tsmax_in_days = 2**63 / oneday_in_ns # 2**63 ns, in days
# just in bounds
should_succeed = Series(
[0, tsmax_in_days - 0.05, -tsmax_in_days + 0.05], dtype=float
)
expected = (should_succeed * oneday_in_ns).astype(np.int64)
for error_mode in ["raise", "coerce", "ignore"]:
result1 = to_datetime(should_succeed, unit="D", 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_days + 0.05], dtype=float)
should_fail2 = Series([0, -tsmax_in_days - 0.05], dtype=float)
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime(should_fail1, unit="D", errors="raise")
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime(should_fail2, unit="D", errors="raise")


class TestToDatetimeDataFrame:
@pytest.fixture
Expand Down