88import numpy as np
99import pandas .tslib as tslib
1010from pandas import compat , _np_version_under1p7
11- from pandas .core .common import (ABCSeries , is_integer , is_timedelta64_dtype ,
11+ from pandas .core .common import (ABCSeries , is_integer , is_integer_dtype , is_timedelta64_dtype ,
1212 _values_from_object , is_list_like , isnull )
1313
1414repr_timedelta = tslib .repr_timedelta64
@@ -23,7 +23,7 @@ def to_timedelta(arg, box=True, unit='ns'):
2323 arg : string, timedelta, array of strings (with possible NAs)
2424 box : boolean, default True
2525 If True returns a Series of the results, if False returns ndarray of values
26- unit : unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer/float number
26+ unit : unit of the arg (D,h,m, s,ms,us,ns) denote the unit, which is an integer/float number
2727
2828 Returns
2929 -------
@@ -32,18 +32,22 @@ def to_timedelta(arg, box=True, unit='ns'):
3232 if _np_version_under1p7 :
3333 raise ValueError ("to_timedelta is not support for numpy < 1.7" )
3434
35- def _convert_listlike (arg , box ):
35+ def _convert_listlike (arg , box , unit ):
3636
3737 if isinstance (arg , (list ,tuple )):
3838 arg = np .array (arg , dtype = 'O' )
3939
4040 if is_timedelta64_dtype (arg ):
41- if box :
42- from pandas import Series
43- return Series (arg ,dtype = 'm8[ns]' )
44- return arg
41+ value = arg .astype ('timedelta64[ns]' )
42+ elif is_integer_dtype (arg ):
43+ # these are shortcutable
44+ value = arg .astype ('timedelta64[{0}]' .format (unit )).astype ('timedelta64[ns]' )
45+ else :
46+ try :
47+ value = tslib .array_to_timedelta64 (_ensure_object (arg ),unit = unit )
48+ except :
49+ value = np .array ([ _coerce_scalar_to_timedelta_type (r , unit = unit ) for r in arg ])
4550
46- value = np .array ([ _coerce_scalar_to_timedelta_type (r , unit = unit ) for r in arg ])
4751 if box :
4852 from pandas import Series
4953 value = Series (value ,dtype = 'm8[ns]' )
@@ -53,10 +57,10 @@ def _convert_listlike(arg, box):
5357 return arg
5458 elif isinstance (arg , ABCSeries ):
5559 from pandas import Series
56- values = _convert_listlike (arg .values , box = False )
60+ values = _convert_listlike (arg .values , box = False , unit = unit )
5761 return Series (values , index = arg .index , name = arg .name , dtype = 'm8[ns]' )
5862 elif is_list_like (arg ):
59- return _convert_listlike (arg , box = box )
63+ return _convert_listlike (arg , box = box , unit = unit )
6064
6165 # ...so it must be a scalar value. Return scalar.
6266 return _coerce_scalar_to_timedelta_type (arg , unit = unit )
@@ -139,7 +143,7 @@ def convert(r=None, unit=None, m=m):
139143 return convert
140144
141145 # no converter
142- raise ValueError ("cannot create timedelta string converter" )
146+ raise ValueError ("cannot create timedelta string converter for [{0}]" . format ( r ) )
143147
144148def _possibly_cast_to_timedelta (value , coerce = True ):
145149 """ try to cast to timedelta64, if already a timedeltalike, then make
0 commit comments