8
8
import numpy as np
9
9
import pandas .tslib as tslib
10
10
from 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 ,
12
12
_values_from_object , is_list_like , isnull )
13
13
14
14
repr_timedelta = tslib .repr_timedelta64
@@ -23,7 +23,7 @@ def to_timedelta(arg, box=True, unit='ns'):
23
23
arg : string, timedelta, array of strings (with possible NAs)
24
24
box : boolean, default True
25
25
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
27
27
28
28
Returns
29
29
-------
@@ -32,18 +32,22 @@ def to_timedelta(arg, box=True, unit='ns'):
32
32
if _np_version_under1p7 :
33
33
raise ValueError ("to_timedelta is not support for numpy < 1.7" )
34
34
35
- def _convert_listlike (arg , box ):
35
+ def _convert_listlike (arg , box , unit ):
36
36
37
37
if isinstance (arg , (list ,tuple )):
38
38
arg = np .array (arg , dtype = 'O' )
39
39
40
40
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 ])
45
50
46
- value = np .array ([ _coerce_scalar_to_timedelta_type (r , unit = unit ) for r in arg ])
47
51
if box :
48
52
from pandas import Series
49
53
value = Series (value ,dtype = 'm8[ns]' )
@@ -53,10 +57,10 @@ def _convert_listlike(arg, box):
53
57
return arg
54
58
elif isinstance (arg , ABCSeries ):
55
59
from pandas import Series
56
- values = _convert_listlike (arg .values , box = False )
60
+ values = _convert_listlike (arg .values , box = False , unit = unit )
57
61
return Series (values , index = arg .index , name = arg .name , dtype = 'm8[ns]' )
58
62
elif is_list_like (arg ):
59
- return _convert_listlike (arg , box = box )
63
+ return _convert_listlike (arg , box = box , unit = unit )
60
64
61
65
# ...so it must be a scalar value. Return scalar.
62
66
return _coerce_scalar_to_timedelta_type (arg , unit = unit )
@@ -139,7 +143,7 @@ def convert(r=None, unit=None, m=m):
139
143
return convert
140
144
141
145
# no converter
142
- raise ValueError ("cannot create timedelta string converter" )
146
+ raise ValueError ("cannot create timedelta string converter for [{0}]" . format ( r ) )
143
147
144
148
def _possibly_cast_to_timedelta (value , coerce = True ):
145
149
""" try to cast to timedelta64, if already a timedeltalike, then make
0 commit comments