Repository navigation
Open
Labels
Description
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: objectExpected 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: 12Reacted by Santiago Castro
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDiscussion
Activity
crispinlogan commentedon Feb 8, 2023
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 :)
thomasjpfan commentedon Feb 9, 2023
SimpleImputer has it's own edge cases to deal with. Let's say that
"age"actually has a missing value using pandasUInt8dtype, the defaultmeanstrategy will require a float to hold the mean of1.5for age:So if we go with preserving the dtype for
SimpleImputer, we have to do it with some rules:SimpleImputerwill try it's best to preserve dtypes. Even withstrategy="constant", whenfill_value=5.5, then the output can not beuint8because it can not hold5.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.
davydw commentedon Feb 13, 2023
@thomasjpfan
which version of Sklearn are you using ?
idanmoradarthas commentedon Feb 13, 2023
@davydw you need sklearn: 1.2.1
see python dependencies in the first comment.
davydw commentedon Feb 14, 2023
Thanks @idanmoradarthas
set_output(trasnform="pandas")on mySimpleImputerto preserve the columns'names. Hence I was able to apply aColumnTransformerafterwardsset_output(trasnform="pandas")after theColumnTransformerand I no longer need to do:like I used to do.
Super convenient 👏
foster999 commentedon Oct 25, 2024
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.