I am new to sklearn pipelines and am using this post as a guide for my code:


I am trying to encode a categorical features using a transformation pipeline, but no matter what encoder I use, I get the same error. As far as I can tell from reading other posts, scikit-learn should be able to handle categorical variables as strings from version 0.20 or greater (namely with the OneHotEncoder.)

ValueError: could not convert string to float: 'Male'

Where I have entered xxxxxxxxxxx below replace with one of the following

from sklearn.pipeline import FeatureUnion
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import Imputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.decomposition import NMF
from sklearn.feature_extraction.text import TfidfVectorizer
import category_encoders as ce
from sklearn.preprocessing import OrdinalEncoder
from sklearn.pipeline import Pipeline
import pandas as pd
import numpy as np

# create example data
example_df = pd.DataFrame({'Sex':['Male','Female','Female'],'Survived':[1,1,0]})
X_train = example_df.drop('Survived', axis=1)
y_train = pd.DataFrame(example_df['Survived'])

# build example pipeline
cat_pipe = ("categorical_features", ColumnTransformer([
            ("categorical", Pipeline(steps=[
                ("impute_stage", Imputer(missing_values=np.nan, strategy="median")),
                ("label_encoder", xxxxxxxxxxx())]), ["Sex"]

example_pipeline = Pipeline(steps=[cat_pipe])

# fit pipeline
example_pipeline.fit(X_train, y_train)

# Name                    Version 
scikit-learn              0.20.3 
category_encoders         1.3.0
numpy                     1.16.2
pandas                    0.24.2

1 Answer 1


In this line:

("impute_stage", Imputer(missing_values=np.nan, strategy="median"))

Because your input type is string, you shouldn't fill the null value to median (we cannot average string value).

From the document, you can fill null value with a string constant like:

Imputer(missing_values=None, strategy="constant", fill_value="NULL")

to represent null value in your string field.

  • $\begingroup$ Thanks for the quick response! I thought I had put mode - turns out you should use "most frequent." My code now reads as follows: ``` ("impute_stage", Imputer(missing_values=np.nan, strategy="most_frequent")) ``` However I am still getting the same error as in the original post. Oddly when I implement strategy="constant" and fill_value="NULL" I get the following error: ``` TypeError: __init__() got an unexpected keyword argument 'fill_value' ``` $\endgroup$ Oct 6, 2019 at 3:46
  • $\begingroup$ Oh sorry, please check whether your null value type is None. If so, we should change the imputer to Imputer(missing_values=np.nan, strategy="constant", fill_value="NULL") $\endgroup$
    – Bruce Kuo
    Oct 6, 2019 at 4:00
  • $\begingroup$ I do agree that the problem is somewhere with the imputer - as if I remove it the encoder works as expected. It turns out that if I replace Imputer with SimpleImputer the pipeline works as expected! Thanks for getting me in the right direction. $\endgroup$ Oct 6, 2019 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.