Skip to content

Commit efce492

Browse files
jbrockmendeljreback
authored andcommitted
Fix uncaught OutOfBounds in array_to_datetime (pandas-dev#19612)
1 parent 605a837 commit efce492

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

doc/source/whatsnew/v0.23.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ Datetimelike
703703
- Bug in :class:`Series` floor-division where operating on a scalar ``timedelta`` raises an exception (:issue:`18846`)
704704
- Bug in :class:`Series`` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` had results cast to ``dtype='int64'`` (:issue:`17250`)
705705
- Bug in :class:`TimedeltaIndex` where division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`19042`)
706-
- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`)
706+
- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (:issue:`19043`)
707707
- Bug in :class:`DatetimeIndex` where the repr was not showing high-precision time values at the end of a day (e.g., 23:59:59.999999999) (:issue:`19030`)
708708
- Bug where dividing a scalar timedelta-like object with :class:`TimedeltaIndex` performed the reciprocal operation (:issue:`19125`)
709709
- Bug in ``.astype()`` to non-ns timedelta units would hold the incorrect dtype (:issue:`19176`, :issue:`19223`, :issue:`12425`)
@@ -713,6 +713,7 @@ Datetimelike
713713
- Bug in comparison of :class:`DatetimeIndex` against ``None`` or ``datetime.date`` objects raising ``TypeError`` for ``==`` and ``!=`` comparisons instead of all-``False`` and all-``True``, respectively (:issue:`19301`)
714714
- Bug in :class:`Timestamp` and :func:`to_datetime` where a string representing a barely out-of-bounds timestamp would be incorrectly rounded down instead of raising ``OutOfBoundsDatetime`` (:issue:`19382`)
715715
- Bug in :func:`Timestamp.floor` :func:`DatetimeIndex.floor` where time stamps far in the future and past were not rounded correctly (:issue:`19206`)
716+
- Bug in :func:`to_datetime` where passing an out-of-bounds datetime with ``errors='coerce'`` and ``utc=True`` would raise ``OutOfBoundsDatetime`` instead of parsing to ``NaT`` (:issue:`19612`)
716717
-
717718

718719
Timezones

pandas/_libs/tslib.pyx

+6-7
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,10 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
524524
seen_datetime = 1
525525
if val.tzinfo is not None:
526526
if utc_convert:
527-
_ts = convert_datetime_to_tsobject(val, None)
528-
iresult[i] = _ts.value
529527
try:
530-
check_dts_bounds(&_ts.dts)
531-
except ValueError:
528+
_ts = convert_datetime_to_tsobject(val, None)
529+
iresult[i] = _ts.value
530+
except OutOfBoundsDatetime:
532531
if is_coerce:
533532
iresult[i] = NPY_NAT
534533
continue
@@ -544,7 +543,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
544543
iresult[i] += val.nanosecond
545544
try:
546545
check_dts_bounds(&dts)
547-
except ValueError:
546+
except OutOfBoundsDatetime:
548547
if is_coerce:
549548
iresult[i] = NPY_NAT
550549
continue
@@ -555,7 +554,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
555554
iresult[i] = pydate_to_dt64(val, &dts)
556555
try:
557556
check_dts_bounds(&dts)
558-
except ValueError:
557+
except OutOfBoundsDatetime:
559558
if is_coerce:
560559
iresult[i] = NPY_NAT
561560
continue
@@ -568,7 +567,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
568567
else:
569568
try:
570569
iresult[i] = get_datetime64_nanos(val)
571-
except ValueError:
570+
except OutOfBoundsDatetime:
572571
if is_coerce:
573572
iresult[i] = NPY_NAT
574573
continue

pandas/tests/indexes/datetimes/test_tools.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import dateutil
99
import numpy as np
1010
from dateutil.parser import parse
11-
from datetime import datetime, date, time
11+
from datetime import datetime, date, time, timedelta
1212
from distutils.version import LooseVersion
1313

1414
import pandas as pd
@@ -1503,6 +1503,15 @@ def test_parsers_iso8601(self):
15031503

15041504

15051505
class TestArrayToDatetime(object):
1506+
def test_coerce_out_of_bounds_utc(self):
1507+
# GH#19612
1508+
ts = Timestamp('1900-01-01', tz='US/Pacific')
1509+
dt = ts.to_pydatetime() - timedelta(days=365 * 300) # ~1600AD
1510+
arr = np.array([dt])
1511+
result = tslib.array_to_datetime(arr, utc=True, errors='coerce')
1512+
expected = np.array(['NaT'], dtype='datetime64[ns]')
1513+
tm.assert_numpy_array_equal(result, expected)
1514+
15061515
def test_parsing_valid_dates(self):
15071516
arr = np.array(['01-01-2013', '01-02-2013'], dtype=object)
15081517
tm.assert_numpy_array_equal(

0 commit comments

Comments
 (0)