Skip to content

Added improvements in to_datetime Error reporting message - Outofbounds error message #47849

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
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ cpdef array_to_datetime(
else:
raise TypeError(f"{type(val)} is not convertible to datetime")

except OutOfBoundsDatetime:
except OutOfBoundsDatetime as ex:
ex.args = (str(ex) + f" present at position {i}", )
if is_coerce:
iresult[i] = NPY_NAT
continue
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/base/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_constructor_datetime_outofbound(self, a, klass):

# Explicit dtype specified
# Forced conversion fails for all -> all cases raise error
msg = "Out of bounds"
msg = "Out of bounds|Out of bounds .* present at position 0"
with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg):
klass(a, dtype="datetime64[ns]")

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def test_construction_outofbounds(self):
# coerces to object
tm.assert_index_equal(Index(dates), exp)

msg = "Out of bounds nanosecond timestamp"
msg = "Out of bounds .* present at position 0"
with pytest.raises(OutOfBoundsDatetime, match=msg):
# can't create DatetimeIndex
DatetimeIndex(dates)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_scalar_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_dti_date(self):
@pytest.mark.parametrize("data", [["1400-01-01"], [datetime(1400, 1, 1)]])
def test_dti_date_out_of_range(self, data):
# GH#1475
msg = "Out of bounds nanosecond timestamp: 1400-01-01 00:00:00"
msg = "Out of bounds .* present at position 0"
with pytest.raises(OutOfBoundsDatetime, match=msg):
DatetimeIndex(data)

Expand Down
14 changes: 9 additions & 5 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,10 @@ def test_to_datetime_dt64s(self, cache, dt):
"dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")]
)
def test_to_datetime_dt64s_out_of_bounds(self, cache, dt):
msg = f"Out of bounds nanosecond timestamp: {dt}"
msg = "Out of bounds .* present at position 0"
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime(dt, errors="raise")
msg = f"Out of bounds nanosecond timestamp: {dt}"
with pytest.raises(OutOfBoundsDatetime, match=msg):
Timestamp(dt)
assert to_datetime(dt, errors="coerce", cache=cache) is NaT
Expand Down Expand Up @@ -973,13 +974,13 @@ def test_datetime_outofbounds_scalar(self, value, format, infer):
assert res is NaT

if format is not None:
msg = "is a bad directive in format|Out of bounds nanosecond timestamp"
msg = "is a bad directive in format|Out of bounds .* present at position 0"
with pytest.raises(ValueError, match=msg):
to_datetime(
value, errors="raise", format=format, infer_datetime_format=infer
)
else:
msg = "Out of bounds nanosecond timestamp"
msg = "Out of bounds .* present at position 0"
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime(
value, errors="raise", format=format, infer_datetime_format=infer
Expand Down Expand Up @@ -1700,7 +1701,7 @@ def test_to_datetime_barely_out_of_bounds(self):
# in an in-bounds datetime
arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object)

msg = "Out of bounds nanosecond timestamp"
msg = "Out of bounds .* present at position 0"
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime(arr)

Expand Down Expand Up @@ -2593,7 +2594,10 @@ def test_invalid_origins_tzinfo(self):
@pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"])
def test_to_datetime_out_of_bounds_with_format_arg(self, format):
# see gh-23830
msg = "Out of bounds nanosecond timestamp"
msg = (
"Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 "
"present at position 0"
)
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime("2417-10-27 00:00:00", format=format)

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/tslibs/test_array_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_coerce_outside_ns_bounds(invalid_date, errors):
kwargs = {"values": arr, "errors": errors}

if errors == "raise":
msg = "Out of bounds nanosecond timestamp"
msg = "Out of bounds .* present at position 0"

with pytest.raises(ValueError, match=msg):
tslib.array_to_datetime(**kwargs)
Expand Down Expand Up @@ -170,7 +170,9 @@ def test_to_datetime_barely_out_of_bounds():
# Close enough to bounds that dropping nanos
# would result in an in-bounds datetime.
arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object)
msg = "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16"
msg = (
"Out of bounds nanosecond timestamp: 2262-04-11 23:47:16 present at position 0"
)

with pytest.raises(tslib.OutOfBoundsDatetime, match=msg):
tslib.array_to_datetime(arr)
Expand Down