Skip to content

Commit 26f7d1f

Browse files
committed
improving OutOfBoundsDatetime exception messages
1 parent 682e4bb commit 26f7d1f

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

pandas/tests/arrays/categorical/test_take.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_take_bounds(self, allow_fill):
2929
if allow_fill:
3030
msg = "indices are out-of-bounds"
3131
else:
32-
msg = "index 4 is out of bounds for( axis 0 with)? size 3"
32+
msg = "index 4 is for( axis 0 with)? size 3"
3333
with pytest.raises(IndexError, match=msg):
3434
cat.take([4, 5], allow_fill=allow_fill)
3535

pandas/tests/base/test_constructors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ 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 = "Out of bounds"
160+
msg = "Out of bounds|" \
161+
f"Cannot convert \"{a[0]}\" at position 0 to datetime"
161162
with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg):
162163
klass(a, dtype="datetime64[ns]")
163164

pandas/tests/indexes/datetimes/test_constructors.py

Lines changed: 1 addition & 1 deletion
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 = "Out of bounds nanosecond timestamp"
524+
msg = f'Cannot convert "{dates[0]}" at position 0 to datetime'
525525
with pytest.raises(OutOfBoundsDatetime, match=msg):
526526
# can't create DatetimeIndex
527527
DatetimeIndex(dates)

pandas/tests/indexes/datetimes/test_scalar_compat.py

Lines changed: 1 addition & 1 deletion
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 = "Out of bounds nanosecond timestamp: 1400-01-01 00:00:00"
41+
msg = f'Cannot convert "{data[0]}" at position 0 to datetime'
4242
with pytest.raises(OutOfBoundsDatetime, match=msg):
4343
DatetimeIndex(data)
4444

pandas/tests/tools/test_to_datetime.py

Lines changed: 8 additions & 5 deletions
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 = f'Cannot convert "{dt}" at position 0 to datetime'
692692
with pytest.raises(OutOfBoundsDatetime, match=msg):
693693
to_datetime(dt, errors="raise")
694694
msg = f"Out of bounds nanosecond timestamp: {dt}"
@@ -974,13 +974,16 @@ def test_datetime_outofbounds_scalar(self, value, format, infer):
974974
assert res is NaT
975975

976976
if format is not None:
977-
msg = f"is a bad directive in format|Cannot convert \"{value}\" at position 0 to datetime"
977+
msg = (
978+
"is a bad directive in format|"
979+
f'Cannot convert "{value}" at position 0 to datetime'
980+
)
978981
with pytest.raises(ValueError, match=msg):
979982
to_datetime(
980983
value, errors="raise", format=format, infer_datetime_format=infer
981984
)
982985
else:
983-
msg = f"Cannot convert \"{value}\" at position 0 to datetime"
986+
msg = f'Cannot convert "{value}" at position 0 to datetime'
984987
with pytest.raises(OutOfBoundsDatetime, match=msg):
985988
to_datetime(
986989
value, errors="raise", format=format, infer_datetime_format=infer
@@ -1701,7 +1704,7 @@ def test_to_datetime_barely_out_of_bounds(self):
17011704
# in an in-bounds datetime
17021705
arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object)
17031706

1704-
msg = f"Cannot convert \"2262-04-11 23:47:16.854775808\" at position 0 to datetime"
1707+
msg = 'Cannot convert "2262-04-11 23:47:16.854775808" at position 0 to datetime'
17051708
with pytest.raises(OutOfBoundsDatetime, match=msg):
17061709
to_datetime(arr)
17071710

@@ -2594,7 +2597,7 @@ def test_invalid_origins_tzinfo(self):
25942597
@pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"])
25952598
def test_to_datetime_out_of_bounds_with_format_arg(self, format):
25962599
# see gh-23830
2597-
msg = f"Cannot convert \"2417-10-27 00:00:00\" at position 0 to datetime"
2600+
msg = 'Cannot convert "2417-10-27 00:00:00" at position 0 to datetime'
25982601
with pytest.raises(OutOfBoundsDatetime, match=msg):
25992602
to_datetime("2417-10-27 00:00:00", format=format)
26002603

pandas/tests/tslibs/test_array_to_datetime.py

Lines changed: 2 additions & 2 deletions
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 = "Out of bounds nanosecond timestamp"
128+
msg = f'Cannot convert "{arr[0]}" at position 0 to datetime'
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 = "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16"
173+
msg = f'Cannot convert "{arr[0]}" at position 0 to datetime'
174174

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

0 commit comments

Comments
 (0)