Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 2 additions & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,11 @@ cpdef array_to_datetime(

# Still raise OutOfBoundsDatetime,
# as error message is informative.
raise
raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime")

assert is_ignore
return values, tz_out
raise
raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime")

except OutOfBoundsDatetime:
if is_raise:
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 = f'Out of bounds|Cannot convert "{a[0]}" at position 0 to datetime'
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 = f'Cannot convert "{dates[0]}" at position 0 to datetime'
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 = f'Cannot convert "{data[0]}" at position 0 to datetime'
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 = f'Cannot convert "{dt}" at position 0 to datetime'
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,16 @@ 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|"
f'Cannot convert "{value}" at position 0 to datetime'
)
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 = f'Cannot convert "{value}" at position 0 to datetime'
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime(
value, errors="raise", format=format, infer_datetime_format=infer
Expand Down Expand Up @@ -1700,7 +1704,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 = 'Cannot convert "2262-04-11 23:47:16.854775808" at position 0 to datetime'
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime(arr)

Expand Down Expand Up @@ -2593,7 +2597,7 @@ 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 = 'Cannot convert "2417-10-27 00:00:00" at position 0 to datetime'
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime("2417-10-27 00:00:00", format=format)

Expand Down
4 changes: 2 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 = f'Cannot convert "{arr[0]}" at position 0 to datetime'

with pytest.raises(ValueError, match=msg):
tslib.array_to_datetime(**kwargs)
Expand Down Expand Up @@ -170,7 +170,7 @@ 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 = f'Cannot convert "{arr[0]}" at position 0 to datetime'

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