Skip to content

Commit

Permalink
do NPY_NAT check inside get_datetime64_nanos (pandas-dev#24031)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and Pingviinituutti committed Feb 28, 2019
1 parent 2d360b0 commit 8dde0ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
17 changes: 7 additions & 10 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,13 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',

elif is_datetime64_object(val):
seen_datetime = 1
if get_datetime64_value(val) == NPY_NAT:
iresult[i] = NPY_NAT
else:
try:
iresult[i] = get_datetime64_nanos(val)
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise
try:
iresult[i] = get_datetime64_nanos(val)
except OutOfBoundsDatetime:
if is_coerce:
iresult[i] = NPY_NAT
continue
raise

elif is_integer_object(val) or is_float_object(val):
# these must be ns unit by-definition
Expand Down
11 changes: 6 additions & 5 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1:
NPY_DATETIMEUNIT unit
npy_datetime ival

unit = get_datetime64_unit(val)
ival = get_datetime64_value(val)
if ival == NPY_NAT:
return NPY_NAT

unit = get_datetime64_unit(val)

if unit != NPY_FR_ns:
pandas_datetime_to_datetimestruct(ival, unit, &dts)
Expand Down Expand Up @@ -283,10 +286,8 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
if ts is None or ts is NaT:
obj.value = NPY_NAT
elif is_datetime64_object(ts):
if ts.view('i8') == NPY_NAT:
obj.value = NPY_NAT
else:
obj.value = get_datetime64_nanos(ts)
obj.value = get_datetime64_nanos(ts)
if obj.value != NPY_NAT:
dt64_to_dtstruct(obj.value, &obj.dts)
elif is_integer_object(ts):
if ts == NPY_NAT:
Expand Down

0 comments on commit 8dde0ab

Please sign in to comment.