to_timedelta()

audinterface.utils.to_timedelta(durations, sampling_rate=None)[source]

Convert duration value(s) to pandas.Timedelta.

The single duration values support all formats mentioned in audmath.duration_in_seconds(), like '2 ms', or pandas.to_timedelta(2, 's'). The exception is that float and integer values are always interpreted as seconds and strings without unit always as samples.

Parameters
  • durations (Union[float, int, str, Timedelta, Sequence[Union[float, int, str, Timedelta]]]) – duration value(s). If value is a float or integer it is treated as seconds. To specify a unit provide as string, e.g. '2ms'. To specify in samples provide as string without unit, e.g. '2000'

  • sampling_rate (Optional[int]) – sampling rate in Hz. Needs to be provided if any duration value is provided in samples

Return type

Union[Timedelta, List[Timedelta]]

Returns

duration value(s) as pandas.Timedelta objects

Raises
  • ValueError – if a duration value is given in samples, but sampling_rate is None

  • ValueError – if a duration is a string that does not match a valid ‘<value><unit>’ pattern or the provided unit is not supported

Examples

>>> to_timedelta(2)
Timedelta('0 days 00:00:02')
>>> to_timedelta(2.0)
Timedelta('0 days 00:00:02')
>>> to_timedelta("2ms")
Timedelta('0 days 00:00:00.002000')
>>> to_timedelta("200milliseconds")
Timedelta('0 days 00:00:00.200000')
>>> to_timedelta([1, "2000"], 1000)
[Timedelta('0 days 00:00:01'), Timedelta('0 days 00:00:02')]