Timedelta

class typhoon.types.timedelta.Timedelta(*args, **kwargs)

Bases: Timedelta

Represents a duration, the difference between two dates or times.

Timedelta is the pandas equivalent of Python’s datetime.timedelta and is interchangeable with it in most cases.

Parameters:
  • value (Timedelta, timedelta, np.timedelta64, str, or int) –

  • unit (str, default 's') –

    Denote the unit of the input, if input is an integer.

    Possible values:
    • ’W’, ‘D’, “H”, ‘T’, ‘S’, ‘L’, ‘U’, or ‘N’

    • ’days’, ‘day’, or ‘d’

    • ’hours’, ‘hour’, ‘hr’, or ‘h’

    • ’minutes’, ‘minute’, ‘min’, or ‘m’

    • ’seconds’, ‘second’, ‘sec’, or “s”

    • ’milliseconds’, ‘millisecond’, ‘millis’, ‘milli’, or “ms”

    • ’microseconds’, ‘microsecond’, ‘micros’, ‘micro’, or “us”

    • ’nanoseconds’, ‘nanosecond’, ‘nanos’, ‘nano’, or ‘ns’.

Notes

The constructor may take in either both values of value and unit or kwargs as above. Either one of them must be used during initialization

The value attribute is always in nanoseconds.

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

Examples

Here we initialize a Timedelta object with both a value and a unit

>>> from typhoon.types.timedelta import Timedelta as td
>>> td1 = td('10s')

or

>>> td1 = td(10, "s")

We can get extra information about the Timedelta using:

>>> td1 = td("300us 500ns")
>>> td1.nanoseconds
500
>>> td1.microseconds
300

Our Typhoontest Timedelta patches a slicing issue with the original pandas structure:

>>> import pandas as pd  # Pandas without Typhoon patch
>>> import numpy as np
>>>
>>> t = np.arange(0, 1, 0.0001)
>>> y = np.sin(2*np.pi*60*t)
>>>
>>> serie = pd.Series(y, index=pd.TimedeltaIndex(t, "s"))
>>>
>>> t1 = serie["0.6s":"0.7s"]
>>> t1.index[0] == pd.Timedelta("0.6s")
True
>>> t1.index[-1] == pd.Timedelta("0.7s")
False

Running the same code with typhoon.types.timedelta.Timedelta:

>>> from typhoon.test.signals import pandas_sine
>>>
>>> sine = pandas_sine()
>>> sliced = sine["0.6s":"0.7s"]
>>> sliced.index[0] == td("0.6s")
True
>>> sliced.index[-1] == td("0.7s")
True

Here we test the total_seconds method that also had to be modified and patch because of error in nanosecond scale

>>> import pandas as pd  # Pandas without Typhoon patch
>>> t = pd.TimedeltaIndex([0, 5e-7], "s")
>>> dt = t[1] - t[0]
>>> dt.total_seconds() == 5e-07
False
>>> dt.total_seconds()
0.0

With the Typhoontest patch version:

>>> sine = pandas_sine(Ts=5e-7)
>>> dt = sine.index[1] - sine.index[0]
>>> dt.total_seconds() == 5e-07
True
>>> dt.total_seconds()
5e-07

Notes

Attributes: Attributes of Timedelta class

asm8 - Return a numpy timedelta64 array scalar view.

components - Returns the timedelta component values in a tuple-like.

days - Number of days.

delta - Return the timedelta in nanoseconds (ns), for internal compatibility.

microseconds - Number of microseconds (>= 0 and less than 1 second).

nanoseconds - Return the number of nanoseconds (n), where 0 <= n < 1 microsecond.

resolution_string - Return a string representing the lowest timedelta resolution.

seconds - Number of seconds (>= 0 and less than 1 day).

freq -

is_populated -

value -

Notes

Methods: Methods of Timedelta class

ceil(freq)

Return a new Timedelta ceiled to this resolution.

floor(freq)

Return a new Timedelta floored to this resolution.

isoformat()

Format Timedelta as ISO 8601 Duration like P[n]Y[n]M[n]DT[n]H[n]M[n]S, where the [n] s are replaced by the values.

round(freq)

Round the Timedelta to the specified resolution.

to_numpy()

Convert the Timedelta to a NumPy timedelta64.

to_pytimedelta()

Convert a pandas Timedelta object into a Python datetime.timedelta object.

to_timedelta64()

Return a numpy.timedelta64 object with ‘ns’ precision.

total_seconds()

Total seconds in the duration.

view(datatype)

Array view compatibility.