pyspark.pandas.to_timedelta#

pyspark.pandas.to_timedelta(arg, unit=None, errors='raise')[source]#

Convert argument to timedelta.

Parameters
argstr, timedelta, list-like or Series

The data to be converted to timedelta.

unitstr, optional

Denotes the unit of the arg for numeric arg. Defaults to "ns".

Possible values: * ‘W’ * ‘D’ / ‘days’ / ‘day’ * ‘hours’ / ‘hour’ / ‘hr’ / ‘h’ * ‘m’ / ‘minute’ / ‘min’ / ‘minutes’ / ‘T’ * ‘S’ / ‘seconds’ / ‘sec’ / ‘second’ * ‘ms’ / ‘milliseconds’ / ‘millisecond’ / ‘milli’ / ‘millis’ / ‘L’ * ‘us’ / ‘microseconds’ / ‘microsecond’ / ‘micro’ / ‘micros’ / ‘U’ * ‘ns’ / ‘nanoseconds’ / ‘nano’ / ‘nanos’ / ‘nanosecond’ / ‘N’

Must not be specified when arg context strings and errors="raise".

Deprecated since version 4.0.0: Units ‘T’ and ‘L’ are deprecated and will be removed in a future version.

errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’
  • If ‘raise’, then invalid parsing will raise an exception.

  • If ‘coerce’, then invalid parsing will be set as NaT.

  • If ‘ignore’, then invalid parsing will return the input.

Returns
rettimedelta64, TimedeltaIndex or Series of timedelta64 if parsing succeeded.

See also

DataFrame.astype

Cast argument to a specified dtype.

to_datetime

Convert argument to datetime.

Notes

If the precision is higher than nanoseconds, the precision of the duration is truncated to nanoseconds for string inputs.

Examples

Parsing a single string to a Timedelta:

>>> ps.to_timedelta('1 days 06:05:01.00003')
Timedelta('1 days 06:05:01.000030')
>>> ps.to_timedelta('15.5us')  
Timedelta('0 days 00:00:00.000015500')

Parsing a list or array of strings:

>>> ps.to_timedelta(['1 days 06:05:01.00003', '15.5us', 'nan'])  
TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500', NaT],
               dtype='timedelta64[ns]', freq=None)

Converting numbers by specifying the unit keyword argument:

>>> ps.to_timedelta(np.arange(5), unit='s')  
TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02',
                '0 days 00:00:03', '0 days 00:00:04'],
               dtype='timedelta64[ns]', freq=None)
>>> ps.to_timedelta(np.arange(5), unit='d')  
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
               dtype='timedelta64[ns]', freq=None)