ANALYSIS TOOL BOX

Data Processing

AddTukeyOutlierColumn

Identify and flag statistical outliers using Tukey's IQR fence method.

outliersdata-qualityiqrpreprocessing

Introduction

Before trusting an average or feeding a column into a model, it helps to know whether a handful of extreme values are quietly distorting it. AddTukeyOutlierColumn answers "which values in this column are statistically unusual, using a method that doesn't assume a normal distribution?" by flagging values that fall outside Tukey's IQR fence — a robust, non-parametric boundary — and optionally visualizing the distribution with those boundaries marked. Reach for it for data quality checks, fraud or error detection, or any exploratory pass where you need a defensible, distribution-free definition of "unusual."

Teaching Note

The function is particularly useful for:

  • Data quality assessment and anomaly detection
  • Identifying extreme values in financial and scientific data
  • Preprocessing data for statistical modeling
  • Finding fraudulent transactions or unusual patterns
  • Quality control in manufacturing and operations
  • Exploratory data analysis and distribution understanding
  • Detecting measurement errors or data entry mistakes

Parameters

ParameterTypeDefaultDescription
dataframerequiredA pandas DataFrame containing the data to analyze for outliers. A new column will be added to flag outlier values.
value_column_namerequiredName of the numeric column to check for outliers.
tukey_boundary_multiplier1.5The multiplier for the IQR to determine outlier boundaries. Common values are 1.5 (standard outliers) or 3.0 (extreme outliers).
plot_tukey_outliersFalseIf True, generates a histogram visualization showing the distribution with outliers highlighted in red and boundary lines marked.
fill_color'#999999'Hex color code for non-outlier histogram bars when plotting. Only used if plot_tukey_outliers=True.
fill_transparency0.6Alpha transparency value for histogram bars (0.0 to 1.0). Only used if plot_tukey_outliers=True.
title_for_plot'Tukey Outliers'Main title text for the plot. Only used if plot_tukey_outliers=True.
subtitle_for_plot'Shows the values that are Tukey outliers.'Subtitle text displayed below the title. Only used if plot_tukey_outliers=True.
caption_for_plotNoneOptional caption text displayed at the bottom of the plot. Only used if plot_tukey_outliers=True.
data_source_for_plotNoneOptional data source attribution text appended to the caption. Only used if plot_tukey_outliers=True.
show_y_axisFalseIf True, displays the y-axis (frequency counts) on the plot. Only used if plot_tukey_outliers=True.
title_y_indent1.1Vertical position adjustment for the title as a fraction of the plot height. Only used if plot_tukey_outliers=True.
subtitle_y_indent1.05Vertical position adjustment for the subtitle as a fraction of the plot height. Only used if plot_tukey_outliers=True.
caption_y_indent-0.15Vertical position adjustment for the caption as a fraction of the plot height. Only used if plot_tukey_outliers=True.
figure_size(8, 6)Tuple specifying the (width, height) of the plot in inches. Only used if plot_tukey_outliers=True.

Returns

The original DataFrame with an additional binary column named {value_column_name} - Tukey outlier, where 1 indicates an outlier and 0 indicates a normal value. If plot_tukey_outliers=True, a visualization is also displayed.

Example

python
from analysistoolbox.data_processing import AddTukeyOutlierColumn
import pandas as pd

# Detect outliers in transaction amounts with default settings
transactions = pd.DataFrame({
    'transaction_id': range(1, 11),
    'amount': [100, 150, 120, 130, 140, 110, 125, 1500, 135, 145]
})
transactions = AddTukeyOutlierColumn(transactions, 'amount')
# Adds column 'amount - Tukey outlier' with 1 for the $1500 transaction