rollit
Zero-copy rolling window statistics for NumPy arrays
The Core Problem
In data science and quantitative finance, computing rolling window statistics (like moving averages, rolling volatility, or sliding mins/maxes) is a fundamental task. Normally, developers import pandas for this, utilizing df.rolling(w).mean().
However, pandas is a massive dependency. It is over 100MB, slow to import (often taking up to a full second on serverless cold starts), and brings unnecessary memory overhead. If your backend only processes raw NumPy arrays, introducing pandas just for rolling windows is highly inefficient.
The Solution: Zero-Copy Memory Striding
rollit bypasses pandas entirely. It uses low-level NumPy stride manipulation via numpy.lib.stride_tricks.as_strided to construct overlapping rolling window views over existing memory blocks.
By re-mapping the memory strides (dim steps), rollit creates the windowed representation with zero-copy memory allocation. A 10,000,000-element array windowed at size 100 uses exactly the same memory footprint as the original array, instead of allocating a new 1GB array structure.
To prevent segmentation faults (a common risk with raw stride operations when writing back to windowed views), rollit locks the returned array views as read-only.
Supported Operations
rollit provides a consistent, clean function signature supporting features like min_periods (to mask incomplete edge windows instead of throwing errors):
- rollit.mean — Rolling arithmetic average
- rollit.sum — Rolling summation
- rollit.std — Rolling standard deviation
- rollit.max / rollit.min — Rolling extreme value discovery
- rollit.zscore / rollit.normalize — Rolling normalization and standardization
- rollit.apply — Custom window mappings