Skip to content

Commit 84875c1

Browse files
gfyoungjreback
authored andcommitted
ERR: Correct error message in to_datetime (#25467)
* ERR: Correct error message in to_datetime Closes gh-23830 xref gh-23969
1 parent 3b570e3 commit 84875c1

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

doc/source/whatsnew/v0.25.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ Performance Improvements
104104

105105
Bug Fixes
106106
~~~~~~~~~
107-
107+
- 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`)
108+
-
108109
-
109110

110111
Categorical

pandas/_libs/tslib.pyx

+5-3
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,11 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
670670
# dateutil parser will return incorrect result because
671671
# it will ignore nanoseconds
672672
if is_raise:
673-
raise ValueError("time data {val} doesn't "
674-
"match format specified"
675-
.format(val=val))
673+
674+
# Still raise OutOfBoundsDatetime,
675+
# as error message is informative.
676+
raise
677+
676678
assert is_ignore
677679
return values, tz_out
678680
raise

pandas/tests/indexes/datetimes/test_tools.py

+9
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,15 @@ def test_invalid_origins_tzinfo(self):
18681868
pd.to_datetime(1, unit='D',
18691869
origin=datetime(2000, 1, 1, tzinfo=pytz.utc))
18701870

1871+
@pytest.mark.parametrize("format", [
1872+
None, "%Y-%m-%d %H:%M:%S"
1873+
])
1874+
def test_to_datetime_out_of_bounds_with_format_arg(self, format):
1875+
# see gh-23830
1876+
msg = "Out of bounds nanosecond timestamp"
1877+
with pytest.raises(OutOfBoundsDatetime, match=msg):
1878+
to_datetime("2417-10-27 00:00:00", format=format)
1879+
18711880
def test_processing_order(self):
18721881
# make sure we handle out-of-bounds *before*
18731882
# constructing the dates

0 commit comments

Comments
 (0)