@@ -8,6 +8,7 @@ import numpy as np
8
8
from cpython cimport (
9
9
PyTypeObject,
10
10
PyFloat_Check,
11
+ PyLong_Check,
11
12
PyObject_RichCompareBool,
12
13
PyObject_RichCompare,
13
14
PyString_Check,
@@ -55,6 +56,9 @@ cdef int64_t NPY_NAT = util.get_nat()
55
56
# < numpy 1.7 compat for NaT
56
57
compat_NaT = np.array([NPY_NAT]).astype(' m8[ns]' ).item()
57
58
59
+ # numpy actual nat object
60
+ np_NaT = np.datetime64(' NaT' ,dtype = ' M8' )
61
+
58
62
try :
59
63
basestring
60
64
except NameError : # py3
@@ -416,6 +420,11 @@ NaT = NaTType()
416
420
iNaT = util.get_nat()
417
421
418
422
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
+
419
428
cdef inline bint _cmp_nat_dt(_NaT lhs, _Timestamp rhs, int op) except - 1 :
420
429
return _nat_scalar_rules[op]
421
430
@@ -761,7 +770,7 @@ cdef convert_to_tsobject(object ts, object tz, object unit):
761
770
762
771
obj = _TSObject()
763
772
764
- if ts is None or ts is NaT:
773
+ if ts is None or ts is NaT or ts is np_NaT :
765
774
obj.value = NPY_NAT
766
775
elif is_datetime64_object(ts):
767
776
obj.value = _get_datetime64_nanos(ts)
@@ -933,7 +942,7 @@ def datetime_to_datetime64(ndarray[object] values):
933
942
iresult = result.view(' i8' )
934
943
for i in range (n):
935
944
val = values[i]
936
- if util._checknull (val) or val is NaT :
945
+ if _checknull_with_nat (val):
937
946
iresult[i] = iNaT
938
947
elif PyDateTime_Check(val):
939
948
if val.tzinfo is not None :
@@ -999,7 +1008,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
999
1008
iresult = result.view(' i8' )
1000
1009
for i in range (n):
1001
1010
val = values[i]
1002
- if util._checknull (val) or val is NaT :
1011
+ if _checknull_with_nat (val):
1003
1012
iresult[i] = iNaT
1004
1013
elif PyDateTime_Check(val):
1005
1014
if val.tzinfo is not None :
@@ -1038,13 +1047,16 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
1038
1047
continue
1039
1048
raise
1040
1049
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
1048
1060
1049
1061
# if we are coercing, dont' allow integers
1050
1062
elif util.is_integer_object(val) and not coerce :
@@ -1114,7 +1126,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
1114
1126
1115
1127
for i in range (n):
1116
1128
val = values[i]
1117
- if util._checknull (val):
1129
+ if _checknull_with_nat (val):
1118
1130
oresult[i] = val
1119
1131
elif util.is_string_object(val):
1120
1132
if len (val) == 0 :
@@ -1166,7 +1178,7 @@ def array_to_timedelta64(ndarray[object] values, coerce=True):
1166
1178
1167
1179
result[i] = val
1168
1180
1169
- elif util._checknull (val) or val == iNaT or val is NaT :
1181
+ elif _checknull_with_nat (val):
1170
1182
result[i] = iNaT
1171
1183
1172
1184
else :
@@ -1316,7 +1328,7 @@ def array_strptime(ndarray[object] values, object fmt, coerce=False):
1316
1328
iresult[i] = iNaT
1317
1329
continue
1318
1330
else :
1319
- if util._checknull (val) or val is NaT :
1331
+ if _checknull_with_nat (val):
1320
1332
iresult[i] = iNaT
1321
1333
continue
1322
1334
else :
0 commit comments