traffic.core.intervals

class traffic.core.intervals.Interval(start, stop)

Bases: object

overlap(other)

Returns True if two intervals overlap.

Return type:

bool

class traffic.core.intervals.IntervalCollection(data=None, *other, start=None, stop=None)

Bases: DataFrameMixin

A class to represent collections of Intervals.

An Interval consists of a start and stop attributes. Collections of intervals are stored as a DataFrame.

Intervals can be created using one of the following syntaxes:

>>> sample_dates = pd.date_range("2023-01-01", "2023-02-01", freq="1D")
>>> t0, t1, t2, t3, *_ = sample_dates
  • as a list of Interval:

    >>> IntervalCollection([Interval(t0, t1), Interval(t2, t3)])
    [[2023-01-01 00:00:00, 2023-01-02 00:00:00], ...]
    
  • as an expanded tuple of Interval:

    >>> IntervalCollection(Interval(t0, t1), Interval(t2, t3))
    [[2023-01-01 00:00:00, 2023-01-02 00:00:00], ...]
    
  • a list of start and stop values:

    >>> IntervalCollection([t0, t2], [t1, t3])
    [[2023-01-01 00:00:00, 2023-01-02 00:00:00], ...]
    
  • as a DataFrame:

    >>> df = pd.DataFrame({'start': [t0, t2], 'stop': [t1, t3]})
    >>> IntervalCollection(df)
    [[2023-01-01 00:00:00, 2023-01-02 00:00:00], ...]
    
consolidate()

Consolidate the IntervalCollection.

>>> sample_dates = pd.date_range("2023-01-01", "2023-02-01", freq="1D")
>>> t0, t1, t2, t3, *_ = sample_dates
>>> interval = IntervalCollection([Interval(t0, t2), Interval(t1, t3)])
>>> interval
[[2023-01-01 ..., 2023-01-03 ...], [2023-01-02 ..., 2023-01-04 ...]]
:rtype: :sphinx_autodoc_typehints_type:`\:py\:class\:\`\~traffic.core.intervals.IntervalCollection\``
>>> interval.consolidate()
[[2023-01-01 ..., 2023-01-04 ...]]
intersection(other)

Returns the result of an intersection of intervals.

Parameters:

other (Interval | IntervalCollection) – the second interval or collection of intervals

Return type:

None | IntervalCollection

Returns:

may be None if the intersection is empty

Note

The binary operator & is equivalent to this method.

total_duration()

Returns the sum of durations of all intervals.

Return type:

Timedelta

union(other)

Returns the result of an union of intervals.

Parameters:

other (IntervalCollection) – the second interval or collection of intervals

Return type:

IntervalCollection

Note

The binary operator | is equivalent to this method.