@@ -705,6 +705,54 @@ def diff(arr, n, axis=0):
705
705
return out_arr
706
706
707
707
708
+ def _coerce_scalar_to_timedelta_type (r ):
709
+ # kludgy here until we have a timedelta scalar
710
+ # handle the numpy < 1.7 case
711
+
712
+ if is_integer (r ):
713
+ r = timedelta (microseconds = r / 1000 )
714
+
715
+ if _np_version_under1p7 :
716
+ if not isinstance (r , timedelta ):
717
+ raise AssertionError ("Invalid type for timedelta scalar: %s" % type (r ))
718
+ if compat .PY3 :
719
+ # convert to microseconds in timedelta64
720
+ r = np .timedelta64 (int (r .total_seconds ()* 1e9 + r .microseconds * 1000 ))
721
+ else :
722
+ return r
723
+
724
+ if isinstance (r , timedelta ):
725
+ r = np .timedelta64 (r )
726
+ elif not isinstance (r , np .timedelta64 ):
727
+ raise AssertionError ("Invalid type for timedelta scalar: %s" % type (r ))
728
+ return r .astype ('timedelta64[ns]' )
729
+
730
+ def _coerce_to_dtypes (result , dtypes ):
731
+ """ given a dtypes and a result set, coerce the result elements to the dtypes """
732
+ if len (result ) != len (dtypes ):
733
+ raise AssertionError ("_coerce_to_dtypes requires equal len arrays" )
734
+
735
+ def conv (r ,dtype ):
736
+ try :
737
+ if isnull (r ):
738
+ pass
739
+ elif dtype == _NS_DTYPE :
740
+ r = Timestamp (r )
741
+ elif dtype == _TD_DTYPE :
742
+ r = _coerce_scalar_to_timedelta_type (r )
743
+ elif dtype == np .bool_ :
744
+ r = bool (r )
745
+ elif dtype .kind == 'f' :
746
+ r = float (r )
747
+ elif dtype .kind == 'i' :
748
+ r = int (r )
749
+ except :
750
+ pass
751
+
752
+ return r
753
+
754
+ return np .array ([ conv (r ,dtype ) for r , dtype in zip (result ,dtypes ) ])
755
+
708
756
def _infer_dtype_from_scalar (val ):
709
757
""" interpret the dtype from a scalar, upcast floats and ints
710
758
return the new value and the dtype """
@@ -1288,7 +1336,7 @@ def _possibly_cast_to_timedelta(value, coerce=True):
1288
1336
# coercion compatability
1289
1337
if coerce == 'compat' and _np_version_under1p7 :
1290
1338
1291
- def convert (td , type ):
1339
+ def convert (td , dtype ):
1292
1340
1293
1341
# we have an array with a non-object dtype
1294
1342
if hasattr (td ,'item' ):
@@ -1317,6 +1365,7 @@ def convert(td, type):
1317
1365
# < 1.7 coercion
1318
1366
if not is_list_like (value ):
1319
1367
value = np .array ([ value ])
1368
+
1320
1369
dtype = value .dtype
1321
1370
return np .array ([ convert (v ,dtype ) for v in value ], dtype = 'm8[ns]' )
1322
1371
0 commit comments