Skip to content

Commit 765049b

Browse files
committed
review comments improving OutOfBoundsDatetime exception messages
1 parent 2a96991 commit 765049b

File tree

6 files changed

+13
-12
lines changed

6 files changed

+13
-12
lines changed

pandas/_libs/tslib.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ cpdef array_to_datetime(
653653
raise TypeError(f"{type(val)} is not convertible to datetime")
654654

655655
except OutOfBoundsDatetime as ex:
656+
ex.args = (str(ex) + f" present at position {i}", )
656657
if is_coerce:
657658
iresult[i] = NPY_NAT
658659
continue
@@ -664,11 +665,11 @@ cpdef array_to_datetime(
664665

665666
# Still raise OutOfBoundsDatetime,
666667
# as error message is informative.
667-
raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") from ex
668+
raise
668669

669670
assert is_ignore
670671
return values, tz_out
671-
raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") from ex
672+
raise
672673

673674
except OutOfBoundsDatetime:
674675
if is_raise:

pandas/tests/base/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_constructor_datetime_outofbound(self, a, klass):
157157

158158
# Explicit dtype specified
159159
# Forced conversion fails for all -> all cases raise error
160-
msg = f'Out of bounds|Cannot convert "{a[0]}" at position 0 to datetime'
160+
msg = "Out of bounds|Out of bounds .* present at position 0"
161161
with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg):
162162
klass(a, dtype="datetime64[ns]")
163163

pandas/tests/indexes/datetimes/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def test_construction_outofbounds(self):
521521
# coerces to object
522522
tm.assert_index_equal(Index(dates), exp)
523523

524-
msg = f'Cannot convert "{dates[0]}" at position 0 to datetime'
524+
msg = "Out of bounds .* present at position 0"
525525
with pytest.raises(OutOfBoundsDatetime, match=msg):
526526
# can't create DatetimeIndex
527527
DatetimeIndex(dates)

pandas/tests/indexes/datetimes/test_scalar_compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_dti_date(self):
3838
@pytest.mark.parametrize("data", [["1400-01-01"], [datetime(1400, 1, 1)]])
3939
def test_dti_date_out_of_range(self, data):
4040
# GH#1475
41-
msg = f'Cannot convert "{data[0]}" at position 0 to datetime'
41+
msg = "Out of bounds .* present at position 0"
4242
with pytest.raises(OutOfBoundsDatetime, match=msg):
4343
DatetimeIndex(data)
4444

pandas/tests/tools/test_to_datetime.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ def test_to_datetime_dt64s(self, cache, dt):
688688
"dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")]
689689
)
690690
def test_to_datetime_dt64s_out_of_bounds(self, cache, dt):
691-
msg = f'Cannot convert "{dt}" at position 0 to datetime'
691+
msg = "Out of bounds .* present at position 0"
692692
with pytest.raises(OutOfBoundsDatetime, match=msg):
693693
to_datetime(dt, errors="raise")
694694
msg = f"Out of bounds nanosecond timestamp: {dt}"
@@ -976,14 +976,14 @@ def test_datetime_outofbounds_scalar(self, value, format, infer):
976976
if format is not None:
977977
msg = (
978978
"is a bad directive in format|"
979-
f'Cannot convert "{value}" at position 0 to datetime'
979+
"Out of bounds .* present at position 0"
980980
)
981981
with pytest.raises(ValueError, match=msg):
982982
to_datetime(
983983
value, errors="raise", format=format, infer_datetime_format=infer
984984
)
985985
else:
986-
msg = f'Cannot convert "{value}" at position 0 to datetime'
986+
msg = "Out of bounds .* present at position 0"
987987
with pytest.raises(OutOfBoundsDatetime, match=msg):
988988
to_datetime(
989989
value, errors="raise", format=format, infer_datetime_format=infer
@@ -1704,7 +1704,7 @@ def test_to_datetime_barely_out_of_bounds(self):
17041704
# in an in-bounds datetime
17051705
arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object)
17061706

1707-
msg = 'Cannot convert "2262-04-11 23:47:16.854775808" at position 0 to datetime'
1707+
msg = "Out of bounds .* present at position 0"
17081708
with pytest.raises(OutOfBoundsDatetime, match=msg):
17091709
to_datetime(arr)
17101710

@@ -2597,7 +2597,7 @@ def test_invalid_origins_tzinfo(self):
25972597
@pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"])
25982598
def test_to_datetime_out_of_bounds_with_format_arg(self, format):
25992599
# see gh-23830
2600-
msg = 'Cannot convert "2417-10-27 00:00:00" at position 0 to datetime'
2600+
msg = "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 present at position 0"
26012601
with pytest.raises(OutOfBoundsDatetime, match=msg):
26022602
to_datetime("2417-10-27 00:00:00", format=format)
26032603

pandas/tests/tslibs/test_array_to_datetime.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_coerce_outside_ns_bounds(invalid_date, errors):
125125
kwargs = {"values": arr, "errors": errors}
126126

127127
if errors == "raise":
128-
msg = f'Cannot convert "{arr[0]}" at position 0 to datetime'
128+
msg = "Out of bounds .* present at position 0"
129129

130130
with pytest.raises(ValueError, match=msg):
131131
tslib.array_to_datetime(**kwargs)
@@ -170,7 +170,7 @@ def test_to_datetime_barely_out_of_bounds():
170170
# Close enough to bounds that dropping nanos
171171
# would result in an in-bounds datetime.
172172
arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object)
173-
msg = f'Cannot convert "{arr[0]}" at position 0 to datetime'
173+
msg = "Out of bounds .* present at position 0"
174174

175175
with pytest.raises(tslib.OutOfBoundsDatetime, match=msg):
176176
tslib.array_to_datetime(arr)

0 commit comments

Comments
 (0)