@@ -148,9 +148,9 @@ def _isnull_ndarraylike(obj):
148
148
elif values .dtype == np .dtype ('M8[ns]' ):
149
149
# this is the NaT pattern
150
150
result = values .view ('i8' ) == tslib .iNaT
151
- elif issubclass ( values .dtype . type , np .timedelta64 ):
152
- # -np.isfinite(values.view('i8'))
153
- result = np . ones ( values .shape , dtype = bool )
151
+ elif values .dtype == np .dtype ( 'm8[ns]' ):
152
+ # this is the NaT pattern
153
+ result = values .view ( 'i8' ) == tslib . iNaT
154
154
else :
155
155
# -np.isfinite(obj)
156
156
result = np .isnan (obj )
@@ -902,35 +902,50 @@ def _possibly_convert_platform(values):
902
902
return values
903
903
904
904
905
+ def _possibly_cast_to_timedelta (value ):
906
+ """ try to cast to timedelta64 w/o coercion """
907
+ new_value = tslib .array_to_timedelta64 (value .astype (object ), coerce = False )
908
+ if new_value .dtype == 'i8' :
909
+ value = np .array (new_value ,dtype = 'timedelta64[ns]' )
910
+ return value
911
+
905
912
def _possibly_cast_to_datetime (value , dtype , coerce = False ):
906
913
""" try to cast the array/value to a datetimelike dtype, converting float nan to iNaT """
907
914
908
915
if isinstance (dtype , basestring ):
909
916
dtype = np .dtype (dtype )
910
917
911
- if dtype is not None and is_datetime64_dtype (dtype ):
912
- if np .isscalar (value ):
913
- if value == tslib .iNaT or isnull (value ):
914
- value = tslib .iNaT
915
- else :
916
- value = np .array (value )
918
+ if dtype is not None :
919
+ is_datetime64 = is_datetime64_dtype (dtype )
920
+ is_timedelta64 = is_timedelta64_dtype (dtype )
917
921
918
- # have a scalar array-like (e.g. NaT)
919
- if value .ndim == 0 :
920
- value = tslib .iNaT
922
+ if is_datetime64 or is_timedelta64 :
921
923
922
- # we have an array of datetime & nulls
923
- elif np .prod (value .shape ):
924
- try :
925
- value = tslib .array_to_datetime (value , coerce = coerce )
926
- except :
927
- pass
924
+ if np .isscalar (value ):
925
+ if value == tslib .iNaT or isnull (value ):
926
+ value = tslib .iNaT
927
+ else :
928
+ value = np .array (value )
929
+
930
+ # have a scalar array-like (e.g. NaT)
931
+ if value .ndim == 0 :
932
+ value = tslib .iNaT
933
+
934
+ # we have an array of datetime or timedeltas & nulls
935
+ elif np .prod (value .shape ) and value .dtype != dtype :
936
+ try :
937
+ if is_datetime64 :
938
+ value = tslib .array_to_datetime (value , coerce = coerce )
939
+ elif is_timedelta64 :
940
+ value = _possibly_cast_to_timedelta (value )
941
+ except :
942
+ pass
928
943
929
944
elif dtype is None :
930
945
# we might have a array (or single object) that is datetime like, and no dtype is passed
931
946
# don't change the value unless we find a datetime set
932
947
v = value
933
- if not ( is_list_like (v ) or hasattr ( v , 'len' ) ):
948
+ if not is_list_like (v ):
934
949
v = [ v ]
935
950
if len (v ):
936
951
inferred_type = lib .infer_dtype (v )
@@ -939,6 +954,8 @@ def _possibly_cast_to_datetime(value, dtype, coerce = False):
939
954
value = tslib .array_to_datetime (np .array (v ))
940
955
except :
941
956
pass
957
+ elif inferred_type == 'timedelta' :
958
+ value = _possibly_cast_to_timedelta (value )
942
959
943
960
return value
944
961
@@ -1281,6 +1298,16 @@ def is_datetime64_dtype(arr_or_dtype):
1281
1298
return issubclass (tipo , np .datetime64 )
1282
1299
1283
1300
1301
+ def is_timedelta64_dtype (arr_or_dtype ):
1302
+ if isinstance (arr_or_dtype , np .dtype ):
1303
+ tipo = arr_or_dtype .type
1304
+ elif isinstance (arr_or_dtype , type ):
1305
+ tipo = np .dtype (arr_or_dtype ).type
1306
+ else :
1307
+ tipo = arr_or_dtype .dtype .type
1308
+ return issubclass (tipo , np .timedelta64 )
1309
+
1310
+
1284
1311
def is_float_dtype (arr_or_dtype ):
1285
1312
if isinstance (arr_or_dtype , np .dtype ):
1286
1313
tipo = arr_or_dtype .type
@@ -1290,8 +1317,7 @@ def is_float_dtype(arr_or_dtype):
1290
1317
1291
1318
1292
1319
def is_list_like (arg ):
1293
- return hasattr (arg , '__iter__' ) and not isinstance (arg , basestring )
1294
-
1320
+ return hasattr (arg , '__iter__' ) and not isinstance (arg , basestring ) or hasattr (arg ,'len' )
1295
1321
1296
1322
def _is_sequence (x ):
1297
1323
try :
0 commit comments