Skip to content

REF: Move non-raising parts of array_to_datetime outside of try/except #24032

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

Merged
merged 5 commits into from
Dec 2, 2018
Merged
Changes from all 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
124 changes: 74 additions & 50 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,10 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
# specify error conditions
assert is_raise or is_ignore or is_coerce

result = np.empty(n, dtype='M8[ns]')
iresult = result.view('i8')

try:
result = np.empty(n, dtype='M8[ns]')
iresult = result.view('i8')
for i in range(n):
val = values[i]

Expand Down Expand Up @@ -706,62 +707,85 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
raise TypeError("{typ} is not convertible to datetime"
.format(typ=type(val)))

if seen_datetime and seen_integer:
# we have mixed datetimes & integers

if is_coerce:
# coerce all of the integers/floats to NaT, preserve
# the datetimes and other convertibles
for i in range(n):
val = values[i]
if is_integer_object(val) or is_float_object(val):
result[i] = NPY_NAT
elif is_raise:
raise ValueError(
"mixed datetimes and integers in passed array")
else:
raise TypeError

if seen_datetime_offset and not utc_convert:
# GH 17697
# 1) If all the offsets are equal, return one offset for
# the parsed dates to (maybe) pass to DatetimeIndex
# 2) If the offsets are different, then force the parsing down the
# object path where an array of datetimes
# (with individual dateutil.tzoffsets) are returned
is_same_offsets = len(out_tzoffset_vals) == 1
if not is_same_offsets:
return array_to_datetime_object(values, is_raise,
dayfirst, yearfirst)
else:
tz_offset = out_tzoffset_vals.pop()
tz_out = pytz.FixedOffset(tz_offset / 60.)
return result, tz_out
except OutOfBoundsDatetime:
if is_raise:
raise

oresult = np.empty(n, dtype=object)
for i in range(n):
val = values[i]
return ignore_errors_out_of_bounds_fallback(values), tz_out

# set as nan except if its a NaT
if checknull_with_nat(val):
if isinstance(val, float):
oresult[i] = np.nan
else:
oresult[i] = NaT
elif is_datetime64_object(val):
if get_datetime64_value(val) == NPY_NAT:
oresult[i] = NaT
else:
oresult[i] = val.item()
else:
oresult[i] = val
return oresult, tz_out
except TypeError:
return array_to_datetime_object(values, is_raise, dayfirst, yearfirst)

if seen_datetime and seen_integer:
# we have mixed datetimes & integers

if is_coerce:
# coerce all of the integers/floats to NaT, preserve
# the datetimes and other convertibles
for i in range(n):
val = values[i]
if is_integer_object(val) or is_float_object(val):
result[i] = NPY_NAT
elif is_raise:
raise ValueError("mixed datetimes and integers in passed array")
else:
return array_to_datetime_object(values, is_raise,
dayfirst, yearfirst)

if seen_datetime_offset and not utc_convert:
# GH#17697
# 1) If all the offsets are equal, return one offset for
# the parsed dates to (maybe) pass to DatetimeIndex
# 2) If the offsets are different, then force the parsing down the
# object path where an array of datetimes
# (with individual dateutil.tzoffsets) are returned
is_same_offsets = len(out_tzoffset_vals) == 1
if not is_same_offsets:
return array_to_datetime_object(values, is_raise,
dayfirst, yearfirst)
else:
tz_offset = out_tzoffset_vals.pop()
tz_out = pytz.FixedOffset(tz_offset / 60.)
return result, tz_out


cdef inline ignore_errors_out_of_bounds_fallback(ndarray[object] values):
"""
Fallback for array_to_datetime if an OutOfBoundsDatetime is raised
and errors == "ignore"

Parameters
----------
values : ndarray[object]

Returns
-------
ndarray[object]
"""
cdef:
Py_ssize_t i, n = len(values)
object val

oresult = np.empty(n, dtype=object)

for i in range(n):
val = values[i]

# set as nan except if its a NaT
if checknull_with_nat(val):
if isinstance(val, float):
oresult[i] = np.nan
else:
oresult[i] = NaT
elif is_datetime64_object(val):
if get_datetime64_value(val) == NPY_NAT:
oresult[i] = NaT
else:
oresult[i] = val.item()
else:
oresult[i] = val
return oresult


@cython.wraparound(False)
@cython.boundscheck(False)
Expand Down