rolling

function of dascore.proc.rolling source

rolling(
    patch: Patch ,
    step = None,
    center = False,
    engine: Literal[‘numpy’, ‘pandas’, None] = None,
    samples = False,
    overlap = None,
    **kwargs ,
)-> ’_NumpyPatchRoller | _PandasPatchRoller’

Apply a rolling function along a specified dimension.

See also the rolling section of the processing tutorial and the smoothing recipe.

Parameters

Parameter Description
patch The patch to apply the rolling function to.
step Evaluate every nth result, like slicing the output. This changes the
output length and is mutually exclusive with overlap.
center Label each window by its center rather than its right edge.
engine "numpy" uses sliding_window_view and "pandas" uses
pandas.rolling. None selects pandas only when the step is below 10
and the squeezed patch has fewer than two dimensions; otherwise it
selects NumPy. Explicit pandas supports at most two dimensions and
raises ParameterError above that.
samples If True, the values in kwargs and step represent samples along a
dimension. Must be integers. Otherwise, values are assumed to have
same units as the specified dimension, or have units attached.
overlap Window overlap in coordinate units, samples, or percent. When given,
step = window - overlap; percentages are relative to the window.
**kwargs Dimension and window size, such as time=10.
Note

Rolling follows Pandas DataFrame.rolling semantics. With no step, the output retains the input shape. When center=False, incomplete leading windows are NaN; when center=True, incomplete windows at both edges are NaN. Use Patch.dropna to remove them. A step downsamples that output. For example, the mean of [0, 1, 2, 3, 4, 5] is:

  • window 2: [NaN, 0.5, 1.5, 2.5, 3.5, 4.5]
  • window 3: [NaN, NaN, 1, 2, 3, 4]
  • window 3, step 2: [NaN, 1, 3]
  • window 3, step 3: [NaN, 2]

apply receives the rolling dimension as the last axis of each window; custom functions should reduce that axis. Extra arguments passed to apply are forwarded to the function.

Examples

import dascore as dc

# Simple example for rolling mean function
patch = dc.get_example_patch()

# apply rolling over 1 second with 0.5 step
mean_patch = patch.rolling(time=1, step=0.5).mean()

# drop nan at the start of the time axis.
out = mean_patch.dropna("time")