@@ -1155,52 +1155,26 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
11551155
11561156 return oresult
11571157
1158- def array_to_timedelta64 (ndarray[object] values , coerce = True ):
1158+ def array_to_timedelta64 (ndarray[object] values , coerce = False ):
11591159 """ convert an ndarray to an array of ints that are timedeltas
11601160 force conversion if coerce = True,
1161- else return an object array """
1161+ else will raise if cannot convert """
11621162 cdef:
11631163 Py_ssize_t i, n
1164- object val
1165- ndarray[int64_t] result
1164+ ndarray[int64_t] iresult
11661165
11671166 n = values.shape[0 ]
1168- result = np.empty(n, dtype = ' i8' )
1169- for i in range (n):
1170- val = values[i]
1171-
1172- # in py3 this is already an int, don't convert
1173- if is_integer_object(val):
1174- result[i] = val
1175-
1176- elif isinstance (val,timedelta) or isinstance (val,np.timedelta64):
1177-
1178- if isinstance (val, np.timedelta64):
1179- if val.dtype != ' m8[ns]' :
1180- val = val.astype(' m8[ns]' )
1181- val = val.item()
1182- else :
1183- val = _delta_to_nanoseconds(np.timedelta64(val).item())
1184-
1185- result[i] = val
1186-
1187- elif _checknull_with_nat(val):
1188- result[i] = iNaT
1189-
1190- else :
1191-
1192- # just return, don't convert
1193- if not coerce :
1194- return values.copy()
1195-
1196- result[i] = iNaT
1167+ result = np.empty(n, dtype = ' m8[ns]' )
1168+ iresult = result.view(' i8' )
11971169
1198- return result
1170+ for i in range (n):
1171+ result[i] = convert_to_timedelta64(values[i], ' ns' , coerce )
1172+ return iresult
11991173
1200- def convert_to_timedelta (object ts , object unit = ' ns' ):
1201- return convert_to_timedelta64(ts, unit)
1174+ def convert_to_timedelta (object ts , object unit = ' ns' , coerce = False ):
1175+ return convert_to_timedelta64(ts, unit, coerce )
12021176
1203- cdef convert_to_timedelta64(object ts, object unit):
1177+ cdef convert_to_timedelta64(object ts, object unit, object coerce ):
12041178 """
12051179 Convert an incoming object to a timedelta64 if possible
12061180
@@ -1210,6 +1184,8 @@ cdef convert_to_timedelta64(object ts, object unit):
12101184 - np.int64 (with unit providing a possible modifier)
12111185 - None/NaT
12121186
1187+ if coerce, set a non-valid value to NaT
1188+
12131189 Return a ns based int64
12141190
12151191 # kludgy here until we have a timedelta scalar
@@ -1237,7 +1213,9 @@ cdef convert_to_timedelta64(object ts, object unit):
12371213
12381214 if _np_version_under1p7:
12391215 if not isinstance (ts, timedelta):
1240- raise AssertionError (" Invalid type for timedelta scalar: %s " % type (ts))
1216+ if coerce :
1217+ return np.timedelta64(iNaT)
1218+ raise ValueError (" Invalid type for timedelta scalar: %s " % type (ts))
12411219 if not PY2:
12421220 # convert to microseconds in timedelta64
12431221 ts = np.timedelta64(int (ts.total_seconds()* 1e9 + ts.microseconds* 1000 ))
@@ -1247,7 +1225,9 @@ cdef convert_to_timedelta64(object ts, object unit):
12471225 if isinstance (ts, timedelta):
12481226 ts = np.timedelta64(ts)
12491227 elif not isinstance (ts, np.timedelta64):
1250- raise AssertionError (" Invalid type for timedelta scalar: %s " % type (ts))
1228+ if coerce :
1229+ return np.timedelta64(iNaT)
1230+ raise ValueError (" Invalid type for timedelta scalar: %s " % type (ts))
12511231 return ts.astype(' timedelta64[ns]' )
12521232
12531233def repr_timedelta64 (object value , format = None ):
0 commit comments