@@ -8,6 +8,7 @@ import numpy as np
88from cpython cimport (
99 PyTypeObject,
1010 PyFloat_Check,
11+ PyLong_Check,
1112 PyObject_RichCompareBool,
1213 PyObject_RichCompare,
1314 PyString_Check,
@@ -55,6 +56,9 @@ cdef int64_t NPY_NAT = util.get_nat()
5556# < numpy 1.7 compat for NaT
5657compat_NaT = np.array([NPY_NAT]).astype(' m8[ns]' ).item()
5758
59+ # numpy actual nat object
60+ np_NaT = np.datetime64(' NaT' ,dtype = ' M8' )
61+
5862try :
5963 basestring
6064except NameError : # py3
@@ -416,6 +420,11 @@ NaT = NaTType()
416420iNaT = util.get_nat()
417421
418422
423+ cdef inline bint _checknull_with_nat(object val):
424+ """ utility to check if a value is a nat or not """
425+ return val is None or (
426+ PyFloat_Check(val) and val != val) or val is NaT
427+
419428cdef inline bint _cmp_nat_dt(_NaT lhs, _Timestamp rhs, int op) except - 1 :
420429 return _nat_scalar_rules[op]
421430
@@ -761,7 +770,7 @@ cdef convert_to_tsobject(object ts, object tz, object unit):
761770
762771 obj = _TSObject()
763772
764- if ts is None or ts is NaT:
773+ if ts is None or ts is NaT or ts is np_NaT :
765774 obj.value = NPY_NAT
766775 elif is_datetime64_object(ts):
767776 obj.value = _get_datetime64_nanos(ts)
@@ -933,7 +942,7 @@ def datetime_to_datetime64(ndarray[object] values):
933942 iresult = result.view(' i8' )
934943 for i in range (n):
935944 val = values[i]
936- if util._checknull (val) or val is NaT :
945+ if _checknull_with_nat (val):
937946 iresult[i] = iNaT
938947 elif PyDateTime_Check(val):
939948 if val.tzinfo is not None :
@@ -999,7 +1008,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
9991008 iresult = result.view(' i8' )
10001009 for i in range (n):
10011010 val = values[i]
1002- if util._checknull (val) or val is NaT :
1011+ if _checknull_with_nat (val):
10031012 iresult[i] = iNaT
10041013 elif PyDateTime_Check(val):
10051014 if val.tzinfo is not None :
@@ -1038,13 +1047,16 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
10381047 continue
10391048 raise
10401049 elif util.is_datetime64_object(val):
1041- try :
1042- iresult[i] = _get_datetime64_nanos(val)
1043- except ValueError :
1044- if coerce :
1045- iresult[i] = iNaT
1046- continue
1047- raise
1050+ if val == np_NaT:
1051+ iresult[i] = iNaT
1052+ else :
1053+ try :
1054+ iresult[i] = _get_datetime64_nanos(val)
1055+ except ValueError :
1056+ if coerce :
1057+ iresult[i] = iNaT
1058+ continue
1059+ raise
10481060
10491061 # if we are coercing, dont' allow integers
10501062 elif util.is_integer_object(val) and not coerce :
@@ -1114,7 +1126,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
11141126
11151127 for i in range (n):
11161128 val = values[i]
1117- if util._checknull (val):
1129+ if _checknull_with_nat (val):
11181130 oresult[i] = val
11191131 elif util.is_string_object(val):
11201132 if len (val) == 0 :
@@ -1166,7 +1178,7 @@ def array_to_timedelta64(ndarray[object] values, coerce=True):
11661178
11671179 result[i] = val
11681180
1169- elif util._checknull (val) or val == iNaT or val is NaT :
1181+ elif _checknull_with_nat (val):
11701182 result[i] = iNaT
11711183
11721184 else :
@@ -1316,7 +1328,7 @@ def array_strptime(ndarray[object] values, object fmt, coerce=False):
13161328 iresult[i] = iNaT
13171329 continue
13181330 else :
1319- if util._checknull (val) or val is NaT :
1331+ if _checknull_with_nat (val):
13201332 iresult[i] = iNaT
13211333 continue
13221334 else :
0 commit comments