Fast NumPy array functions written in C for high-performance numerical computing
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
High-performance moving window operations for time series analysis and sequential data processing. These functions support customizable window sizes, minimum count requirements, and deliver substantial performance improvements over equivalent NumPy implementations.
Compute rolling statistics over sliding windows with NaN-aware implementations.
def move_sum(a, window, min_count=None, axis=-1):
"""
Moving sum over specified window size.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
Returns:
ndarray, moving sum values
"""
def move_mean(a, window, min_count=None, axis=-1):
"""
Moving arithmetic mean over specified window size.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
Returns:
ndarray, moving mean values
"""Calculate rolling standard deviation and variance measures.
def move_std(a, window, min_count=None, axis=-1, ddof=0):
"""
Moving standard deviation over specified window size.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
- ddof: int, delta degrees of freedom (default: 0)
Returns:
ndarray, moving standard deviation values
"""
def move_var(a, window, min_count=None, axis=-1, ddof=0):
"""
Moving variance over specified window size.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
- ddof: int, delta degrees of freedom (default: 0)
Returns:
ndarray, moving variance values
"""Find minimum and maximum values within moving windows, including index locations.
def move_min(a, window, min_count=None, axis=-1):
"""
Moving minimum over specified window size.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
Returns:
ndarray, moving minimum values
"""
def move_max(a, window, min_count=None, axis=-1):
"""
Moving maximum over specified window size.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
Returns:
ndarray, moving maximum values
"""
def move_argmin(a, window, min_count=None, axis=-1):
"""
Moving indices of minimum values over specified window size.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
Returns:
ndarray, indices of moving minimum values within each window
"""
def move_argmax(a, window, min_count=None, axis=-1):
"""
Moving indices of maximum values over specified window size.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
Returns:
ndarray, indices of moving maximum values within each window
"""Compute moving median and ranking statistics.
def move_median(a, window, min_count=None, axis=-1):
"""
Moving median over specified window size.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
Returns:
ndarray, moving median values
"""
def move_rank(a, window, min_count=None, axis=-1):
"""
Moving rank of the last element in each window.
Rank is normalized to be between -1 and 1, where -1 indicates the last element
is the minimum in the window, 1 indicates it's the maximum, and 0 indicates
it's the median.
Parameters:
- a: array_like, input array
- window: int, size of moving window
- min_count: int or None, minimum number of valid values in window (default: window)
- axis: int, axis along which to move (default: -1)
Returns:
ndarray, normalized rank of last element in each window (-1 to 1)
"""import bottleneck as bn
import numpy as np
# Sample time series data with some missing values
prices = np.array([100, 102, np.nan, 98, 101, 103, 97, 105, np.nan, 99])
# Calculate moving averages with different window sizes
ma_3 = bn.move_mean(prices, window=3, min_count=2)
ma_5 = bn.move_mean(prices, window=5, min_count=3)
# Moving standard deviation for volatility analysis
volatility = bn.move_std(prices, window=5, min_count=3)
# Moving min/max for support/resistance levels
support = bn.move_min(prices, window=5, min_count=3)
resistance = bn.move_max(prices, window=5, min_count=3)
print("Original prices:", prices)
print("3-period MA: ", ma_3)
print("5-period MA: ", ma_5)
print("Volatility: ", volatility)import bottleneck as bn
import numpy as np
# Generate noisy signal
t = np.linspace(0, 4*np.pi, 100)
signal = np.sin(t) + 0.3 * np.random.randn(100)
# Apply moving window smoothing
window_size = 5
smoothed = bn.move_mean(signal, window=window_size, min_count=1)
# Calculate moving statistics for signal analysis
rolling_std = bn.move_std(signal, window=window_size, min_count=1)
rolling_median = bn.move_median(signal, window=window_size, min_count=1)
# Detect signal extremes using moving min/max
local_mins = bn.move_min(signal, window=window_size, min_count=1)
local_maxs = bn.move_max(signal, window=window_size, min_count=1)import bottleneck as bn
import numpy as np
# Stock price data
prices = np.array([45.5, 46.2, 47.1, 46.8, 47.5, 48.2, 47.9, 49.1, 48.7, 49.5])
# Simple moving average (SMA)
sma_5 = bn.move_mean(prices, window=5, min_count=1)
# Bollinger Bands calculation
window = 5
rolling_mean = bn.move_mean(prices, window=window, min_count=1)
rolling_std = bn.move_std(prices, window=window, min_count=1)
upper_band = rolling_mean + 2 * rolling_std
lower_band = rolling_mean - 2 * rolling_std
# Rate of change using moving windows
roc_3 = (prices / bn.move_mean(prices, window=3, min_count=1) - 1) * 100
# Moving rank for momentum analysis
momentum = bn.move_rank(prices, window=5, min_count=3)
print("Prices: ", prices)
print("SMA(5): ", sma_5)
print("Upper Band: ", upper_band)
print("Lower Band: ", lower_band)
print("Momentum: ", momentum)import bottleneck as bn
import numpy as np
# Multi-dimensional time series (e.g., multiple sensors)
data = np.random.randn(3, 20) # 3 sensors, 20 time points
data[:, [5, 12, 17]] = np.nan # Add some missing values
# Moving statistics along time axis (axis=1)
moving_avg = bn.move_mean(data, window=5, min_count=3, axis=1)
moving_vol = bn.move_std(data, window=5, min_count=3, axis=1)
# Moving statistics along sensor axis (axis=0)
cross_sensor_avg = bn.move_mean(data, window=2, min_count=1, axis=0)
print("Original shape:", data.shape)
print("Moving avg shape:", moving_avg.shape)
print("Cross-sensor avg shape:", cross_sensor_avg.shape)Moving window functions provide exceptional performance improvements:
min_count allows partial windows at boundariesThe performance advantage is most pronounced for:
Install with Tessl CLI
npx tessl i tessl/pypi-bottleneck