scikit-learn "ValueError: Input contains NaN" in CI
A scikit-learn estimator validated its input with check_array and found NaN or infinity in data that the estimator cannot handle. The fit or transform aborts before any model is produced.
What this error means
A training or test step fails with "ValueError: Input contains NaN" (older versions: "Input contains NaN, infinity or a value too large for dtype('float64')"). It often appears only in CI when the fixture data differs.
ValueError: Input contains NaN.
LogisticRegression does not accept missing values encoded as NaN natively.Common causes
Missing values reach an estimator that rejects them
Most linear models and many estimators call check_array(..., force_all_finite=True), so a single NaN in the feature matrix raises before fitting.
A division or join produced NaN upstream
A merge, a divide-by-zero, or a parsed column with empty cells introduces NaN that only the CI dataset exposes.
How to fix it
Impute before fitting
Insert a SimpleImputer (or fill values) ahead of the estimator in a pipeline so NaN never reaches it.
from sklearn.impute import SimpleImputer
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
clf = make_pipeline(SimpleImputer(strategy="mean"), LogisticRegression())
clf.fit(X, y)Locate the NaN source
- Print where NaN appears with
np.isnan(X).any(axis=0)to find the column. - Fix the upstream computation or drop/fill the offending rows.
- Re-run the fit once the input is finite.
import numpy as np
print("cols with NaN:", np.where(np.isnan(X).any(axis=0))[0])How to prevent it
- Put imputation inside the pipeline so train and test share it.
- Assert finiteness of inputs in a data-validation test.
- Use deterministic CI fixtures so NaN handling is exercised the same way.