Commit 04931068 authored by Ibrahim's avatar Ibrahim
Browse files

Timeseries and plotting refactored

parent 592cb6c7
Loading
Loading
Loading
Loading

helpers/timeseries.py

0 → 100644
+48 −0
Original line number Diff line number Diff line
"""
Timeseries operations
"""

from typing import Iterable
from datetime import datetime, timedelta



def contiguous_sequences(index: Iterable[datetime], interval: timedelta,
                        filter_min: int=1) -> Iterable[Iterable[datetime]]:
    """
    Breaks up a `DatetimeIndex` or a list of timestamps into a list of contiguous
    sequences.

    Parameters
    ----------
    index: Iterable[pd.datetime]
        An index/list of timestamps in chronoligical order,
    interval: pd.Timedelta
        A `Timedelta` object specifying the uniform intervals to determine
        contiguous indices.
    filter_min: int, optional
        Minimum size of subsequence to include in the result.

    Returns
    -------
    List[Iterable[pd.datetime]]
        A list of lists of `pd.datetime` objects.
    """
    indices = []
    j, k = 0, 1
    while k < len(index):           # for each subsequence
        seq = [index[j]]
        indices.append(seq)
        while k < len(index):       # for each element in subsequence
            diff = index[k] - index[j]
            if diff == interval:    # exact interval, add to subsequence
                seq.append(index[k])
                k += 1
                j += 1
            elif diff < interval:   # interval too small, look ahead
                k += 1
            else:                   # new subsequence
                j = k
                k += 1
                break
    return [i for i in indices if len(i) >= filter_min]
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
from .surfaces import (
    plot_surface
)