Skip to content

Migrate SklearnWrapper to narwhals, add polars support - #1067

Merged
solegalli merged 7 commits into
narwhals-migrationfrom
narwhals-sklearn-wrapper
Nov 22, 2026
Merged

solegalli merged 7 commits into
narwhals-migrationfrom
narwhals-sklearn-wrapper

Conversation

@solegalli

@solegalli solegalli commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

SklearnWrapper (and the deprecated SklearnTransformerWrapper) 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.

  • The selected variables go to scikit-learn in the user's library. The inner transformer is set to set_output(transform="default"), so a global scikit-learn config can't change what the wrapper gets back.
  • The numpy output goes back with the shared helpers replace_columns_in_df, add_columns_to_df and subset_df. The wrapper has no library-specific code and doesn't import pandas.
  • Bug fix: inverse_transform no longer modifies the user's dataframe.

Behaviour

  • pandas: same output as main (values, dtypes, column order, index).
  • polars: what scikit-learn returns. Missing numbers come back as NaN, and scikit-learn doesn't treat nulls in text columns as missing: imputers leave them, encoders treat them as a category.

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_df uses concat, which copies the dataframe on pandas 2.3. That's for a separate PR on the helper.

Tests and docs

  • Tests rewritten to the repo conventions, on both backends. All transformers and selectors from the old tests are kept, compared with scikit-learn's own output, without the California housing download. 214 tests pass; the full suite has the same 8 failures as the base branch.
  • User guide: new polars section, and fixes to examples that no longer ran on scikit-learn >= 1.7 or pandas 3.

Needs decision

  • The TypeError message now ends Got {transformer} instead. (was got ... instead. ).

Known issues, not fixed

  • pandas integer column names fail with OneHotEncoder and PolynomialFeatures, as on main.
  • polars builds an Object column instead of String when a transformer returns text whose first value is missing.
  • OrdinalEncoder.inverse_transform fails on pandas 3 when the column has NaN; scikit-learn alone fails the same way.

@solegalli
solegalli force-pushed the narwhals-sklearn-wrapper branch from e576cee to 05e9a73 Compare September 19, 2026 09:33
@solegalli
solegalli force-pushed the narwhals-sklearn-wrapper branch 2 times, most recently from 4900e55 to 621c483 Compare November 21, 2026 09:53
solegalli and others added 4 commits November 22, 2026 16:10
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
solegalli force-pushed the narwhals-sklearn-wrapper branch from 621c483 to 34ba9a5 Compare November 22, 2026 15:17
solegalli and others added 2 commits November 22, 2026 16:19
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@solegalli
solegalli force-pushed the narwhals-sklearn-wrapper branch from c411e86 to 19a8eae Compare November 22, 2026 15:27
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>
@solegalli
solegalli merged commit 226c3b4 into narwhals-migration Nov 22, 2026
4 of 10 checks passed
@solegalli
solegalli deleted the narwhals-sklearn-wrapper branch November 22, 2026 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant