Skip to content

Commit 893fceb

Browse files
committed
improving OutOfBoundsDatetime exception messages
1 parent f11a5d2 commit 893fceb

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

pandas/_libs/tslib.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,11 +664,11 @@ cpdef array_to_datetime(
664664

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

669669
assert is_ignore
670670
return values, tz_out
671-
raise
671+
raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime")
672672

673673
except OutOfBoundsDatetime:
674674
if is_raise:

pandas/tests/tools/test_to_datetime.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,9 +688,10 @@ 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"Out of bounds nanosecond timestamp: {dt}"
691+
msg = f"Cannot convert \"{dt}\" at position 0 to datetime"
692692
with pytest.raises(OutOfBoundsDatetime, match=msg):
693693
to_datetime(dt, errors="raise")
694+
msg = f"Out of bounds nanosecond timestamp: {dt}"
694695
with pytest.raises(OutOfBoundsDatetime, match=msg):
695696
Timestamp(dt)
696697
assert to_datetime(dt, errors="coerce", cache=cache) is NaT
@@ -973,13 +974,13 @@ def test_datetime_outofbounds_scalar(self, value, format, infer):
973974
assert res is NaT
974975

975976
if format is not None:
976-
msg = "is a bad directive in format|Out of bounds nanosecond timestamp"
977+
msg = f"is a bad directive in format|Cannot convert \"{value}\" at position 0 to datetime"
977978
with pytest.raises(ValueError, match=msg):
978979
to_datetime(
979980
value, errors="raise", format=format, infer_datetime_format=infer
980981
)
981982
else:
982-
msg = "Out of bounds nanosecond timestamp"
983+
msg = f"Cannot convert \"{value}\" at position 0 to datetime"
983984
with pytest.raises(OutOfBoundsDatetime, match=msg):
984985
to_datetime(
985986
value, errors="raise", format=format, infer_datetime_format=infer
@@ -1700,7 +1701,7 @@ def test_to_datetime_barely_out_of_bounds(self):
17001701
# in an in-bounds datetime
17011702
arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object)
17021703

1703-
msg = "Out of bounds nanosecond timestamp"
1704+
msg = f"Cannot convert \"2262-04-11 23:47:16.854775808\" at position 0 to datetime"
17041705
with pytest.raises(OutOfBoundsDatetime, match=msg):
17051706
to_datetime(arr)
17061707

@@ -2593,7 +2594,7 @@ def test_invalid_origins_tzinfo(self):
25932594
@pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"])
25942595
def test_to_datetime_out_of_bounds_with_format_arg(self, format):
25952596
# see gh-23830
2596-
msg = "Out of bounds nanosecond timestamp"
2597+
msg = f"Cannot convert \"2417-10-27 00:00:00\" at position 0 to datetime"
25972598
with pytest.raises(OutOfBoundsDatetime, match=msg):
25982599
to_datetime("2417-10-27 00:00:00", format=format)
25992600

0 commit comments

Comments
 (0)