I want to apply pd.cut as a transformer in a pipeline, like this:

numerical_preprocessing = Pipeline([
            ('cut_into_bins', FunctionTransformer(pd.cut, kw_args={'bins': [10, 100, 1000]})

However, I get an error: ValueError("Input array must be 1 dimensional") I could just write the same function over and over again with each column separately, but that looks like a terrible coding practice. Any thoughs on this?


1 Answer 1


I solved the problem by creating a wrapper around pd.cut, which then applies pd.cut using the apply method of DataFrame:

if isinstance(x, pd.Series):
    return pd.cut(x, bins_final, labels=labels, **kwargs)
elif isinstance(x, pd.DataFrame):
    return x.apply(pd.cut, args=(bins_final,), axis=0, labels=labels, **kwargs)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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