Skip to content

Commit 613af9e

Browse files
committed
ERR: Correct error message in to_datetime
Closes pandas-devgh-23830 xref pandas-devgh-23969
1 parent c986386 commit 613af9e

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-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

+2-3
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,8 @@ 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+
raise
674+
676675
assert is_ignore
677676
return values, tz_out
678677
raise

pandas/tests/indexes/datetimes/test_tools.py

+10
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,16 @@ 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("kwargs", [
1872+
dict(),
1873+
dict(format="%Y-%m-%d %H:%M:%S")
1874+
])
1875+
def test_to_datetime_out_of_bounds_with_format_arg(self, kwargs):
1876+
# see gh-23830
1877+
msg = "Out of bounds nanosecond timestamp"
1878+
with pytest.raises(OutOfBoundsDatetime, match=msg):
1879+
to_datetime("2417-10-27 00:00:00", **kwargs)
1880+
18711881
def test_processing_order(self):
18721882
# make sure we handle out-of-bounds *before*
18731883
# constructing the dates

0 commit comments

Comments
 (0)