Skip to content

inverse_transform of SimpleImputer with empty features changes order of columns #27012

Description

@JoelHuberQC

Describe the bug

When one uses a SimpleImputer with keep_empty_features=False and add_indicator=True, then if a column has only missing values during fit, but has values during transform, then the inverse transform will shift all columns to the right of that column one to the left.

(Note that I found this while experimenting and is not something that we rely on being fixed)

Steps/Code to Reproduce

import numpy as np
from sklearn.impute import SimpleImputer

X1 = np.array(
    [
        [np.nan, 2.0, 3.0],
        [np.nan, 2.0, 3.0],
    ]
)

X2 = np.array(
    [
        [1.0, 2.0, 3.0],
        [1.0, 2.0, 3.0],
    ]
)

imputer = SimpleImputer(add_indicator=True)
imputer.fit(X1)
print(imputer.inverse_transform(imputer.transform(X2)))

Expected Results

I think the best way to go is to fill the missing columns with the value from missing_values.

[[nan 2. 3.]
 [nan 2. 3.]]

Actual Results

[[2. 3. 0.]
 [2. 3. 0.]]

Versions

System:
    python: 3.11.4
executable: [redacted]
   machine: macOS-13.4.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.3.0
          pip: 23.2
   setuptools: 68.0.0
        numpy: 1.25.1
        scipy: 1.11.1
       Cython: None
       pandas: 2.0.3
   matplotlib: 3.7.2
       joblib: 1.3.0
threadpoolctl: 3.2.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 12
         prefix: libopenblas
       filepath: [redacted]
        version: 0.3.23
threading_layer: openmp
   architecture: VORTEX

       user_api: openmp
   internal_api: openmp
    num_threads: 12
         prefix: libomp
       filepath: [redacted]
        version: None

Activity

  1. added
    Needs Triage
    on Aug 4, 2023
  2. Micky774 commented on Aug 11, 2023

    @Micky774
    Contributor

    Two bugs highlighted here:

    1. The indicator column (the final column of zeros) ought to be removed after the inverse
    2. The inverse transform knows what it removes, and so should be able to "put it back". Ideally, the inverse should behave as an actual inverse and fully undo the initial transformation.

    PRs are welcome for both.

  3. removed
    Needs Triage
    on Aug 11, 2023
  4. Kaustbh commented on Aug 17, 2023

    @Kaustbh

    Can I work on this issue ? I think I can solve this issue.

  5. Micky774 commented on Aug 17, 2023

    @Micky774
    Contributor

    Can I work on this issue ? I think I can solve this issue.

    Sure, we'd appreciate the help! Just keep in mind that there are two bugs which ought to be handled in two, separate PRs. The first is probably easier.

  6. vjoshi253 commented on Feb 11, 2024

    @vjoshi253
    Contributor

    Since the help wanted tag is here, I am assuming no one is working on this. I would take this up.

    @lorentzenchr @Micky774

  7. vjoshi253 commented on Feb 17, 2024

    @vjoshi253
    Contributor

    @lorentzenchr @Micky774 I have a fix for the above issue but I feel in general the current architecture of simple imputer is such that if you fit on some data and transform other data then it is prone to many errors.
    I can think of few additional test cases where things would fail if we fit and transform in this manner.

    I feel when we are transforming, we should re-calculate the statistics [number of missing feature for example] again rather than using them from fit time. I am opening a sperate issue for the new failing test cases I can think of to highlight the bigger issue.

  8. AlessandroCesaTs commented on Jan 28, 2026

    @AlessandroCesaTs

    Hi,
    I was still able to reproduce the issue on scikit-learn 1.8.0 (numoy 2.4.1).
    I noticed that PR #28729 attempted a fix but was closed when the fork was deleted.
    If that's ok, would be happy to work on it

  9. Shreecharana24 commented on Feb 3, 2026

    @Shreecharana24

    Thanks for confirming this is still reproducible.

    I can reproduce the issue on the current main branch.
    I’m working on a fix with a regression test; will open a draft PR once ready.

  10. added a commit that references this issue on Apr 19, 2026
    ec1b181
  11. added a commit that references this issue on Aug 9, 2026
    7173028
  12. added a commit that references this issue on Nov 6, 2026
    9e7e407
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions