Skip to content

Rolling Windows

Rolling Windows

apply_rolling_data(values, function, window, step=1)

Perform a rolling window analysis at the column col from data

Given a dataframe data with time series, call function at sections of length window at the data of column col. Append the results to data at a new columns with name label.

Parameters:

Name Type Description Default
values ndarray

1-D Time series of data

required
function Callable[[ndarray], ndarray]

Function to be called to calculate the rolling window analysis, the function must receive as input an array or pandas series. Its output must be either a number or a pandas series

required
window int

Length of the window to perform the analysis

required
step int

Step to take between two consecutive windows, by default 1

1

Returns:

Name Type Description
data array

Columns generated by the function applied

Source code in ceruleo/transformation/features/rolling_windows.py
def apply_rolling_data(values : np.ndarray, function: Callable[[np.ndarray], np.ndarray], window: int, step: int =1) -> np.array:
    """
    Perform a rolling window analysis at the column `col` from `data`

    Given a dataframe `data` with time series, call `function` at sections of length `window` at the data of column `col`. Append the results to `data` at a new columns with name `label`.

    Parameters:
        values: 1-D Time series of data
        function: Function to be called to calculate the rolling window analysis, the function must receive as input an array or pandas series. Its output must be either a number or a pandas series
        window: Length of the window to perform the analysis
        step: Step to take between two consecutive windows, by default 1

    Returns:
        data: Columns generated by the function applied
    """

    x = _strided_app(values, window, step)

    return np.vstack([function(np.array(b)) for b in x])