Skip to content

SklearnTransformerWrapper fails when FunctionTransformer used with non-numerical columns #416

@david-cortes

Description

@david-cortes

The SklearnTransformerWrapper will throw an error if I try to use it for non-numerical columns, even though some scikit-learn transformers are able to handle non-numerical columns.

Consider the following example:

import numpy as np, pandas as pd
from sklearn.preprocessing import FunctionTransformer
from feature_engine.wrappers import SklearnTransformerWrapper

df = pd.DataFrame({
    "col1" : ["1", "2", "3"],
    "col2" : ["a", "b", "c"]
})

pipeline = SklearnTransformerWrapper(
    FunctionTransformer(lambda x: x.astype(np.float64)),
    variables=["col1"]
)
pipeline.fit(df)
TypeError                                 Traceback (most recent call last)
<ipython-input-1-88807bab80e9> in <module>
     12     variables=["col1"]
     13 )
---> 14 pipeline.fit(df)

~/anaconda3/envs/py3/lib/python3.9/site-packages/feature_engine/wrappers/wrappers.py in fit(self, X, y)
    122 
    123         else:
--> 124             self.variables_ = _find_or_check_numerical_variables(X, self.variables)
    125 
    126         self.transformer_.fit(X[self.variables_], y)

~/anaconda3/envs/py3/lib/python3.9/site-packages/feature_engine/variable_manipulation.py in _find_or_check_numerical_variables(X, variables)
     75         # check that user entered variables are of type numerical
     76         if any(X[variables].select_dtypes(exclude="number").columns):
---> 77             raise TypeError(
     78                 "Some of the variables are not numerical. Please cast them as "
     79                 "numerical before using this transformer"

TypeError: Some of the variables are not numerical. Please cast them as numerical before using this transformer

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions