duration_in_seconds()¶
- audmath.duration_in_seconds(duration, sampling_rate=None)[source]¶
Duration in seconds.
Converts the given duration value to seconds. A unit can be provided when
duration
is given as a string. As units the following values are possible.Unit
Meaning
W
week
D, days, day
day
h, hours, hour, hr
hour
m, minutes, minute, min, T
minute
s, seconds, second, sec, S
second
ms, milliseconds, millisecond, millis, milli, L
millisecond
us, μs, microseconds, microsecond, micros, micro, U
microsecond
ns, nanoseconds, nanoseconds, nanos, nano, N
nanosecond
- Parameters
duration (
Union
[float
,int
,str
,timedelta64
,None
]) – ifduration
is a float, integer or string without unit it is treated as seconds or ifsampling_rate
is provided as samples. Ifduration
is provided as a string with unit, e.g.'2ms'
or'2 ms'
, or as anumpy.timedelta64
orpandas.Timedelta
object it will be converted to seconds andsampling_rate
is always ignored. If duration isNone
,numpy.nan
,pandas.NA
,pandas.NaT
,''
,'None'
,'NaN'
,'NaT'
, or any other lower/mixed case version of those stringsnumpy.nan
is returned. If duration isnumpy.inf
,'Inf'
or any other lower/mixed case version of that stringnumpy.inf
is returned, and-
numpy.inf
for the negative casesampling_rate (
Union
[float
,int
,None
]) – sampling rate in Hz. Is ignored if duration is provided with a unit
- Return type
- Returns
duration in seconds
- Raises
ValueError – if the provided unit is not supported
ValueError – if
duration
is a string that does not match a valid ‘<value><unit>’ pattern
Examples
>>> duration_in_seconds(2) np.float64(2.0) >>> duration_in_seconds(2.0) np.float64(2.0) >>> duration_in_seconds("2") np.float64(2.0) >>> duration_in_seconds("2ms") np.float64(0.002) >>> duration_in_seconds("2 ms") np.float64(0.002) >>> duration_in_seconds("ms") np.float64(0.001) >>> duration_in_seconds(2000, sampling_rate=1000) np.float64(2.0) >>> duration_in_seconds(np.timedelta64(2, "s")) np.float64(2.0) >>> duration_in_seconds(pd.to_timedelta(2, "s")) np.float64(2.0) >>> duration_in_seconds("Inf") inf >>> duration_in_seconds(None) nan