13
13
from pandas .core .arrays .timedeltas import sequence_to_td64ns
14
14
15
15
16
- def to_timedelta (arg , unit = "ns" , errors = "raise" ):
16
+ def to_timedelta (arg , unit = None , errors = "raise" ):
17
17
"""
18
18
Convert argument to timedelta.
19
19
@@ -27,6 +27,7 @@ def to_timedelta(arg, unit="ns", errors="raise"):
27
27
arg : str, timedelta, list-like or Series
28
28
The data to be converted to timedelta.
29
29
unit : str, default 'ns'
30
+ Must not be specified if the arg is/contains a str.
30
31
Denotes the unit of the arg. Possible values:
31
32
('W', 'D', 'days', 'day', 'hours', hour', 'hr', 'h',
32
33
'm', 'minute', 'min', 'minutes', 'T', 'S', 'seconds',
@@ -76,7 +77,8 @@ def to_timedelta(arg, unit="ns", errors="raise"):
76
77
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
77
78
dtype='timedelta64[ns]', freq=None)
78
79
"""
79
- unit = parse_timedelta_unit (unit )
80
+ if unit is not None :
81
+ unit = parse_timedelta_unit (unit )
80
82
81
83
if errors not in ("ignore" , "raise" , "coerce" ):
82
84
raise ValueError ("errors must be one of 'ignore', 'raise', or 'coerce'}" )
@@ -104,6 +106,9 @@ def to_timedelta(arg, unit="ns", errors="raise"):
104
106
"arg must be a string, timedelta, list, tuple, 1-d array, or Series"
105
107
)
106
108
109
+ if isinstance (arg , str ) and unit is not None :
110
+ raise ValueError ("unit must not be specified if the input is/contains a str" )
111
+
107
112
# ...so it must be a scalar value. Return scalar.
108
113
return _coerce_scalar_to_timedelta_type (arg , unit = unit , errors = errors )
109
114
@@ -124,7 +129,7 @@ def _coerce_scalar_to_timedelta_type(r, unit="ns", errors="raise"):
124
129
return result
125
130
126
131
127
- def _convert_listlike (arg , unit = "ns" , errors = "raise" , name = None ):
132
+ def _convert_listlike (arg , unit = None , errors = "raise" , name = None ):
128
133
"""Convert a list of objects to a timedelta index object."""
129
134
if isinstance (arg , (list , tuple )) or not hasattr (arg , "dtype" ):
130
135
# This is needed only to ensure that in the case where we end up
0 commit comments