Skip to content

Commit 0fdad4e

Browse files
authored
Added improvements in to_datetime Error reporting message - Outofbounds error message (pandas-dev#47849)
* Revert "addressing reviews CL#1" This reverts commit 69dc2d4. * improving OutOfBoundsDatetime exception messages * improving OutOfBoundsDatetime exception messages * improving OutOfBoundsDatetime exception messages * improving OutOfBoundsDatetime exception messages * improving OutOfBoundsDatetime exception messages * testcase update * improving OutOfBoundsDatetime exception messages * review comments improving OutOfBoundsDatetime exception messages * testcase update * addressed testcase review comments * Revert "testcase update" This reverts commit 6da730d. * precommit code format changes * precommit code format changes
1 parent 595ae8c commit 0fdad4e

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

pandas/_libs/tslib.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,8 @@ cpdef array_to_datetime(
652652
else:
653653
raise TypeError(f"{type(val)} is not convertible to datetime")
654654

655-
except OutOfBoundsDatetime:
655+
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

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 = "Out of bounds"
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 = "Out of bounds nanosecond timestamp"
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 = "Out of bounds nanosecond timestamp: 1400-01-01 00:00:00"
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

+9-5
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 = "Out of bounds .* present at position 0"
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 = "is a bad directive in format|Out of bounds .* present at position 0"
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 = "Out of bounds .* present at position 0"
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 = "Out of bounds .* present at position 0"
17041705
with pytest.raises(OutOfBoundsDatetime, match=msg):
17051706
to_datetime(arr)
17061707

@@ -2593,7 +2594,10 @@ 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 = (
2598+
"Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 "
2599+
"present at position 0"
2600+
)
25972601
with pytest.raises(OutOfBoundsDatetime, match=msg):
25982602
to_datetime("2417-10-27 00:00:00", format=format)
25992603

pandas/tests/tslibs/test_array_to_datetime.py

+4-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 = "Out of bounds nanosecond timestamp"
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,9 @@ 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 = (
174+
"Out of bounds nanosecond timestamp: 2262-04-11 23:47:16 present at position 0"
175+
)
174176

175177
with pytest.raises(tslib.OutOfBoundsDatetime, match=msg):
176178
tslib.array_to_datetime(arr)

0 commit comments

Comments
 (0)