Skip to main content

imputation-methods

PyPI Python versions CI License: MIT Ruff

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 DataFrame and 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.DataFrame with numeric columns, including pandas nullable dtypes such as Int64; missing values as NaN or pd.NA. Encode categorical columns before imputing. GroupMeanImputer is 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/float64 keep their precision and nullable columns become Float64.
  • Columns with no observed values are left as NaN, except by imputers that fill in a constant you choose, such as ConstantImputer.

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

imputation_methods-0.2.0.tar.gz (68.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

imputation_methods-0.2.0-py3-none-any.whl (53.7 kB view details)

Uploaded Python 3

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

Hashes for imputation_methods-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4fe0f5f8b802304405c25cd19aa416636a2a94e41c117f29f6bb7db7451cbc5b
MD5 07336338e9ff1fe011f0c1c1586b2a26
BLAKE2b-256 127f21179b9c37ce7fe41b12e5bc2093c04574225a9cd9037aec0e15c58f8f3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for imputation_methods-0.2.0.tar.gz:

Publisher: release.yml on DiogoRibeiro7/imputation-methods

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file imputation_methods-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for imputation_methods-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa8bff85a8199c4d8ee2248b164d6217fe33072ed01c9c6c6b0a14caedac9d45
MD5 608f132b34839e552b5619a3f7714daf
BLAKE2b-256 b755c40d2a3c721c9f5603b035212554f9084562a0f4bcaee8147c813632d3a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for imputation_methods-0.2.0-py3-none-any.whl:

Publisher: release.yml on DiogoRibeiro7/imputation-methods

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page