1313from pandas .core .arrays .timedeltas import sequence_to_td64ns
1414
1515
16- def to_timedelta (arg , unit = "ns" , errors = "raise" ):
16+ def to_timedelta (arg , unit = None , errors = "raise" ):
1717 """
1818 Convert argument to timedelta.
1919
@@ -27,6 +27,7 @@ def to_timedelta(arg, unit="ns", errors="raise"):
2727 arg : str, timedelta, list-like or Series
2828 The data to be converted to timedelta.
2929 unit : str, default 'ns'
30+ Must not be specified if the arg is/contains a str.
3031 Denotes the unit of the arg. Possible values:
3132 ('W', 'D', 'days', 'day', 'hours', hour', 'hr', 'h',
3233 'm', 'minute', 'min', 'minutes', 'T', 'S', 'seconds',
@@ -76,7 +77,8 @@ def to_timedelta(arg, unit="ns", errors="raise"):
7677 TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
7778 dtype='timedelta64[ns]', freq=None)
7879 """
79- unit = parse_timedelta_unit (unit )
80+ if unit is not None :
81+ unit = parse_timedelta_unit (unit )
8082
8183 if errors not in ("ignore" , "raise" , "coerce" ):
8284 raise ValueError ("errors must be one of 'ignore', 'raise', or 'coerce'}" )
@@ -104,6 +106,9 @@ def to_timedelta(arg, unit="ns", errors="raise"):
104106 "arg must be a string, timedelta, list, tuple, 1-d array, or Series"
105107 )
106108
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+
107112 # ...so it must be a scalar value. Return scalar.
108113 return _coerce_scalar_to_timedelta_type (arg , unit = unit , errors = errors )
109114
@@ -124,7 +129,7 @@ def _coerce_scalar_to_timedelta_type(r, unit="ns", errors="raise"):
124129 return result
125130
126131
127- def _convert_listlike (arg , unit = "ns" , errors = "raise" , name = None ):
132+ def _convert_listlike (arg , unit = None , errors = "raise" , name = None ):
128133 """Convert a list of objects to a timedelta index object."""
129134 if isinstance (arg , (list , tuple )) or not hasattr (arg , "dtype" ):
130135 # This is needed only to ensure that in the case where we end up
0 commit comments