imputation-methods
42 missing-data imputation methods behind one pandas API. Swap mean imputation for KNN, MICE, a Kalman filter or low-rank matrix completion by changing one line, and compare them with the same evaluation code.
from imputation_methods import KNNImputer
completed = KNNImputer(n_neighbors=5).impute(df)
- One interface. Every imputer takes a numeric
DataFrameand returns a new one with the same index and columns. The input is never modified. - Broad coverage. Statistical, donor-based, time-series, nearest-neighbor, regression, iterative, matrix-completion, neural and ensemble methods.
- Light dependencies. NumPy, pandas, SciPy and scikit-learn only.
- Typed and tested. Inline type hints checked by mypy in strict mode, and tests on Python 3.10–3.14, on the newest and the oldest supported dependency versions.
Installation
pip install imputation-methods
The optional viz extra installs matplotlib and seaborn, used by the example
notebooks and scripts:
pip install "imputation-methods[viz]"
Requires Python 3.10 or newer.
Quick start
import numpy as np
import pandas as pd
from imputation_methods import KNNImputer, MeanImputer, knn_impute
df = pd.DataFrame(
{
"height": [170.0, 165.0, np.nan, 180.0, 175.0],
"weight": [65.0, np.nan, 70.0, 85.0, 78.0],
"age": [30.0, 25.0, 35.0, np.nan, 40.0],
}
)
mean_filled = MeanImputer().impute(df)
knn_filled = KNNImputer(n_neighbors=2).impute(df)
# Every imputer also has a functional shortcut.
same_as_knn = knn_impute(df, n_neighbors=2)
Imputers are configured in the constructor. Those with a random component accept
random_state for reproducible results.
Available methods
| Family | Imputers |
|---|---|
| Statistical | MeanImputer, MedianImputer, ModeImputer, ConstantImputer, QuantileImputer, TrimmedMeanImputer, EndOfDistributionImputer, GroupMeanImputer, IndicatorImputer |
| Donor sampling | RandomSamplingImputer, HotDeckImputer, ColdDeckImputer |
| Time series | LOCFImputer, NOCBImputer, ForwardFillFallbackImputer, InterpolationImputer, MovingAverageImputer, WeightedMovingAverageImputer, LinearTrendImputer, PolynomialTrendImputer, SeasonalImputer, KalmanFilterImputer |
| Nearest neighbors | KNNImputer, RadiusNeighborsImputer, LocalMeanImputer |
| Regression | RegressionImputer, StochasticRegressionImputer, PMMImputer (predictive mean matching), BayesianRidgeImputer, HuberImputer, RANSACImputer, GaussianProcessImputer |
| Iterative | MICEImputer, MissForestImputer, EMImputer |
| Matrix completion | SoftImputeImputer, PPCAImputer (probabilistic PCA) |
| Neural networks | AutoencoderImputer, GAINImputer (generative adversarial imputation) |
| Ensembles | HybridImputer (fallback chain), StackingImputer, BaggingImputer (bootstrap aggregating) |
EMImputer runs iterative chained-equation imputation rather than closed-form EM
for a multivariate normal distribution.
The API reference documents every class and its parameters.
Evaluating an imputation
When you have complete data, hide some values, impute them, and score only the cells you hid:
import numpy as np
import pandas as pd
from sklearn.datasets import load_diabetes
from imputation_methods import KNNImputer, MeanImputer, MICEImputer, mae, rmse
complete = load_diabetes(as_frame=True).data
rng = np.random.default_rng(0)
mask = rng.random(complete.shape) < 0.2
incomplete = complete.mask(mask)
imputers = {
"mean": MeanImputer(),
"knn": KNNImputer(n_neighbors=5),
"mice": MICEImputer(random_state=0),
}
for name, imputer in imputers.items():
completed = imputer.impute(incomplete)
true = pd.Series(complete.to_numpy()[mask])
pred = pd.Series(completed.to_numpy()[mask])
print(f"{name:>5}: RMSE={rmse(true, pred):.4f} MAE={mae(true, pred):.4f}")
Input requirements
- A
pandas.DataFramewith numeric columns, including pandas nullable dtypes such asInt64; missing values asNaNorpd.NA. Encode categorical columns before imputing.GroupMeanImputeris the exception: its grouping column may be non-numeric. - Time-series imputers use row order, so sort the data first.
- Columns without missing values are returned unchanged. Imputed columns are
floating point:
float32/float64keep their precision and nullable columns becomeFloat64. - Columns with no observed values are left as
NaN, except by imputers that fill in a constant you choose, such asConstantImputer.
Documentation
Full documentation, including guides on choosing a method and evaluating results: https://diogoribeiro7.github.io/imputation-methods/
The roadmap describes what is planned before 1.0.
The repository also has example scripts and Jupyter notebooks.
Contributing
Contributions are welcome. See the contributing guide for the development setup, and the code of conduct. Report security issues as described in the security policy.
Citation
If you use this library in research, please cite it. Citation metadata is in
CITATION.cff;
GitHub's "Cite this repository" button exports it as BibTeX or APA.
License
MIT. See LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file imputation_methods-0.2.0.tar.gz.
File metadata
- Download URL: imputation_methods-0.2.0.tar.gz
- Upload date:
- Size: 68.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fe0f5f8b802304405c25cd19aa416636a2a94e41c117f29f6bb7db7451cbc5b
|
|
| MD5 |
07336338e9ff1fe011f0c1c1586b2a26
|
|
| BLAKE2b-256 |
127f21179b9c37ce7fe41b12e5bc2093c04574225a9cd9037aec0e15c58f8f3b
|
Provenance
The following attestation bundles were made for imputation_methods-0.2.0.tar.gz:
Publisher:
release.yml on DiogoRibeiro7/imputation-methods
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
imputation_methods-0.2.0.tar.gz -
Subject digest:
4fe0f5f8b802304405c25cd19aa416636a2a94e41c117f29f6bb7db7451cbc5b - Sigstore transparency entry: 2868232380
- Sigstore integration time:
-
Permalink:
DiogoRibeiro7/imputation-methods@08345406bba5a799f2bb8a4fbf7e84432f4276ab -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/DiogoRibeiro7
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@08345406bba5a799f2bb8a4fbf7e84432f4276ab -
Trigger Event:
release
-
Statement type:
File details
Details for the file imputation_methods-0.2.0-py3-none-any.whl.
File metadata
- Download URL: imputation_methods-0.2.0-py3-none-any.whl
- Upload date:
- Size: 53.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa8bff85a8199c4d8ee2248b164d6217fe33072ed01c9c6c6b0a14caedac9d45
|
|
| MD5 |
608f132b34839e552b5619a3f7714daf
|
|
| BLAKE2b-256 |
b755c40d2a3c721c9f5603b035212554f9084562a0f4bcaee8147c813632d3a2
|
Provenance
The following attestation bundles were made for imputation_methods-0.2.0-py3-none-any.whl:
Publisher:
release.yml on DiogoRibeiro7/imputation-methods
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
imputation_methods-0.2.0-py3-none-any.whl -
Subject digest:
aa8bff85a8199c4d8ee2248b164d6217fe33072ed01c9c6c6b0a14caedac9d45 - Sigstore transparency entry: 2868232386
- Sigstore integration time:
-
Permalink:
DiogoRibeiro7/imputation-methods@08345406bba5a799f2bb8a4fbf7e84432f4276ab -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/DiogoRibeiro7
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@08345406bba5a799f2bb8a4fbf7e84432f4276ab -
Trigger Event:
release
-
Statement type: