Fast NumPy array functions written in C for high-performance numerical computing
npx @tessl/cli install tessl/pypi-bottleneck@1.5.0Fast NumPy array functions written in C for high-performance numerical computing. Bottleneck provides optimized implementations of statistical functions, moving window operations, ranking functions, and array manipulation utilities, delivering significant speed improvements over standard NumPy implementations.
pip install bottleneckimport bottleneck as bnFor testing and benchmarking:
import bottleneck as bn
# Run benchmarks
bn.bench()
# Run tests
bn.test()import bottleneck as bn
import numpy as np
# Create sample data with NaN values
data = np.array([1, 2, np.nan, 4, 5])
# Statistical functions ignore NaN values
mean_val = bn.nanmean(data) # 3.0
sum_val = bn.nansum(data) # 12.0
std_val = bn.nanstd(data) # 1.58...
# Moving window operations
series = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
moving_avg = bn.move_mean(series, window=3, min_count=1)
# array([1. , 1.5, 2. , 3. , 4. , 5. , 6. , 7. , 8. , 9. ])
# Ranking operations
ranks = bn.rankdata([3, 1, 4, 1, 5]) # array([3., 1.5, 4., 1.5, 5.])
# Array manipulation
arr = np.array([1, 2, np.nan, 4, 5])
bn.replace(arr, np.nan, 0) # Replaces NaN with 0 in-placeBottleneck uses a dual-implementation approach for maximum performance:
The library automatically falls back to slower implementations for:
This design ensures compatibility with all NumPy arrays while providing significant performance gains where possible.
Statistical and aggregation functions that reduce arrays along specified axes, with optimized NaN handling and support for all common statistical operations.
def nansum(a, axis=None): ...
def nanmean(a, axis=None): ...
def nanstd(a, axis=None, ddof=0): ...
def nanvar(a, axis=None, ddof=0): ...
def nanmin(a, axis=None): ...
def nanmax(a, axis=None): ...
def nanargmin(a, axis=None): ...
def nanargmax(a, axis=None): ...
def median(a, axis=None): ...
def nanmedian(a, axis=None): ...
def ss(a, axis=None): ...
def anynan(a, axis=None): ...
def allnan(a, axis=None): ...High-performance moving window operations for time series analysis and sequential data processing, supporting customizable window sizes and minimum count requirements.
def move_sum(a, window, min_count=None, axis=-1): ...
def move_mean(a, window, min_count=None, axis=-1): ...
def move_std(a, window, min_count=None, axis=-1, ddof=0): ...
def move_var(a, window, min_count=None, axis=-1, ddof=0): ...
def move_min(a, window, min_count=None, axis=-1): ...
def move_max(a, window, min_count=None, axis=-1): ...
def move_argmin(a, window, min_count=None, axis=-1): ...
def move_argmax(a, window, min_count=None, axis=-1): ...
def move_median(a, window, min_count=None, axis=-1): ...
def move_rank(a, window, min_count=None, axis=-1): ...Utilities for array transformation, ranking, and data manipulation operations that maintain array structure while modifying values or order.
def replace(a, old, new): ...
def rankdata(a, axis=None): ...
def nanrankdata(a, axis=None): ...
def partition(a, kth, axis=-1): ...
def argpartition(a, kth, axis=-1): ...
def push(a, n=None, axis=-1): ...Testing, benchmarking, and introspection utilities for performance analysis and development support.
def bench(): ...
def bench_detailed(func_name, fraction_nan=0.3): ...
def test(): ...
def get_functions(module_name, as_string=False): ...Bottleneck delivers substantial performance improvements over NumPy:
Performance gains are most significant for: