From 8dde0abe3c4273e0891ef77f023e5b0a40dd3cdc Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 2 Dec 2018 08:40:13 -0800 Subject: [PATCH] do NPY_NAT check inside get_datetime64_nanos (#24031) --- pandas/_libs/tslib.pyx | 17 +++++++---------- pandas/_libs/tslibs/conversion.pyx | 11 ++++++----- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 609608a0948c5d..1205ebbe311e2a 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -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 diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 65f3894cb631a5..4a34065fe471f0 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -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) @@ -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: