Bottleneck
Bottleneck is a Python library that provides a collection of fast, C-optimized functions for NumPy arrays, particularly useful for operations involving NaNs (Not a Number). It aims to offer significant performance improvements over equivalent NumPy functions for large arrays by implementing them in C. The current version is 1.6.0, with releases generally following significant NumPy updates or critical bug fixes, ensuring compatibility and performance.
Warnings
- gotcha Results from bottleneck functions might differ slightly from NumPy equivalents due to distinct algorithms and floating-point arithmetic. This is generally within acceptable numerical precision but could be a concern for highly sensitive applications.
- gotcha Performance gains from bottleneck are primarily noticeable for large arrays and specific operations (especially those handling NaNs). For small arrays, the overhead of calling the C extension might negate or even reverse performance benefits compared to pure NumPy.
- breaking Bottleneck 1.x series (including 1.6.0) requires Python 3.10 or newer. Installing on older Python versions will fail.
Install
-
pip install bottleneck
Imports
- bottleneck
import bottleneck as bn
Quickstart
import numpy as np
import bottleneck as bn
a = np.array([1.0, 2.0, np.nan, 4.0])
b = np.arange(12.0).reshape(3, 4)
b[0, 0] = np.nan
print(f"Original array a: {a}")
print(f"bn.nansum(a): {bn.nansum(a)}")
print(f"\nOriginal array b:\n{b}")
print(f"bn.nanmean(b, axis=1): {bn.nanmean(b, axis=1)}")