Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uncaught OutOfBounds in array_to_datetime #19612

Merged
merged 8 commits into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,10 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
seen_datetime = 1
if val.tzinfo is not None:
if utc_convert:
_ts = convert_datetime_to_tsobject(val, None)
iresult[i] = _ts.value
try:
check_dts_bounds(&_ts.dts)
except ValueError:
_ts = convert_datetime_to_tsobject(val, None)
iresult[i] = _ts.value
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
Expand All @@ -542,7 +541,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
iresult[i] += val.nanosecond
try:
check_dts_bounds(&dts)
except ValueError:
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
Expand All @@ -553,7 +552,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
try:
check_dts_bounds(&dts)
seen_datetime = 1
except ValueError:
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
Expand All @@ -566,7 +565,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
try:
iresult[i] = get_datetime64_nanos(val)
seen_datetime = 1
except ValueError:
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
Expand Down Expand Up @@ -656,7 +655,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
try:
_ts = convert_datetime_to_tsobject(py_dt, None)
iresult[i] = _ts.value
except ValueError:
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/tslibs/test_tslib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from datetime import timedelta

import numpy as np

from pandas._libs.tslib import array_to_datetime
from pandas import Timestamp

import pandas.util.testing as tm


class TestArrayToDatetime(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm this should be in pandas/tests/indexes/datetimes/test_tools.py. I know you are ceating new locations is fine. but would rather move all of tools testing there (in a new PR).

def test_coerce_out_of_bounds_utc(self):
ts = Timestamp('1900-01-01', tz='US/Pacific')
dt = ts.to_pydatetime() - timedelta(days=365 * 300) # ~1600AD
arr = np.array([dt])
result = array_to_datetime(arr, utc=True, errors='coerce')
expected = np.array(['NaT'], dtype='datetime64[ns]')
tm.assert_numpy_array_equal(result, expected)