w3resource

Pandas: Data Manipulation - to_timedelta() function

to_timedelta() function

The to_timedelta() function is used to convert argument to datetime.

Timedeltas are absolute differences in times, expressed in difference units (e.g. days, hours, minutes, seconds). This method converts an argument from a recognized timedelta format / value into a Timedelta type.

Syntax:

pandas.to_timedelta(arg, unit='ns', errors='raise')

Parameters:

Name Description Type / Default Value Required / Optional
arg The data to be converted to timedelta. strtimedelta, list-like or Series Required
unit Denotes the unit of the arg. Possible values: ('Y', 'M', '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'). str
Default Value: 'ns'
Required
errors
  • 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.
boolea{‘ignore’, ‘raise’, ‘coerce’},
Default Value: ‘raise’ n
Required

Returns: timedelta64 or numpy.array of timedelta64
Output type returned if parsing succeeded.

Example:


Download the above Notebook from here.

Previous: to_datetime() function
Next: date_range() function