Repository navigation
Migrate SklearnWrapper to narwhals, add polars support - #1067
Merged
Merged
Conversation
solegalli
force-pushed
the
narwhals-sklearn-wrapper
branch
from
September 19, 2026 09:33
e576cee to
05e9a73
Compare
solegalli
force-pushed
the
narwhals-sklearn-wrapper
branch
2 times, most recently
from
November 21, 2026 09:53
4900e55 to
621c483
Compare
SklearnWrapper (and its deprecated alias SklearnTransformerWrapper) now accepts pandas, polars and other narwhals-supported dataframes, and returns the same library it receives. - pandas keeps native inputs to scikit-learn; the wrapped transformer is set to return pandas output, which makes creators faster. - other backends pass numpy arrays to scikit-learn (None in text columns becomes NaN, as with pandas) and rebuild the output with narwhals, turning NaN back into null. - inverse_transform no longer modifies the input dataframe. - tests rewritten to the make_df conventions; user guide gains a polars example and fixes outdated examples. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
SklearnWrapper binds X = check_X(X) and no longer checks which dataframe library it receives. The code that depends on the library moved to feature_engine/wrappers/_wrappers_helpers.py: setting the scikit-learn output container, passing the variables to the transformer, and replacing or adding the transformer's output. Column reordering uses subset_df. Outputs are the same as before on pandas and polars. pandas keeps set_output and the column assignment into a shallow copy: replacing the columns with a concat was 1.5 to 2.7 times slower on pandas 2.3. Add a test for OneHotEncoder with pandas integer column names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The wrapper passes the selected variables to the scikit-learn transformer in the user's dataframe library, always asks it for numpy output, and puts the result back with the shared dataframe helpers. The wrappers' own helpers module is removed, and the wrapper no longer turns polars nulls into NaN before scikit-learn or NaN back into null after it. With pandas the outputs are the same as on main. With polars, missing numbers come back as NaN, and scikit-learn does not treat nulls in text variables as missing values. The user guide explains this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
solegalli
force-pushed
the
narwhals-sklearn-wrapper
branch
from
November 22, 2026 15:17
621c483 to
34ba9a5
Compare
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
solegalli
force-pushed
the
narwhals-sklearn-wrapper
branch
from
November 22, 2026 15:27
c411e86 to
19a8eae
Compare
Test all the transformers and selectors that the old tests wrapped, with a variable list and with variables=None, against scikit-learn's own output on the same data. Also bring back SimpleImputer and OneHotEncoder on all variables, constant imputation of numbers, inverse_transform with variables=None, and feature names for all the transformers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SklearnWrapper(and the deprecatedSklearnTransformerWrapper) now works with pandas, polars and any other eager dataframe supported by narwhals, and returns the library it receives. It applies the scikit-learn transformer to the selected variables and returns what scikit-learn returns.set_output(transform="default"), so a global scikit-learn config can't change what the wrapper gets back.replace_columns_in_df,add_columns_to_dfandsubset_df. The wrapper has no library-specific code and doesn't import pandas.inverse_transformno longer modifies the user's dataframe.Behaviour
Speed
Fit is the same or faster than main. Transform is faster than main on pandas 3. On pandas 2.3, transformers that replace columns are up to 2x slower than main (e.g. StandardScaler on 5 of 21 columns, 500k rows: 9.9 -> 17.1 ms), because
replace_columns_in_dfusesconcat, which copies the dataframe on pandas 2.3. That's for a separate PR on the helper.Tests and docs
Needs decision
Got {transformer} instead.(wasgot ... instead.).Known issues, not fixed
Objectcolumn instead ofStringwhen a transformer returns text whose first value is missing.OrdinalEncoder.inverse_transformfails on pandas 3 when the column has NaN; scikit-learn alone fails the same way.