diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 9c7f35d240f96..b3e51191e8efa 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -652,7 +652,8 @@ cpdef array_to_datetime( else: raise TypeError(f"{type(val)} is not convertible to datetime") - except OutOfBoundsDatetime: + except OutOfBoundsDatetime as ex: + ex.args = (str(ex) + f" present at position {i}", ) if is_coerce: iresult[i] = NPY_NAT continue diff --git a/pandas/tests/base/test_constructors.py b/pandas/tests/base/test_constructors.py index 44d6bc57b0431..858eaacd67ec2 100644 --- a/pandas/tests/base/test_constructors.py +++ b/pandas/tests/base/test_constructors.py @@ -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 = "Out of bounds|Out of bounds .* present at position 0" with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg): klass(a, dtype="datetime64[ns]") diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index e971e311e7d20..086cb18dbe463 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -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 = "Out of bounds .* present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): # can't create DatetimeIndex DatetimeIndex(dates) diff --git a/pandas/tests/indexes/datetimes/test_scalar_compat.py b/pandas/tests/indexes/datetimes/test_scalar_compat.py index c60e56875bfcd..890590094094a 100644 --- a/pandas/tests/indexes/datetimes/test_scalar_compat.py +++ b/pandas/tests/indexes/datetimes/test_scalar_compat.py @@ -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 = "Out of bounds .* present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): DatetimeIndex(data) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index afa06bf1a79af..b128838318ac0 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -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 = "Out of bounds .* present at position 0" 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 @@ -973,13 +974,13 @@ 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|Out of bounds .* present at position 0" 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 = "Out of bounds .* present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime( value, errors="raise", format=format, infer_datetime_format=infer @@ -1700,7 +1701,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 = "Out of bounds .* present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime(arr) @@ -2593,7 +2594,10 @@ 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 = ( + "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 " + "present at position 0" + ) with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime("2417-10-27 00:00:00", format=format) diff --git a/pandas/tests/tslibs/test_array_to_datetime.py b/pandas/tests/tslibs/test_array_to_datetime.py index a0fafc227e001..64a45f6507810 100644 --- a/pandas/tests/tslibs/test_array_to_datetime.py +++ b/pandas/tests/tslibs/test_array_to_datetime.py @@ -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 = "Out of bounds .* present at position 0" with pytest.raises(ValueError, match=msg): tslib.array_to_datetime(**kwargs) @@ -170,7 +170,9 @@ 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 = ( + "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16 present at position 0" + ) with pytest.raises(tslib.OutOfBoundsDatetime, match=msg): tslib.array_to_datetime(arr)