Statistical¶
Fill each column from a summary statistic of its own observed values.
Univariate statistical imputers.
Each column is filled independently from a summary statistic of its own observed values (mean, median, mode, quantile, ...).
MeanImputer
¶
Bases: BaseImputer
Impute missing values using column means.
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import MeanImputer
>>> df = pd.DataFrame({"a": [1, 2, np.nan, 4]})
>>> imputer = MeanImputer()
>>> imputed = imputer.impute(df)
>>> print(imputed.loc[2, "a"]) # Mean of [1, 2, 4]
2.333...
impute
¶
Fill each column's missing values with that column's mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with potential NaN values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. Columns with no observed values stay NaN. |
Source code in src/imputation_methods/statistical.py
MedianImputer
¶
Bases: BaseImputer
Impute missing values using column medians.
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import MedianImputer
>>> df = pd.DataFrame({"a": [1, 2, np.nan, 10]})
>>> imputer = MedianImputer()
>>> imputed = imputer.impute(df)
>>> print(imputed.loc[2, "a"]) # Median of [1, 2, 10]
2.0
impute
¶
Fill each column's missing values with that column's median.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with potential NaN values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. Columns with no observed values stay NaN. |
Source code in src/imputation_methods/statistical.py
ModeImputer
¶
ModeImputer(dropna: bool = True)
Bases: BaseImputer
Impute missing values with the mode (most frequent value).
Particularly useful for categorical data or discrete numeric data. For continuous data with no repeated values, falls back to median.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dropna
|
bool
|
Whether to exclude NaN values when computing mode. Default: True |
True
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import ModeImputer
>>> df = pd.DataFrame({'a': [1, 2, 2, np.nan, 2, 3]})
>>> imputer = ModeImputer()
>>> imputed = imputer.impute(df)
>>> # Missing value filled with 2 (most frequent)
References
Standard statistical technique for categorical/discrete data.
Initialize the mode imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dropna
|
bool
|
Whether to exclude NaN values when computing mode |
True
|
Source code in src/imputation_methods/statistical.py
impute
¶
Impute using the mode of each column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |
Source code in src/imputation_methods/statistical.py
ConstantImputer
¶
Bases: BaseImputer
Impute missing values with a user-specified constant.
Allows different constants for different columns or a single constant for all columns. Useful for domain-specific imputation strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fill_value
|
float | dict[str, float]
|
Constant value(s) to use for imputation. Can be: - A scalar (applied to all columns) - A dict mapping column names to fill values Default: 0 |
0
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import ConstantImputer
>>> df = pd.DataFrame({'a': [1, np.nan, 3], 'b': [np.nan, 2, 3]})
>>> # Single value for all columns
>>> imputer = ConstantImputer(fill_value=-999)
>>> imputed = imputer.impute(df)
>>>
>>> # Different values per column
>>> imputer = ConstantImputer(fill_value={'a': 0, 'b': 100})
>>> imputed = imputer.impute(df)
References
Common practice in many domains (e.g., -999 for missing sensor data).
Initialize the constant imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fill_value
|
float | dict[str, float]
|
Constant value(s) for imputation |
0
|
Source code in src/imputation_methods/statistical.py
impute
¶
Impute using constant value(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fill_value dict contains unknown column names |
Source code in src/imputation_methods/statistical.py
QuantileImputer
¶
QuantileImputer(quantile: float = 0.5)
Bases: BaseImputer
Impute using specified quantile of observed values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quantile
|
float
|
Quantile to use (0.0 to 1.0). Default: 0.5 (median) |
0.5
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import QuantileImputer
>>> df = pd.DataFrame({'a': [1, 2, np.nan, 4, 5]})
>>> # Use 75th percentile
>>> imputer = QuantileImputer(quantile=0.75)
>>> imputed = imputer.impute(df)
Initialize the quantile imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quantile
|
float
|
Quantile value (0.0 to 1.0) |
0.5
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If quantile is not between 0 and 1 |
Source code in src/imputation_methods/statistical.py
impute
¶
Impute using specified quantile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |
Source code in src/imputation_methods/statistical.py
TrimmedMeanImputer
¶
TrimmedMeanImputer(trim_fraction: float = 0.1)
Bases: BaseImputer
Trimmed mean imputation excluding extreme values.
Computes mean after removing a percentage of extreme values from both ends. More robust than simple mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trim_fraction
|
float
|
Fraction to trim from each end (0-0.5). Default: 0.1 |
0.1
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import TrimmedMeanImputer
>>> df = pd.DataFrame({'a': [1, 2, np.nan, 4, 100]}) # 100 is outlier
>>> imputer = TrimmedMeanImputer(trim_fraction=0.2)
>>> imputed = imputer.impute(df)
>>> # Excludes 100 from mean calculation
References
Robust statistics using trimmed estimators.
Initialize the trimmed mean imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trim_fraction
|
float
|
Fraction to trim (0-0.5) |
0.1
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If trim_fraction not in [0, 0.5] |
Source code in src/imputation_methods/statistical.py
impute
¶
Impute using trimmed mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |
Source code in src/imputation_methods/statistical.py
EndOfDistributionImputer
¶
Bases: BaseImputer
Impute at the edges of the distribution (mean ± n_std*std).
Useful for flagging or handling extreme/suspicious values. Can impute at low end (mean - n_stdstd) or high end (mean + n_stdstd).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
position
|
str
|
Where to impute ('low' or 'high'). Default: 'high' |
'high'
|
n_std
|
float
|
Number of standard deviations from mean. Default: 3.0 |
3.0
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import EndOfDistributionImputer
>>> df = pd.DataFrame({'a': [1, 2, 3, np.nan, 5]})
>>> imputer = EndOfDistributionImputer(position='high', n_std=2)
>>> imputed = imputer.impute(df)
>>> # Missing value filled with mean + 2*std
References
Used in outlier detection and robust imputation strategies.
Initialize the end-of-distribution imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
position
|
str
|
'low' (mean - n_stdstd) or 'high' (mean + n_stdstd) |
'high'
|
n_std
|
float
|
Number of standard deviations |
3.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If position is not 'low' or 'high' |
Source code in src/imputation_methods/statistical.py
impute
¶
Impute at distribution edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |
Source code in src/imputation_methods/statistical.py
GroupMeanImputer
¶
Bases: BaseImputer
Group-wise mean or median imputation.
Imputes missing values using statistics computed within groups. Useful for panel data, time series with categories, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_col
|
str
|
Column name to group by (must be in the dataframe) |
required |
strategy
|
str
|
Aggregation strategy ('mean' or 'median'). Default: 'mean' |
'mean'
|
global_fallback
|
bool
|
Use global statistic if group has no data. Default: True |
True
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import GroupMeanImputer
>>> df = pd.DataFrame({
... 'category': [1, 1, 2, 2, 1],
... 'value': [10, np.nan, 20, np.nan, 12]
... })
>>> imputer = GroupMeanImputer(group_col='category', strategy='mean')
>>> imputed = imputer.impute(df)
>>> # Row 1 filled with mean of category 1, row 3 with mean of category 2
References
Common in hierarchical data and panel data analysis.
Initialize the group mean imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_col
|
str
|
Column name to group by |
required |
strategy
|
str
|
'mean' or 'median' |
'mean'
|
global_fallback
|
bool
|
Use global statistic for groups with no data |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If strategy is not 'mean' or 'median' |
Source code in src/imputation_methods/statistical.py
impute
¶
Impute using group-wise statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If group_col is not in dataframe columns |
Source code in src/imputation_methods/statistical.py
IndicatorImputer
¶
Bases: BaseImputer
Impute and add binary indicator columns for missingness.
Creates indicator columns showing which values were missing, then imputes the original columns. Useful when missingness itself is informative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
str
|
Imputation strategy for values ('mean', 'median', 'zero'). Default: 'mean' |
'mean'
|
indicator_prefix
|
str
|
Prefix for indicator column names. Default: 'missing_' |
'missing_'
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import IndicatorImputer
>>> df = pd.DataFrame({'a': [1, 2, np.nan, 4], 'b': [5, np.nan, 7, 8]})
>>> imputer = IndicatorImputer(strategy='mean')
>>> imputed = imputer.impute(df)
>>> print(imputed.columns.tolist())
['a', 'b', 'missing_a', 'missing_b']
Initialize the indicator imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
str
|
Imputation strategy |
'mean'
|
indicator_prefix
|
str
|
Prefix for indicator columns |
'missing_'
|
Source code in src/imputation_methods/statistical.py
impute
¶
Impute and add indicator columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe with additional indicator columns. |