Skip to content

Commit 8dde0ab

Browse files
jbrockmendelPingviinituutti
authored andcommitted
do NPY_NAT check inside get_datetime64_nanos (pandas-dev#24031)
1 parent 2d360b0 commit 8dde0ab

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

pandas/_libs/tslib.pyx

+7-10
Original file line numberDiff line numberDiff line change
@@ -571,16 +571,13 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
571571

572572
elif is_datetime64_object(val):
573573
seen_datetime = 1
574-
if get_datetime64_value(val) == NPY_NAT:
575-
iresult[i] = NPY_NAT
576-
else:
577-
try:
578-
iresult[i] = get_datetime64_nanos(val)
579-
except OutOfBoundsDatetime:
580-
if is_coerce:
581-
iresult[i] = NPY_NAT
582-
continue
583-
raise
574+
try:
575+
iresult[i] = get_datetime64_nanos(val)
576+
except OutOfBoundsDatetime:
577+
if is_coerce:
578+
iresult[i] = NPY_NAT
579+
continue
580+
raise
584581

585582
elif is_integer_object(val) or is_float_object(val):
586583
# these must be ns unit by-definition

pandas/_libs/tslibs/conversion.pyx

+6-5
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1:
6262
NPY_DATETIMEUNIT unit
6363
npy_datetime ival
6464

65-
unit = get_datetime64_unit(val)
6665
ival = get_datetime64_value(val)
66+
if ival == NPY_NAT:
67+
return NPY_NAT
68+
69+
unit = get_datetime64_unit(val)
6770

6871
if unit != NPY_FR_ns:
6972
pandas_datetime_to_datetimestruct(ival, unit, &dts)
@@ -283,10 +286,8 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
283286
if ts is None or ts is NaT:
284287
obj.value = NPY_NAT
285288
elif is_datetime64_object(ts):
286-
if ts.view('i8') == NPY_NAT:
287-
obj.value = NPY_NAT
288-
else:
289-
obj.value = get_datetime64_nanos(ts)
289+
obj.value = get_datetime64_nanos(ts)
290+
if obj.value != NPY_NAT:
290291
dt64_to_dtstruct(obj.value, &obj.dts)
291292
elif is_integer_object(ts):
292293
if ts == NPY_NAT:

0 commit comments

Comments
 (0)