From 86669df40b10173de8bdf42caafadfdcf84089e3 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Thu, 28 Feb 2019 03:56:49 +0000 Subject: [PATCH 1/2] 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 | 8 +++++--- pandas/tests/indexes/datetimes/test_tools.py | 10 ++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 170e7f14da397..f8b57f668c44d 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 f932e236b5218..624872c1c56c6 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -670,9 +670,11 @@ 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)) + + # Still raise OutOfBoundsDatetime, + # as error message is informative. + 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 b94935d2521eb..265f6459d438f 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 From 30e7cd3c2d3a7262c259be4bd5dbe9f740927390 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Thu, 28 Feb 2019 07:29:22 +0000 Subject: [PATCH 2/2] Parameterize on format --- pandas/tests/indexes/datetimes/test_tools.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index 265f6459d438f..dd914d8a79837 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -1868,15 +1868,14 @@ 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") + @pytest.mark.parametrize("format", [ + None, "%Y-%m-%d %H:%M:%S" ]) - def test_to_datetime_out_of_bounds_with_format_arg(self, kwargs): + def test_to_datetime_out_of_bounds_with_format_arg(self, format): # see gh-23830 msg = "Out of bounds nanosecond timestamp" with pytest.raises(OutOfBoundsDatetime, match=msg): - to_datetime("2417-10-27 00:00:00", **kwargs) + to_datetime("2417-10-27 00:00:00", format=format) def test_processing_order(self): # make sure we handle out-of-bounds *before*