Skip to content

set_output API do not preserve original dtypes for pandas #25560

Open

Description

@idanmoradarthas

Describe the bug

Following issue #24182,

When using the set_output with expected output to be a pandas' data frame, while converting tougher columns with different dtypes the output does not preserve the original dtype but the "common type" by numpy.

Possible workaround is to create transformer for each column, but it doesn't feel like the right approach.

In collaboration with @BenEfrati

Steps/Code to Reproduce

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



X = pd.DataFrame({
                  "age": [1, 2, 4],
                  "float": [1.5, 2.5, None],
                  })
X["age"] = X["age"].astype(np.uint8)
X.dtypes

# age        uint8
# float    float64
# dtype: object

simple_imputer = SimpleImputer(strategy='constant', fill_value=1).set_output(transform="pandas")
X_trans_partial = simple_imputer.fit_transform(X)
X_trans_partial.dtypes

# age      float64
# float    float64
# dtype: object

Expected Results

We'd expect the age column to keep being an unsigned 8-bit integer.

Actual Results

the age column is now a 64-bit floating point.

Versions

System:
    python: 3.9.12 (main, Apr  4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]
executable: C:\New folder\venv\Scripts\python.exe
   machine: Windows-10-10.0.19044-SP0

Python dependencies:
      sklearn: 1.2.1
          pip: 23.0
   setuptools: 58.1.0
        numpy: 1.24.2
        scipy: 1.10.0
       Cython: None
       pandas: 1.5.3
   matplotlib: None
       joblib: 1.2.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:\New folder\venv\Lib\site-packages\numpy\.libs\libopenblas64__v0.3.21-gcc_10_3_0.dll
        version: 0.3.21
threading_layer: pthreads
   architecture: Haswell
    num_threads: 12

       user_api: openmp
   internal_api: openmp
         prefix: vcomp
       filepath: C:\New folder\venv\Lib\site-packages\sklearn\.libs\vcomp140.dll
        version: None
    num_threads: 12

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:\New folder\venv\Lib\site-packages\scipy.libs\libopenblas-802f9ed1179cb9c9b03d67ff79f48187.dll
        version: 0.3.18
threading_layer: pthreads
   architecture: Haswell
    num_threads: 12

Activity

  1. crispinlogan commented on Feb 8, 2023

    @crispinlogan
    Contributor

    Hi I'm trying to look at understandable issues so I can improve my knowledge of scikit-learn, with a view of contributing (other than the 'good first issue' type issues), so spent a bit of time looking at this issue. So don't take what i say too seriously!

    Might there be some useful discussion about the impact of this in #24860, which raise some points about how ensuring the pandas dtypes is preserved for all transformers can lead to problems. However, in the case of SimpleImputer, a lot of those points are not relevant, so is this an example of where we could do something similar to #25102 ?

    P.S. If it's decided to do some work on this, I'd love to help if possible :)

  2. thomasjpfan commented on Feb 9, 2023

    @thomasjpfan
    Member

    SimpleImputer has it's own edge cases to deal with. Let's say that "age" actually has a missing value using pandas UInt8 dtype, the default mean strategy will require a float to hold the mean of 1.5 for age:

    import numpy as np
    import pandas as pd
    from sklearn.impute import SimpleImputer
    
    X = pd.DataFrame(
        {
            "age": pd.Series([1, 2, None], dtype="UInt8"),
            "float": [1.5, 2.5, None],
        }
    )
    simple_imputer = SimpleImputer().set_output(transform="pandas")
    X_trans_partial = simple_imputer.fit_transform(X)
    X_trans_partial.dtypes
    # age      float64
    # float    float64
    # dtype: object

    So if we go with preserving the dtype for SimpleImputer, we have to do it with some rules:

    1. If a feature has no missing values, then preserve the dtype.
    2. If a feature has missing values, then SimpleImputer will try it's best to preserve dtypes. Even with strategy="constant", when fill_value=5.5, then the output can not be uint8 because it can not hold 5.5.

    I am unsure if the semantics listed above are too complex or surprising for users. The current "always output float64" is a fairly simple rule.

  3. added
    Needs Decision - Include Feature
    and removed
    Needs Triage
    on Feb 9, 2023
  4. davydw commented on Feb 13, 2023

    @davydw

    @thomasjpfan

    which version of Sklearn are you using ?

    Screenshot 2023-02-13 at 11 37 06

    simple_imputer = SimpleImputer().set_output(transform="pandas")
    

    Screenshot 2023-02-13 at 11 37 23

  5. idanmoradarthas commented on Feb 13, 2023

    @idanmoradarthas
    Author

    @davydw you need sklearn: 1.2.1
    see python dependencies in the first comment.

  6. davydw commented on Feb 14, 2023

    @davydw

    Screenshot 2023-02-14 at 10 30 39

    Thanks @idanmoradarthas

    • I used set_output(trasnform="pandas") on my SimpleImputer to preserve the columns'names. Hence I was able to apply a ColumnTransformer afterwards
    • I also used set_output(trasnform="pandas") after the ColumnTransformer and I no longer need to do:
    X_preprocessed = pd.DataFrame(X, columns = preprocessor.get_feature_names_out()) 

    like I used to do.

    Super convenient 👏

  7. foster999 commented on Oct 25, 2024

    @foster999

    Just to add another use case where I've hit this issue: The sklearn interface for xgboost uses the pandas "category" type to infer that a column should be treated as categorical. I'm using a pipeline that is set to output pandas and the category transformer is set to return category type, but I get the whole dataframe back as object type.

    Pipeline(
            [
                (
                    "processing",
                    ColumnTransformer(
                        transformers=[
                            (
                                "num",
                                StandardScaler(),
                                numerical_features,
                            ),
                            (
                                "cat",
                                OrdinalEncoder(dtype=pd.CategoricalDtype),
                                categorical_features,
                            ),
                        ],
                        remainder="passthrough",
                    ),
                ),
                (
                    "model",
                    DummyClassifier(),
                ),
            ]
        )
    
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 Decision - Include Featuremodule:impute

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions