Donor sampling¶
Fill gaps with values sampled from the data or from reference values.
Donor-based imputers that fill gaps with sampled or reference values.
ReferenceValues
module-attribute
¶
ReferenceValues: TypeAlias = dict[str, float | np.floating[Any] | NDArray[np.floating[Any]]] | pd.DataFrame
Reference data for :class:ColdDeckImputer: per-column values or a dataframe.
RandomSamplingImputer
¶
RandomSamplingImputer(random_state: int | None = None)
Bases: BaseImputer
Impute by randomly sampling from observed values.
Similar to Hot Deck but without stratification. Preserves the empirical distribution of observed values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
random_state
|
int | None
|
Random seed for reproducibility. Default: None |
None
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import RandomSamplingImputer
>>> df = pd.DataFrame({'a': [1, 2, np.nan, 4, np.nan, 6]})
>>> imputer = RandomSamplingImputer(random_state=42)
>>> imputed = imputer.impute(df)
Initialize the random sampling imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
random_state
|
int | None
|
Random seed |
None
|
Source code in src/imputation_methods/sampling.py
impute
¶
Impute by random sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |
Source code in src/imputation_methods/sampling.py
HotDeckImputer
¶
Bases: BaseImputer
Impute missing values using the Hot Deck method.
This approach fills missing entries by randomly sampling existing values
("donors") from the same column. Optionally, sampling can be restricted to
rows sharing the same values in stratify_cols.
Initialize the imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stratify_cols
|
list[str] | None
|
Columns used to define similarity groups. If
|
None
|
random_state
|
int | None
|
Seed controlling the random sampling of donors. |
None
|
Source code in src/imputation_methods/sampling.py
impute
¶
Fill missing values by sampling existing observations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with potential NaN values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe where NaNs are replaced by randomly sampled donors. |
Source code in src/imputation_methods/sampling.py
ColdDeckImputer
¶
ColdDeckImputer(reference_values: ReferenceValues | None = None, random_state: int | None = None)
Bases: BaseImputer
Cold deck imputation using predetermined reference values.
Uses values from a reference dataset or predetermined mapping to fill missing values. Useful when you have historical or domain knowledge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference_values
|
ReferenceValues | None
|
Dictionary mapping column names to reference values or a reference DataFrame. If dict, can map to scalar or array. Default: None (uses column median as fallback) |
None
|
random_state
|
int | None
|
Seed used when sampling from array reference values. Default: None |
None
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import ColdDeckImputer
>>> df = pd.DataFrame({'a': [1, np.nan, 3], 'b': [np.nan, 2, 3]})
>>> # Use predetermined values
>>> imputer = ColdDeckImputer(reference_values={'a': 2.5, 'b': 2.0})
>>> imputed = imputer.impute(df)
References
Traditional imputation method predating hot deck imputation. Uses external/historical data rather than current dataset.
Initialize the cold deck imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference_values
|
ReferenceValues | None
|
Reference values for imputation. |
None
|
random_state
|
int | None
|
Seed used when sampling from array reference values. |
None
|
Source code in src/imputation_methods/sampling.py
impute
¶
Impute using cold deck reference values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |