Skip to content

category_encoders/wrapper-fit-returns-none

Pack Severity Fix Versions
Category Encoders error unsafe, with xpd check --fix --unsafe-fixes All versions

fit of PolynomialWrapper and NestedCVWrapper returns None, so calling a method on its result raises AttributeError. Call fit on one line and the method on the next, or call fit_transform.

encoded = PolynomialWrapper(ce.TargetEncoder(cols=["city"])).fit(X, y).transform(X)
wrapper = PolynomialWrapper(ce.TargetEncoder(cols=["city"]))
wrapper.fit(X, y)
encoded = wrapper.transform(X)

The rule reports a method call on the result of fit of a PolynomialWrapper or a NestedCVWrapper, and a name assigned from that fit. Their fit has no return statement, so it returns None where every scikit-learn estimator returns itself.

When the wrapper is assigned on a separate line, its assignment and the fit call must be in the same function or both at module level. The rule skips error-handling paths such as pytest.raises.

With --unsafe-fixes, xpd uses fit_transform(X, y) when the chained transform uses the same training data as fit.

The old form raised AttributeError: 'NoneType' object has no attribute 'transform' on both wrappers. fit_transform returns the encoded frame on both and is left alone.

fit of $W returns None on category_encoders 2.11.2, so transform on its result raises AttributeError. Use fit_transform for the training data.

See all rules in the Category Encoders pack.