From 613af9eb635f2b43c19b3f6cb85414c02af95e20 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Thu, 28 Feb 2019 03:56:49 +0000 Subject: [PATCH] ERR: Correct error message in to_datetime Closes gh-23830 xref gh-23969 --- doc/source/whatsnew/v0.25.0.rst | 3 ++- pandas/_libs/tslib.pyx | 5 ++--- pandas/tests/indexes/datetimes/test_tools.py | 10 ++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 170e7f14da3973..f8b57f668c44d8 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -104,7 +104,8 @@ Performance Improvements Bug Fixes ~~~~~~~~~ - +- Bug in :func:`to_datetime` which would raise an (incorrect) ``ValueError`` when called with a date far into the future and the ``format`` argument specified instead of raising ``OutOfBoundsDatetime`` (:issue:`23830`) +- - Categorical diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index f932e236b5218d..f61b5d5bea4f64 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -670,9 +670,8 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise', # dateutil parser will return incorrect result because # it will ignore nanoseconds if is_raise: - raise ValueError("time data {val} doesn't " - "match format specified" - .format(val=val)) + raise + assert is_ignore return values, tz_out raise diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index b94935d2521eb7..265f6459d438f4 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -1868,6 +1868,16 @@ def test_invalid_origins_tzinfo(self): pd.to_datetime(1, unit='D', origin=datetime(2000, 1, 1, tzinfo=pytz.utc)) + @pytest.mark.parametrize("kwargs", [ + dict(), + dict(format="%Y-%m-%d %H:%M:%S") + ]) + def test_to_datetime_out_of_bounds_with_format_arg(self, kwargs): + # see gh-23830 + msg = "Out of bounds nanosecond timestamp" + with pytest.raises(OutOfBoundsDatetime, match=msg): + to_datetime("2417-10-27 00:00:00", **kwargs) + def test_processing_order(self): # make sure we handle out-of-bounds *before* # constructing the dates