Skip to content

KNNImputer returns incorrect feature names with keep_empty_features=True #35610

Backlog

Description

@Mohit-25-tech

Warning

This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.

Introduce yourself

I am Mohit Nirmal , currently in BTech AI and ML.I have experience using scikit-learn for preprocessing , classification and model evaluation.I have buildd 3-4 projects using sklearn.

Describe the bug and give evidence about its user-facing impact

KNNImputer retains completely missing columns when keep_empty_features = True , but get_feature_names_out() still excludes their names.

This will create mismatch between the transformed data and feature names , causing an error when pandas output is enabled,It also affects pipeline and columnTranformer workflows.

Yhis bug is reproduced on sklearn 1.9.1 and 1.10.0rc1.

Steps/Code to Reproduce

import numpy as np
import pandas as pd
from sklearn.impute import KNNImputer

X = pd.DataFrame({
"a": [1.0, 2.0, np.nan],
"b": [np.nan, np.nan, np.nan],
"c": [3.0, 4.0, 5.0]
})

imputer = KNNImputer(keep_empty_features=True)

result = imputer.fit_transform(X)

print(result.shape)
print(imputer.get_feature_names_out())

imputer.set_output(transform="pandas")
print(imputer.transform(X))

Expected Results

(3, 3)
['a' 'b' 'c']

Actual Results

(3, 3)
['a' 'c']

ValueError: Length mismatch: Expected axis has 3 elements, new values have 2 elements

Versions

System:
    python: 3.13.16 (main, Oct  1 2026, 15:40:24) [GCC 13.3.0]
executable: /usr/bin/python3
   machine: Linux-6.18.55+-x86_64-with-glibc2.39

Python dependencies:
      sklearn: 1.10.0rc1
          pip: 26.2.2
   setuptools: 81.0.0
        numpy: 2.1.3
        scipy: 1.16.3
       Cython: 3.0.12
       pandas: 2.3.3
   matplotlib: 3.10.0
       joblib: 1.6.0
threadpoolctl: 3.7.0
     narwhals: 2.27.1

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 4
         prefix: libscipy_openblas
       filepath: /usr/local/lib/python3.13/dist-packages/scipy.libs/libscipy_openblas-b75cc656.so
        version: 0.3.29.dev
threading_layer: pthreads
   architecture: SkylakeX

       user_api: openmp
   internal_api: openmp
    num_threads: 4
         prefix: libgomp
       filepath: /usr/local/lib/python3.13/dist-packages/scikit_learn.libs/libgomp-6a29c32d.so.1.0.0
        version: None

       user_api: blas
   internal_api: openblas
    num_threads: 4
         prefix: libscipy_openblas
       filepath: /usr/local/lib/python3.13/dist-packages/numpy.libs/libscipy_openblas64_-ff651d7f.so
        version: 0.3.27
threading_layer: pthreads
   architecture: SkylakeX

Interest in fixing the bug

in sklearn/impute/_knn.py , get_feature_names_out uses names = input_features[self._valid_mask]

This filters out empty feature names without checking keep_empty_features.

The possible solution is :

if self.keep_empty_features:
names = input_features
else:
names = input_features[self._valid_mask]

I will wait for the maintainer's review to go forward .I want to work on this issue.

AI Disclosure :

I have taken help of antigravity coding assistant to find this bug and then reproduce the entire bug in the kaggle platform .

Activity

  1. added
    Needs Triage
    on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions