Hampel Filter

raw JSON →
1.0.2 verified Mon Apr 27 auth: no python

Python implementation of the Hampel Filter for outlier detection in time series. Current version 1.0.2, requires Python >=3.8. Maintained as of 2025.

pip install hampel
error TypeError: 'tuple' object is not callable
cause Trying to call the result of hampel as a function, e.g., result()
fix
Access attributes directly: result.filtered_data or result.outliers.
error ValueError: window_size must be >= 2
cause window_size too small, minimum is 2
fix
Set window_size to at least 2 (recommended 3 or more).
gotcha The hampel function returns a namedtuple-like object, not a tuple. Access fields using .filtered_data and .outliers, not by index.
gotcha The 'window_size' parameter is half-window, meaning a value of 3 uses 3 points to each side (total window 7). This differs from some other implementations.

Simple outlier detection on a numpy array.

import numpy as np
from hampel import hampel

data = np.array([1, 2, 1, 2, 10, 2, 1, 2, 1])
result = hampel(data, window_size=3, n_sigma=3.0)
print(result.filtered_data)
print(result.outliers)