Skip to main content
Version: 0.0.98

ts_shape.events.quality.outlier_detection

Classes
NameDescription
OutlierDetectionEventsProcesses time series data to detect outliers based on specified statistical methods.

OutlierDetectionEvents​

classsource
OutlierDetectionEvents(
dataframe: pd.DataFrame,
value_column: str,
event_uuid: str = "quality:outlier",
time_threshold: str = "5min",
group_by_uuid: bool = True,
*,
signal_uuids: list[str] | None = None
) -> None

Bases: Base

Processes time series data to detect outliers based on specified statistical methods.

Example
>>> import ts_shape
>>> df = ts_shape.make_timeseries(["temp"], n_outliers=3)
>>> out = ts_shape.OutlierDetectionEvents(df, "value_double").detect_outliers_zscore()
>>> out[["systime", "value_double", "severity"]].round(
... {"value_double": 1, "severity": 1}
... )
systime value_double severity
0 2025-01-01 01:34:30 89.7 8.9
1 2025-01-01 03:21:00 88.8 9.6
2 2025-01-01 05:23:00 88.6 9.8
3 2025-01-01 07:54:30 96.4 3.1
Functions
NameDescription
detect_outliers_zscoreDetects outliers using the Z-score method.
detect_outliers_iqrDetects outliers using the IQR method.
detect_outliers_madDetects outliers using the Median Absolute Deviation (MAD) method.
detect_outliers_isolation_forestDetects outliers using sklearn's IsolationForest algorithm.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameThe input time series DataFrame.required
value_columnstrThe name of the column containing the values for outlier detection.required
event_uuidstrA UUID or identifier for detected outlier events.'quality:outlier'
time_thresholdstrThe time threshold to group close events together.'5min'
group_by_uuidboolCompute the statistics (mean/std, quartiles, median/MAD, forest) and group events per uuid (default True), so a frame holding e.g. a temperature (~80) and a pressure (~2) is scored per signal. False pools every row into one population (one mean/std across all signals).True
signal_uuidslist[str] | NoneSignals to analyze. None (default) analyzes every uuid in the frame.None

detect_outliers_zscore​

methodsource
detect_outliers_zscore(
n_sigma: float = 3.0, include_singles: bool = True
) -> pd.DataFrame

Detects outliers using the Z-score method.

NaN values are ignored when computing the mean and standard deviation (one NaN used to turn every z-score into NaN, so nothing was found).

Parameters
NameTypeDescriptionDefault
n_sigmafloatThe Z-score threshold for detecting outliers: a value more than n_sigma standard deviations from the mean is an outlier (formerly threshold).3.0
include_singlesboolWhether to include single outliers in the output. Default is True.True
Returns
TypeDescription
DataFramepd.DataFrame: A DataFrame of detected outliers and grouped events.

detect_outliers_iqr​

methodsource
detect_outliers_iqr(
threshold: tuple = (1.5, 1.5), include_singles: bool = True
) -> pd.DataFrame

Detects outliers using the IQR method.

Parameters
NameTypeDescriptionDefault
thresholdtupleThe multipliers for the IQR range for detecting outliers (lower, upper).(1.5, 1.5)
include_singlesboolWhether to include single outliers in the output. Default is True.True
Returns
TypeDescription
DataFramepd.DataFrame: A DataFrame of detected outliers and grouped events.

detect_outliers_mad​

methodsource
detect_outliers_mad(
n_sigma: float = 3.5, include_singles: bool = True
) -> pd.DataFrame

Detects outliers using the Median Absolute Deviation (MAD) method. This method is more robust to outliers than z-score.

When more than half of the values are identical the MAD is 0. The modified z-score then falls back to the mean absolute deviation ((x - median) / (1.253314 * MeanAD)); if that is 0 too, no point is an outlier. (Previously MAD was replaced by machine epsilon, which flagged every value that differed from the median at all.)

Parameters
NameTypeDescriptionDefault
n_sigmafloatThe modified z-score threshold for detecting outliers (formerly threshold). Default is 3.5.3.5
include_singlesboolWhether to include single outliers in the output. Default is True.True
Returns
TypeDescription
DataFramepd.DataFrame: A DataFrame of detected outliers and grouped events.

detect_outliers_isolation_forest​

methodsource
detect_outliers_isolation_forest(
contamination: float = 0.1,
include_singles: bool = True,
random_state: int | None = 42,
) -> pd.DataFrame

Detects outliers using sklearn's IsolationForest algorithm. Needs scikit-learn (pip install ts-shape[ml]), imported on the first call.

Rows with a NaN value are not scored.

Parameters
NameTypeDescriptionDefault
contaminationfloatThe proportion of outliers in the dataset. Default is 0.1.0.1
include_singlesboolWhether to include single outliers in the output. Default is True.True
random_stateOptional[int]Random state for reproducibility. Default is 42.42
Returns
TypeDescription
DataFramepd.DataFrame: A DataFrame of detected outliers and grouped events.
Raises
TypeDescription
ImportErrorIf scikit-learn is not installed.