Python implementation of the Hampel Filter
Slowing down. Last release over 3 years ago, little activity since. Fine for existing code; think twice for a new project.
pip install hampelHampel
Warning: Starting from version 1.0.1, there are breaking changes in this library. Please review the Usage section for details and update your code accordingly.
The Hampel filter is generally used to detect anomalies in data with a timeseries structure. It basically consists of a sliding window of a parameterizable size. For each window, each observation will be compared with the Median Absolute Deviation (MAD). The observation will be considered an outlier in the case in which it exceeds the MAD by n times (the parameter n is also parameterizable).
For more details, see the Related Links section.

Table of Contents
Installation
To use the Hampel filter in your Python project, you can install it via pip:
pip install hampel
Usage
Here's a simple example of how to use the Hampel filter:
import pandas as pd
from hampel import hampel
# Sample data as a pandas.Series
data = pd.Series([1.0, 2.0, 3.0, 100.0, 4.0, 5.0, 6.0])
# Apply the Hampel filter
result = hampel(data, window_size=3, n_sigma=3.0)
print(result.filtered_data)
When you apply the Hampel filter, it returns a Result object with the following attributes:
-
filtered_data: The data with outliers replaced. -
outlier_indices: Indices of the detected outliers. -
medians: Median values within the sliding window. -
median_absolute_deviations: Median Absolute Deviation (MAD) values within the sliding window.