Skip to content

Commit

Permalink
ERR: Correct error message in to_datetime (pandas-dev#25467)
Browse files Browse the repository at this point in the history
* ERR: Correct error message in to_datetime

Closes pandas-devgh-23830

xref pandas-devgh-23969
  • Loading branch information
gfyoung authored and haison committed Mar 12, 2019
1 parent a136245 commit 33ddcd9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.25.0.rst
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions pandas/_libs/tslib.pyx
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Expand Up @@ -1868,6 +1868,15 @@ def test_invalid_origins_tzinfo(self):
pd.to_datetime(1, unit='D',
origin=datetime(2000, 1, 1, tzinfo=pytz.utc))

@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"
with pytest.raises(OutOfBoundsDatetime, match=msg):
to_datetime("2417-10-27 00:00:00", format=format)

def test_processing_order(self):
# make sure we handle out-of-bounds *before*
# constructing the dates
Expand Down

0 comments on commit 33ddcd9

Please sign in to comment.