Skip to content

[MNT] narwhals migration - #965

Open
solegalli wants to merge 85 commits into
mainfrom
narwhals-migration
Open

solegalli wants to merge 85 commits into
mainfrom
narwhals-migration

Conversation

@solegalli

Copy link
Copy Markdown
Collaborator

No description provided.

@ojassharma7

Copy link
Copy Markdown
Contributor

Hi @solegalli — I'd like to help with the narwhals migration.

If it is still free, I can take feature_engine/scaling first (small surface: mainly MeanNormalisationScaler) as a single-module PR, following the dataframe_checks pattern from #966.

Please let me know if that module is already spoken for — happy to pick another (e.g. a simpler preprocessing piece) instead.

@solegalli

Copy link
Copy Markdown
Collaborator Author

That is actually a good one to start with. The tests should pass with pandas. I am not sure they will pass with polars because we need to change the functions that select variables, on which I am working on right now and will soon make a PR.

@ojassharma7

Copy link
Copy Markdown
Contributor

Started on scaling as discussed — opened a PR against this branch: will link here once created (see latest open PR from @ojassharma7 titled migrate scaling module to narwhals).

Pandas tests for the module pass locally. As you said, polars may still need your variable-selection updates.

@ojassharma7 ojassharma7 mentioned this pull request Jul 29, 2026
1 of 2 tasks
@ojassharma7

Copy link
Copy Markdown
Contributor

Scaling PR: #979

@solegalli
solegalli force-pushed the narwhals-migration branch 3 times, most recently from 8fe8359 to ea95750 Compare July 31, 2026 12:30

@FBruzzesi FBruzzesi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @solegalli - Following up on my discord comment. I added a few comments, mostly focusing on the changes in feature_engine/dataframe_checks.py - I hope you find them helpful

I noticed that a lot of tests were refactored as well: if you want to test the same behavior for many dataframes, I would reference what I did for fairlearn (see their conftest file), namely create fixture dataframe constructor for all the dataframe types you want to test. Ideally I would like to move that into narwhals as well (see narwhals-dev/narwhals#3552), but that's still work-in-progress and under discussion 🙏🏼

Comment thread feature_engine/dataframe_checks.py Resolved
Comment thread feature_engine/dataframe_checks.py Resolved
Comment thread feature_engine/dataframe_checks.py Resolved
elif isinstance(y, pd.DataFrame):
if y.isnull().any().any():
if nw_y.dtype.is_numeric():
if not np.isfinite(nw_y.to_numpy()).all():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not np.isfinite(nw_y.to_numpy()).all():
if not nw_y.is_finite().all():

(see Series.is_finite())

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @FBruzzesi , thanks for the suggestion. It seems that using numpy is faster than using narwhals both for pandas and polars (mostly so for pandas). Is this a known issue?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • For the polars case we run its native functionality polars.Series.is_finite. I am surprised that's faster than numpy, at least at scale
  • For pandas-like, we do (s > float("-inf")) & (s < float("inf")). IIRC that's to avoid using numpy with non-numpy backed series (e.g. pyarrow backed series, cudf series that live in the GPU, etc). If the delta is large at scale, we can take a look for a refactor with performance in mind.

For context: in general we tend to use the native dataframe libraries API/functionalities. pandas is a special kid as we need to do quite some gymnastic for null vs nan's, its datatype system, its multiple backends, etc..

So please keep reporting these kind of performance issues - we aim to keep overhead at the minimum

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for replying so quickly. These are the values I've got (on pandas and polars, 200k rows × 20 cols):

Check pandas polars
null check (multi-col) narwhals-native 1.2x slower narwhals-native 4x slower
inf check (multi-col) narwhals-native 2.4x slower narwhals-native 1.3x slower
is_finite (single series) narwhals-native 10x slower ~same

is_finite is the same for polars, the inf and null checks make it a bit slower respect to numpy.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For pandas I just opened a PR to use numpy/cupy/pyarrow.compute native functionalities directly: see narwhals-dev/narwhals#3874

For polars, I cannot tell why numpy is faster than their native implementation - If interested, you can double check with them either in discord or in their repo

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

Comment thread feature_engine/dataframe_checks.py Outdated

if nwd.is_into_dataframe(y):
nw_y = nw.from_native(y, eager_only=True)
if nw_y.select(nw.all().is_null().any()).to_numpy().any():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid casting to numpy:

Suggested change
if nw_y.select(nw.all().is_null().any()).to_numpy().any():
if nw_y.select(nw.any_horizontal(nw.all().is_null().any())).item():

Comment thread feature_engine/dataframe_checks.py Outdated
"`missing_values='ignore'` when initialising this transformer."
)
nw_X = nw.from_native(X, eager_only=True)
if nw_X.select(nw.col(variables).is_null().any()).to_numpy().any():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar as above, you can use any_horizontal

solegalli added a commit that referenced this pull request Aug 24, 2026
* address review feedback on dataframe_checks.py

Follow-up to FBruzzesi's review on PR #965:

- Clarify docstrings for check_X, check_y, check_X_y in terms of which
  dataframe libraries are safe to pass in (pandas, polars, PyArrow, modin,
  cuDF), instead of narwhals-specific "eager" terminology.
- Use narwhals' IntoDataFrameT instead of IntoDataFrame for check_X and
  check_X_y, since both return the same concrete dataframe type they
  receive.
- Fix a null/NaN detection bug: in polars, is_null() does not catch an
  explicit float("nan") value (only None counts as null), so check_y and
  _check_contains_na could silently miss NaNs in polars data. Now also
  check is_nan() for numeric columns/series, keeping numpy for the
  finite/inf checks since it benchmarks as fast or faster there.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* inline null/nan checks to match FBruzzesi's suggested one-liner

Collapses the has_na/has_null/has_nan accumulator variables into a single
short-circuiting if-condition, as suggested in review. This also avoids an
unnecessary is_nan() call when is_null() already found a null value.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* speed up numeric column detection in _check_contains_na

The schema-based list comprehension rebuilt narwhals' full column
schema on every single-column access, making it scale roughly
quadratically with column count on pandas (benchmarked up to ~500x
slower than necessary at 200 columns). Switch to the pandas fast-path /
narwhals-selector pattern already used in variable_handling
(find_numerical_variables, check_numerical_variables) for the same
"which of these columns are numeric" problem.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
solegalli and others added 6 commits August 24, 2026 12:12
* update dataframe checks

* update dataframe checks take 2

* update dataframe checks take 3

* update docstrings

* refactor dataframe checks

* fix mypy error

* add missing type hints

* add missing matching error syntax

* finalise tests for df checks'
The project already requires scikit-learn>=1.7.0 (pyproject.toml,
tox.ini, .circleci/config.yml), so the sklearn<=1.6 branches of every
check_estimator/tags conditional were dead code. This removes them,
keeping only the >=1.6 branch (the one using
check_estimator(expected_failed_checks=...)):

- feature_engine/tags.py: collapse the sklearn_version > 1.6 check in
  _return_tags(), the shared helper used across ~20 estimator classes.
- 11 tests/**/test_check_estimator_*.py files: collapse each
  if/else on sklearn_version vs 1.6, drop the now-unused sklearn/
  parse_version imports and sklearn_version variables.
- tests/test_prediction/test_check_estimator_prediction.py: this file
  had no >=1.6 branch, only the dead <1.6 one (its own TODO already
  flagged this). Removing it leaves the prediction module with no
  test_check_estimator_from_sklearn coverage - a pre-existing gap,
  not introduced by this change, left as a follow-up.
- tests/test_creation/test_geo_features.py: __sklearn_tags__ always
  exists at sklearn>=1.7, so drop the hasattr() guard around it.
- tests/test_wrappers/test_sklearn_wrapper.py: also collapse the
  _OneHotEncoder() test helper's sparse/sparse_output branch (sklearn
  <1.2 compat, dead for the same reason). The separate
  KBinsDiscretizer(quantile_method=...) branch (sklearn<1.7) is
  intentionally left as-is - different threshold, out of scope here.
- tests/check_estimators_with_parametrize_tests.py: delete entirely.
  A standalone, non-CI reference file documenting the pre-1.6
  parametrize_with_checks() call signature.

_more_tags()/__sklearn_tags__() method definitions are untouched:
_more_tags() is feature_engine's own internal metadata/xfail-checks
store (read by tests/estimator_checks/*.py), not a legacy sklearn
shim, and __sklearn_tags__() is the current sklearn API.

Verified: identical test suite pass/fail counts before and after
(2010 passed, 114 failed - all 114 are pre-existing narwhals-migration
WIP failures unrelated to this change), flake8 and mypy clean (the one
remaining mypy error is pre-existing in datetime_subtraction.py,
unrelated to this PR).
* address review feedback on dataframe_checks.py

Follow-up to FBruzzesi's review on PR #965:

- Clarify docstrings for check_X, check_y, check_X_y in terms of which
  dataframe libraries are safe to pass in (pandas, polars, PyArrow, modin,
  cuDF), instead of narwhals-specific "eager" terminology.
- Use narwhals' IntoDataFrameT instead of IntoDataFrame for check_X and
  check_X_y, since both return the same concrete dataframe type they
  receive.
- Fix a null/NaN detection bug: in polars, is_null() does not catch an
  explicit float("nan") value (only None counts as null), so check_y and
  _check_contains_na could silently miss NaNs in polars data. Now also
  check is_nan() for numeric columns/series, keeping numpy for the
  finite/inf checks since it benchmarks as fast or faster there.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* inline null/nan checks to match FBruzzesi's suggested one-liner

Collapses the has_na/has_null/has_nan accumulator variables into a single
short-circuiting if-condition, as suggested in review. This also avoids an
unnecessary is_nan() call when is_null() already found a null value.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* speed up numeric column detection in _check_contains_na

The schema-based list comprehension rebuilt narwhals' full column
schema on every single-column access, making it scale roughly
quadratically with column count on pandas (benchmarked up to ~500x
slower than necessary at 200 columns). Switch to the pandas fast-path /
narwhals-selector pattern already used in variable_handling
(find_numerical_variables, check_numerical_variables) for the same
"which of these columns are numeric" problem.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
* update variable handling module for narwahls

* creating own datetime parser

* Improve readability of narwhals date/type-check helpers, add missing tests

Replace the double-parse-with-disagreeing-defaults trick in
_looks_like_date_string with a direct call to dateutil's parser()._parse(),
which exposes which date/time fields were actually found in a string without
needing to approximate it - this also drops the now-unneeded sentinel
default datetimes and the defensive str() coercion at its call site. Make
truthiness checks and compound boolean returns explicit throughout the
module, and restore the pre-narwhals function names that PR #978 had
prefixed with _nw_ for no continuing reason.

Rename test_fe_type_checks.py to test_variable_type_checks.py to match the
module it tests, add docstrings, and add coverage for _looks_like_date_string
and _is_categories_num, the two functions that previously had no direct
tests.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* Replace per-column schema access with bulk narwhals selectors for speed

nw_X.schema is not cached - every access re-derives the full schema from
the underlying native dataframe, so checking dtype-based conditions
(is_numeric(), native Date/Datetime, categorical/enum/string) one column
at a time inside a loop was quadratic instead of linear. Replace each such
loop with a single nw_df.select(<selector>).columns call converted to a
set, then a plain membership test per column - confirmed old vs new give
identical results, and measured 8x-120x speedups depending on backend and
column count. Also use by_dtype(Date, Datetime) to bulk-detect native
datetime columns in one pass, only falling back to the expensive
per-value _is_categorical_and_is_datetime check for columns that aren't
already known to be numeric or natively datetime. Drop the now-unused
_is_date_or_datetime import from both files.

Simplify _looks_like_date_string's comment to link directly to the pandas
source it mirrors, and instantiate dateutil's parser() per call instead of
reusing a module-level instance.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* refactor find variables:

* refactor check_variables

* final refactor of find and check variables

* finalise migration of variable handling module

* update user guide

* Trim backend-difference notes from docs, revert datetime.py out of scope

Removes the trailing pandas/polars note blocks from the check/find
categorical and datetime variable docs, keeping them focused on the
walkthrough. Reverts feature_engine/datetime/datetime.py to main - the
DatetimeFeatures index-datetime fix needed there for the narwhals
migration belongs in a separate datetime-module PR.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
solegalli and others added 6 commits August 24, 2026 17:00
* Migrate creation/mixins shared base classes to narwhals, remove all pandas imports

BaseCreation, BaseNumericalTransformer, and mixins.py (TransformXyMixin,
FitFromDictMixin, GetFeatureNamesOutMixin) are used by every transformer in
the creation module, so their remaining pandas-only code blocked a
polars-only install regardless of which transformer was migrated. Adds
pandas fast paths (benchmarked ~2-11x) alongside narwhals-generic branches,
replaces y.loc[X.index] row alignment in TransformXyMixin with a
narwhals with_row_index()-based mechanism for non-pandas backends, and adds
test_base_creation.py plus polars coverage for transform_x_y.

* Apply suggestion from @FBruzzesi

* Fix test_get_feature_names_out_mixin.py after to_list() removal, add process rules to AGENTS.md

The 48 failures here were pre-existing (unrelated to the to_list() fix,
confirmed identical before/after): check_X no longer accepts raw numpy
arrays, and most of this file's tests fit() on df_vartypes.to_numpy() or
feed a raw-array-outputting sklearn transformer upstream. Fixes:
- array-input tests converted to set feature_names_in_/n_features_in_
  directly, since that's the only way left to reach the mixin's x0/x1/...
  naming branch (fit() rejects arrays outright now).
- SimpleImputer/PolynomialFeatures steps get .set_output(transform="pandas")
  so they hand a dataframe to the next pipeline step instead of an array -
  this is also the fix any real user chaining sklearn + feature-engine
  transformers in a Pipeline now needs.
- pure Mock-only tests (no sklearn transformer involved) parametrized over
  pandas and polars.
Also adds two AGENTS.md rules: run a changed function/class's tests and
resolve any failures, and keep user-guide docs in sync with new
transformer functionality.

* Remove dead array-input branch from GetFeatureNamesOutMixin

This branch handled feature_names_in_ == ["x0", "x1", ...], the naming
sklearn gives an estimator fit on a raw array. check_X no longer accepts
arrays (dataframe-only input, per AGENTS.md), so fit() can never produce
that pattern anymore - the branch, its indices=True path in
_remove_feature_names, and get_support(indices=True) were all unreachable.
It was also a latent correctness gap: a dataframe with columns genuinely
named x0..xn would have hit this branch and skipped the usual
input_features-must-match-feature_names_in_ validation.

Verified via git history (#519, 2022) this was built for the old
array-accepting check_X; confirmed no other code in the library still
generates x0/x1/... names. Removed the branch, its now-single-path
_remove_feature_names, and the tests that existed only to reach it -
replaced by tests/test_base_transformers/test_get_feature_names_out_mixin.py's
remaining pandas+polars dataframe coverage, which already exercises the
same validation/renaming logic through the one reachable path.
* Migrate CyclicalFeatures to narwhals, add polars support

fit(): unified across backends via .to_numpy().max(axis=0) instead of
pandas' .max().to_dict() (~1.55x faster for pandas, ~1.28x for polars,
benchmarked). .tolist() keeps the returned dict's values as plain Python
int/float, matching the old .to_dict() dtype.

transform(): kept as two branches rather than one narwhals-only path -
benchmarked running narwhals expressions against a pandas-backed frame and
it was consistently 1.24x-2.06x slower than the pandas-native loop across
variable counts and row counts, worse at small scale. The pandas branch is
therefore left as the original, unmodified loop (an earlier numpy-vectorized
version of it was only a 1.0x-1.4x gain, not worth it once the branches
stay separate anyway). The narwhals branch uses column expressions, the
only approach that stayed competitive with pandas-native as variable count
grows (a numpy-array round-trip loses to expressions on polars once there
is more than 1 variable).

Verified no legacy numpy-array-input code remains in this file or its base
classes. Tests rewritten to parametrize pandas and polars via make_df;
error-matching tightened per AGENTS.md except where the message
legitimately differs by backend. Docstring and user-guide example gained a
polars walkthrough per the new AGENTS.md doc-sync rule.

* unify pandas/polars branches

* Fix style/docs failures on top of the pandas/polars branch unification

Style: removed the now-unused narwhals.dependencies import (flake8 F401)
left over from dropping the is_pandas_dataframe branch. Also fixed 7
pre-existing flake8 issues (line length, unused variable) in
test_get_feature_names_out_mixin.py that predate this branch.

Docs: docs/user_guide/creation/CyclicalFeatures.rst's polars output block
was under `.. code:: python`, and Sphinx's Pygments highlighter can't lex
the box-drawing table as Python (misc.highlighting_failure), which -W
promotes to a build error. Switched to `.. code:: text`, matching the
convention already used elsewhere (PowerTransformer.rst, MeanImputer.rst)
for output-only blocks. Pre-existing bug in my own doc addition, unrelated
to the branch unification.

Two correctness issues surfaced by testing the unification:
- max_values_ lost its .tolist() call, so it held numpy scalars
  (np.int64) instead of plain Python int/float - restored.
- narwhals' .select([]) collapses row count to 0 (not just columns),
  so routing pandas through the narwhals numpy path broke
  return_empty=True (empty variables_) with a "zero-size array to
  reduction operation maximum" error. Guarded for it explicitly, since
  return_empty=True is a real, designed-for case, not a hypothetical.
* Migrate GeoDistanceFeatures to narwhals, add polars support

Six pandas-specific spots split into a pandas-native branch and a
narwhals-generic branch, each decision benchmarked at 10k-50k rows and
0/1/6 extra columns (not assumed):

- missing-columns check, feature_names_in_ extraction: narwhals-on-pandas
  is 13-22x slower (pure metadata overhead, row-count independent) - kept
  the pandas fast path established in Pass 1.
- coordinate range validation: 6-8.6x slower on narwhals-on-pandas - new
  narwhals branch added (previously crashed outright on polars), pandas
  branch untouched.
- numpy extraction of the 4 coordinate columns: 5-9x slower via narwhals on
  pandas; for the narwhals branch itself, .get_column().to_numpy() per
  column beats .select().to_numpy() by 5-7x on polars, so that's what it
  uses.
- assign new column + optional drop: 1.7-2.9x slower on narwhals-on-pandas,
  consistent with the bar CyclicalFeatures used to keep branches separate.
- column reorder is the one exception - narwhals-on-pandas is actually
  ~35% *faster* here at 10k rows - but stays a two-branch split per an
  explicit decision to keep the narwhals-everywhere pattern consistent
  with Pass 1/2, rather than special-case one operation.

Verified end-to-end (not just isolated snippets): pandas output identical
to the pre-migration code, polars value-identical to pandas, ~2% pandas
speed delta (noise) at 10k rows/1 extra column, both backends' fit() error
paths (missing columns, out-of-range coordinates) raise the same messages.

Also fixed a pre-existing, unrelated inaccuracy in the class docstring's
Examples section - the documented pandas output didn't match what the
current (pre-migration) code actually produces. The same drift exists in
the user guide's Python-implementation number tables (haversine, euclidean,
manhattan, miles) but fixing those throughout is out of scope for this
pass - flagged separately.

Tests parametrized pandas+polars where a dataframe is involved; pure
__init__/tag-validation tests (no dataframe) left as-is, already using
match= throughout.

* Apply suggestion from @solegalli

* Fix stale example output throughout GeoDistanceFeatures user guide

Every numeric output table in the "Python implementation" section
(haversine, euclidean, manhattan, miles) had drifted from what the code
actually produces - confirmed by running each documented example directly
and comparing. Some differences are rounding-level, but euclidean trip 4
(1720.18 documented vs 1898.82 actual) and manhattan trip 2 (4684.16 vs
4266.82) are real gaps, and the pipeline predictions example was the
furthest off: documented as the training targets exactly
([100, 150, 80, 200]), actual output is [116.67, 120.75, 88.48, 204.10].
Pre-existing, unrelated to the narwhals migration - verified the old,
unmigrated code produces the same "actual" numbers used here.
* Migrate MathFeatures to narwhals, add polars support

The numpy-reducer fast path (sum/mean/std/var/min/max/prod/median) is
unified into a single narwhals-based code path rather than split by
backend: benchmarked narwhals-on-pandas vs pandas-native at 10k rows/3
reducers and found only a 1.01x-1.27x difference, well under the bar
that kept CyclicalFeatures/GeoDistanceFeatures split (1.7x+). Value
extraction for the fast path stays a small pandas/narwhals split though -
narwhals' select() doesn't accept integer column names the way pandas'
own indexing does, and int-named variables is a real, tested, pandas-only
feature (polars requires string columns).

The custom-callable/uncommon-aggregation fallback can't be unified at all -
narwhals has no row-wise apply. Pandas keeps .agg(func, axis=1); polars
uses its native map_rows(), which passes each row as a plain tuple rather
than a Series, so callables relying on Series methods (row.max()) need
max(row) instead to work on both backends. Documented this explicitly.
A non-callable func (e.g. an uncommon pandas aggregation string like "sem")
now raises NotImplementedError for polars input rather than failing
obscurely, since there's no way to resolve a pandas-specific aggregation
name without pandas itself.

Also fixed a real bug: the module-level `_PANDAS_LT_3 = int(pd.__version__...)`
constant required pandas importable just to import this module at all,
breaking every creation transformer for a polars-only install. Replaced
with a lazy check using narwhals.dependencies.get_pandas() (returns the
already-imported module without importing it), computed only once we
already know X is pandas-backed.

User guide had three separate pre-existing inaccuracies, unrelated to this
migration (confirmed against the old, unmigrated code): a get_feature_names_out
example listed 'amin_Age_Marks'/'amax_Age_Marks' for a transformer that was
never passed np.min/np.max - it uses plain "min"/"max" strings, which have
always produced "min_Age_Marks"/"max_Age_Marks"; and a std column's values
matched pre-pandas-3 semantics (ddof=1) for a np.std example that runs
under ddof=0 in the installed pandas 3.x, already reflected in this
repo's own tests. Fixed both while verifying every table for the new
"With polars" section.

* Rewrite MathFeatures tests to run the same test against both backends

Previously: the original pandas-only tests were left untouched and new,
separate polars-only tests were added alongside them for the same
behavior. That's not what dataframe-agnostic means - same input in, same
values out, checked by the same test. Rewrote every test that touches a
dataframe to build it via make_df and parametrize over
[pd.DataFrame, pl.DataFrame], replacing pd.testing.assert_frame_equal with
a cross-backend assert_df_equal (nw.from_native(...).to_dict() + a per
column approx compare, handling None-vs-NaN as the same "missing" value
on both sides).

The one deliberately un-unified case: an uncommon aggregation string like
"sem" succeeds on pandas (routes through its native .agg()) but raises
NotImplementedError on polars (no way to resolve an arbitrary
pandas-specific string without pandas) - that's a real, documented
asymmetry, not an oversight, so it's one parametrized test with an
explicit if/else on the expected outcome rather than two separate tests
pretending it's the same behavior.

Two genuinely pandas-only tests stay pandas-only, with a comment saying
why: integer column names (polars requires string columns) and pandas'
nullable Int64 dtype (no polars equivalent). Custom-callable fallback
tests merged into one using max()/min()/sum() built-ins, which work
identically whether the callable receives a pandas Series (pandas'
agg(axis=1)) or a plain tuple (polars' map_rows) - no need for
Series-specific vs tuple-specific callables in separate tests.

Picked up narwhals.dependencies.is_pandas_dataframe(X) is True ->
nwd.is_pandas_dataframe(X) and the _pandas_lt_3() -> _pandas_version()
rename from upstream changes to the class file.

* fix: correct _pandas_version() return type hint from bool to int

The function returns int(pandas_version.split(".")[0]) and is used as
_pandas_version() < 3, but its signature still said -> bool, failing
type checking.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
* Migrate RelativeFeatures to narwhals+numpy, add polars support

Replaces the 8 near-identical _add/_sub/_mul/_div/_truediv/_floordiv/_mod/
_pow pandas methods (~90 lines) with a single numpy-ufunc-driven transform(),
per request. Benchmarked at 10k rows, 3 variables, 2 references: the numpy
version is not just "minimal loss" but actually faster than the current
pandas .div(..., axis=0) approach (552.6us vs 637.0us, 0.87x) - so this is
a single unified narwhals+numpy code path, no pandas/polars branch at all
(re-verified against the final committed code: 635.9us pandas, down from
800.7us before this change; 262.4us polars, previously unsupported).

One correctness fix during implementation: extracting all `variables` as
one batched 2D array via select().to_numpy() upcasts every column to a
common dtype, silently turning an int column's subtraction result into
float and failing 3 existing tests. Fixed by extracting each variable as
its own 1D array instead, preserving each column's own dtype promotion
independently - matches pandas' per-column .sub()/.div()/etc. semantics,
still a single vectorized numpy op per column (no Python-level row loop).

Also matched a subtler pandas behavior: floordiv/mod on integer input stay
integer-typed, and assigning a float fill_value at zero-denominator
positions needs the result array explicitly widened to float first (numpy
arrays don't auto-promote dtype on assignment the way pandas' DataFrame
column assignment does) - verified this reproduces pandas' output exactly,
including for negative numbers (floor-division sign conventions matched
NumPy's floor_divide/mod exactly across int/float/negative cases, so no
other adjustment was needed there).

User guide's example tables verified accurate already (including the
Age_pow_Age int64-overflow values, which are genuine hardware overflow
behavior, not a doc error - confirmed identical between pandas and polars).
Added "With polars" sections to docstring and user guide.

* test: merge pandas/polars tests for RelativeFeatures into single parametrized suite

Same treatment as the MathFeatures test rewrite: one test per behavior,
parametrized over make_df=[pd.DataFrame, pl.DataFrame], checking identical
values come out for identical input instead of separate pandas-only and
polars-only test functions. Deletes the redundant separately-added polars
section, keeps its 3 genuinely-new cases (mixed dtype preservation, float
fill_value dtype widening, drop_original column list), and converts the
pandas-specific .loc-based zero-fill assertion to a narwhals-based one.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
* Migrate DecisionTreeFeatures to narwhals, add polars support

Follows the same pandas-native / narwhals-generic split established for
GeoDistanceFeatures (this transformer also reimplements fit()/transform()
directly, not via BaseCreation): .columns extraction, column reorder,
prediction-column assignment, and drop_original all split by backend,
consistent with every other operation in this module that's been
benchmarked as a real (not minimal) loss when routed through narwhals
on pandas.

Confirmed empirically before designing: sklearn's DecisionTreeRegressor/
Classifier and GridSearchCV accept a polars DataFrame directly for both
fit() and predict()/predict_proba(), so the actual tree training/inference
calls are unchanged - only the surrounding column selection, extraction,
and reassembly needed migrating.

Fixed a pre-existing bug found while rewriting the exact code path it
lived in: single-feature combos with an integer column name (e.g.
DecisionTreeFeatures(features_to_combine=1) on a dataframe with columns
0, 1, ...) crashed, since the original `isinstance(features, str)` check
missed the int case and fell through to plain X[features] indexing, which
returns a 1D Series rather than the 2D input sklearn requires. Widened to
isinstance(features, (str, int)); verified the same single-feature
narwhals path (get_column().to_frame()) already handles both cleanly.

Regression, binary classification, and multiclass classification paths
all verified to produce identical predictions between pandas and polars
input. return_empty=True + polars remains untestable here too (same
nw.col([]) bug in dataframe_checks.py found during CyclicalFeatures,
still tabled) - this is the second transformer it blocks.

docs/user_guide/creation/DecisionTreeFeatures.rst is large (511 lines)
and built around actual cross-validated tree fitting on the real
California housing dataset across many sections - re-verified the cheap,
deterministic parts (the raw data table) but did not re-run every
tree-fitting example given the cost of repeated grid-search CV fits;
unlike the other three creation-module docs this pass touched, the rest
of this file's numbers are unverified. Added a self-contained "With
polars" section using simple synthetic data instead, fully verified.

* Apply suggestion from @solegalli

* Apply suggestion from @solegalli

* docs: clarify is True/is False and cross-backend test conventions in AGENTS.md

Two rules made explicit based on recent work: the is True/is False
comparison is for flow control only, not variable assignment (per Sole's
own simplification of is_pandas = nwd.is_pandas_dataframe(X) is True to
just nwd.is_pandas_dataframe(X) in decision_tree_features.py); and
dataframe-agnostic transformers get one parametrized test per behavior
covering both pandas and polars, never separate per-backend tests.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* feat: add n_jobs for parallel tree training, merge tests to single cross-backend suite

Adds an n_jobs parameter to DecisionTreeFeatures that parallelizes tree
training across feature combinations via joblib, using threads rather
than processes since fitting a decision tree releases the GIL for the
bulk of its computation - threads avoid the overhead of copying the whole
dataframe to worker processes. Defaults to None (sequential), preserving
current behavior.

Benchmarked on the committed transformer (5000 rows, 10 vars,
features_to_combine=3, 8-point param_grid, 175 trees): 12.17s sequential
vs 5.15s at n_jobs=-1, ~2.4x. On small workloads (a handful of feature
combinations, the shape of the existing unit tests) parallelizing is a
net loss - thread-dispatch overhead outweighs the gain - which is why the
default stays sequential. Parallelizing transform()'s predict loop the
same way was also benchmarked and found to have no benefit (predict is
too cheap per call), so only fit()'s tree training is parallelized.
Correctness verified: identical trees/predictions regardless of n_jobs.

Also rewrites test_decision_tree_features.py to the single
cross-backend-parametrized-test convention used elsewhere in this
migration: one test per behavior over make_df=[pd.DataFrame,
pl.DataFrame], deleting the separately-added polars-only section that
duplicated coverage already present once the original tests are
parametrized. Adds n_jobs correctness coverage (parallel vs sequential
training gives identical output, both backends).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: avoid pandas fragmentation warning in DecisionTreeFeatures.transform

transform() assigned one new tree-prediction column at a time
(X[col_name] = preds), which triggers pandas' "DataFrame is highly
fragmented" PerformanceWarning once there are enough feature
combinations - confirmed with 10 vars/features_to_combine=3 (175 new
columns). .assign(**kwargs) does NOT fix this: it inserts columns one
at a time internally too, same warning. The actual fix is building all
new columns into one DataFrame and joining once (single insertion).

Verified: output is byte-identical to the old behavior
(pd.testing.assert_frame_equal on a 3000-row/9-var/129-tree case),
drop_original still works, and a new regression test confirms the
warning is gone (and fails against the old code, confirming it
actually catches the regression).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* shorten docstring

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
solegalli and others added 3 commits August 25, 2026 20:20
Pure elementwise math (1 / x), so followed the same precedent as
ArcsinTransformer (same module, same shape of problem): extract the
transform columns to a single numpy array via narwhals' to_numpy(),
apply the division once, reassign via nw.new_series + with_columns.

Benchmarked narwhals-on-pandas vs the old pandas-native .loc-assignment
across 10k-100k rows and 1-10 columns: narwhals-on-pandas was 2-3x
*faster* than the old code (0.34x-0.45x of old runtime), narwhals-on-
polars faster still - a stronger case for merging into one path than
even ArcsinTransformer's parity/faster numbers, so no pandas/polars
branch was added.

The zero-denominator check (raises ValueError "Some variables contain
the value zero...") is preserved exactly in both fit() and transform(),
just computed via a numpy comparison on the extracted values instead of
a pandas boolean mask. inverse_transform() is unchanged - it still just
calls transform(), since 1/(1/x) = x.

Rewrote test_reciprocal_transformer.py to one parametrized test per
behavior over pandas/polars input (previously pandas-only, relying on
the global df_vartypes/df_na fixtures - replaced with local dict data,
same pattern as test_arcsin_transformer.py, so both backends build from
the same source). Added a verified "With polars" section to the docs;
left the pre-existing Ames-housing walkthrough untouched (no network
access in this environment to re-verify fetch_openml output, and it
wasn't modified by this migration).

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Pure elementwise math (arcsin(sqrt(x))), so followed the MathFeatures/
RelativeFeatures precedent: extract the transform columns to a single
numpy array via narwhals' to_numpy(), apply np.arcsin(np.sqrt(...))
once, reassign via nw.new_series + with_columns. Benchmarked against
the old pandas-native .loc assignment across 10k-100k rows and 1-10
columns: narwhals-on-pandas was consistently at parity or faster
(0.4x-1.05x of old runtime, never a regression), so merged into one
narwhals-generic path with no pandas/polars branch - same decision
MathFeatures/RelativeFeatures landed on for the same shape of problem.

fit() and transform() both extract the same numpy array for the
range check (values must be in [0, 1]) and reuse it directly for the
transform in transform(), avoiding a second backend round-trip.
inverse_transform() follows the same pattern.

Rewrote test_arcsin_transformer.py to one parametrized test per
behavior over pandas/polars input (previously pandas-only, relying on
the global df_vartypes/df_na fixtures - replaced with local dict data
so both backends can build from the same source). Added a verified
"With polars" section to the docs.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Same elementwise-math shape as ArcsinTransformer: extract the transform
columns to one numpy array via narwhals' to_numpy(), apply
np.arcsinh((x - loc) / scale) once, reassign via nw.new_series +
with_columns. Benchmarked against the old pandas-native .loc assignment
across 10k-100k rows and 1-10 columns: narwhals-on-pandas was
consistently faster than the old code (0.48x-0.83x of old runtime), so
merged into one narwhals-generic path with no backend branch.

Found a pre-existing stale docstring while verifying output against the
old code: the class docstring's example table (arcsinh of
np.random.randn(100) * 1000 with seed 42) printed values that don't
match what either the old or new code actually produces (e.g. 7.516076
vs the real 6.901163 for the first row) - confirmed by running the old
(pre-migration) code directly, so this predates the migration. Fixed
the docstring numbers to the verified real output. The
docs/user_guide/transformation/ArcSinhTransformer.rst walkthrough's
printed tables were re-run and already matched exactly, so those were
left as-is; added a verified "With polars" section to both the
docstring and the user guide.

Rewrote test_arcsinh.py to parametrize every behavior over pandas and
polars input (previously pandas-only).

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
solegalli and others added 17 commits September 18, 2026 15:05
…ion transformers (#1057)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Migrate scaling module (MeanNormalisationScaler) to narwhals, add polars support

Redone from scratch off the current narwhals-migration HEAD rather than
rebased forward from #979: that branch predates the dataframe_checks
rewrite (#989), the variable_handling rewrite (#978), and the creation
base rewrite (#990), so the delta had grown too large to carry forward
safely for a module this small.

fit() replaces the pandas .mean()/.max()/.min() reductions with a
single narwhals+numpy path: wrap via nw.from_native, extract the
variables as one batched array (nw_X.select(variables_).to_numpy()),
reduce with numpy. transform()/inverse_transform() extract each
variable as its own 1D array via get_column().to_numpy(), do the
elementwise (x - mean) / range (or the inverse) in numpy, and write
each back via nw.new_series(same_name, ...) + with_columns() --
same-named series replace the existing column in place, same as
polars, rather than adding a new one the way RelativeFeatures/
MathFeatures do for their derived columns.

Benchmarked narwhals-expression vs. narwhals+numpy for both fit and
transform, at 100/10k/200k rows and 3/20 variables, both backends,
before choosing: numpy wins by 2x-73x at small/medium scale on both
pandas and polars, and even at 200k rows/polars where narwhals-expr
pulls ahead it's only by ~2x, well inside the range this migration has
been treating as "not worth a backend split" (CyclicalFeatures/
GeoDistanceFeatures used ~1.7x+ as the bar for splitting; nothing here
gets close). One unified path, no pandas/polars branch, matching
RelativeFeatures' precedent.

return_empty=True guarded explicitly (mean_/range_ default to {} when
variables_ is empty) -- narwhals' select([]) collapses row count too,
so .to_numpy() on it would reduce over zero rows, not zero columns.
Same fix CyclicalFeatures needed for the same reason.

Docstring and user-guide numbers were wrong before this PR touched
them, found while verifying rather than assumed: the docstring's five
example values were literally the raw pre-normalization np.random.seed(42)
draws, never the actual transform() output, and the user guide's
inverse_transform table showed Age as a bare int (20, 21, ...) when
both the pre-migration and post-migration code have always produced
float64 there (multiplying by a float range always promotes the dtype,
confirmed by running the pre-migration code directly). Fixed both,
added a "With polars" section per AGENTS.md's doc-sync rule.

Tests rewritten to the single-parametrized-over-both-backends
convention (make_df=[pd.DataFrame, pl.DataFrame]) rather than kept
pandas-only; all prior coverage preserved, including both class names
(MeanNormalisationScaler and the deprecated MeanNormalizationScaler
alias) and the deferred-attribute-assignment regression test.

Verified: full test suite run twice, once against this branch and once
against the unmodified narwhals-migration HEAD (via git stash) --
identical 68 pre-existing, unrelated failures in both runs (none in
scaling; confirmed by diffing the two failure lists directly, not just
comparing counts), 2273 -> 2287 passed (the +14 is exactly this file's
new parametrized test count minus its old one). flake8 and mypy clean.

* Use shared backend test fixtures and helpers in MeanNormalisationScaler tests

Replace the file-local assert_df_equal/_none_to_nan helpers and parametrize
decorators with the shared test structure: make_df fixture,
isinstance(X, make_df) plus to_dict() checks (pytest.approx for floats), and
pytest.raises(match=re.escape(msg)).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Use frame_to_dict after the shared helper rename in #1045

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Match errors, drop init asserts and rename a test in MeanNormalisationScaler tests

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Soledad Galli <solegalli@protonmail.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
…ds (#1053)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
…intainers (#1055)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Six methods head their return section `Return` instead of `Returns`.
numpydoc only recognises `Returns`, so it warns "Unknown section Return"
and drops the content: the documented return value does not appear in the
rendered docs at all. The rest of the package uses `Returns` in 97 places,
so these six are outliers.

`_transform` in base_predictor.py additionally had its whole docstring
body indented one space further than the entry beneath it, which produced
a second numpydoc warning about the underline length; normalised it to
match every other docstring in the file.

`DropMissingData.return_na_data` had no return section at all - its return
value was documented under Parameters as `X_na`, leaving the actual
parameter `X` undocumented. Moved `X_na` to Returns and documented `X`
using the same wording as `transform()` directly above it.
#1033)

* Migrate BaseOutlier and WinsorizerBase to narwhals, add polars support

Shared base for all outlier transformers (ArbitraryOutlierCapper extends
BaseOutlier directly; Winsoriser/OutlierTrimmer extend WinsorizerBase):
column reorder + NA/Inf checks in _check_transform_input_and_state(),
the fold-limit estimation in WinsorizerBase.fit() (gaussian/iqr/mad/
quantiles), and the capping step in BaseOutlier._transform() are now
dataframe-agnostic.

Capping (np.clip against per-column bounds) was benchmarked three ways
at 10k/50k/100k rows x 1/2/10 columns: pandas-native .clip() loop vs. a
single narwhals with_columns(nw.col(v).clip(lo, hi) for v in ...) vs.
grouping columns by which bound(s) apply and running up to 3 vectorized
numpy calls (np.clip/minimum/maximum) via to_numpy()/new_series(), mirroring
ReciprocalTransformer's numpy-acceleration pattern. narwhals-generic alone
was already close to parity (0.95-1.49x pandas-native - minimal loss,
mergeable per the imputation-base precedent), but the numpy-grouped version
was faster still: 0.16-0.82x of pandas-native on the homogeneous case
(single tail, all columns share the same bound - the common Winsoriser/
OutlierTrimmer case) and 0.42-1.52x on mixed-coverage dicts (the
ArbitraryOutlierCapper case, up to 3 groups). Adopted the numpy-grouped
version as the single merged code path for both backends.

A first numpy attempt used a blanket -inf/inf sentinel for the missing
side per column (like RelativeFeatures-style bound arrays) - that's a
correctness bug, not just a style choice: mixing an int64 numpy array
with a float -inf/inf bound upcasts the whole column to float64 even
when the real, present bound is an int (e.g. ArbitraryOutlierCapper's
own docstring example, `max_capping_dict=dict(x1=8)`, expects int64 out).
Grouping columns into "both bounds" / "right only" / "left only" buckets
and calling np.clip/minimum/maximum with only the bounds that actually
exist avoids ever introducing an inf, so dtype promotion matches pandas
.clip() exactly - verified byte-for-byte against the old pandas-only
implementation across all 4 capping methods x 3 tails, plus the int-dtype
and mixed-dict-coverage cases.

Also found and fixed a real bug introduced while migrating fit(): plain
np.mean/np.std/np.quantile/np.median propagate NaN, unlike pandas'
mean/std/quantile/median which skip NaN by default. With
missing_values="ignore" and NaN present, this silently produced NaN
caps instead of the caps computed from non-null data. Fixed by using
the nan-aware numpy variants (np.nanmean/nanstd/nanquantile/nanmedian).
Caught by tests/test_outliers/test_winsorizer.py::test_transformer_ignores_na_in_df,
which predates this migration but exercises exactly this path.

variables/feature names can be int or str; passing a plain list to
narwhals' .select() only works for string columns, so every .select()
call here uses nw.col(*variables) instead - .select(list_of_ints)
raises InvalidIntoExprError.

Verified: tests/test_outliers full suite - 83 passed, 3 pre-existing
failures in test_check_estimator_outliers.py (sklearn's check_estimator
feeds raw numpy arrays, which check_X() has always rejected per the
narwhals migration's dataframe-only contract; identical failure set
before and after this change). flake8 and mypy clean on the file.
Module imports and runs fit/_transform end-to-end on polars with pandas
import fully blocked. sphinx -W build clean (only the pre-existing
unrelated linkcode_resolve warning). All 4 capping-method x tail
combinations and the Winsoriser/OutlierTrimmer/ArbitraryOutlierCapper
docstring examples produce byte-identical output to the pre-migration
code (checked exact numeric values and dtypes).

Not migrated here (belongs to the 3 follow-on transformer branches):
ArbitraryOutlierCapper.fit()/transform(), Winsoriser's add_indicators
branch (pd.concat), and OutlierTrimmer.transform() (its own .le/.ge/.loc
row-filtering, which doesn't go through BaseOutlier._transform at all)
all still import pandas directly. Existing tests in tests/test_outliers
were left pandas-only rather than parametrized over polars, since they
exercise those still-pandas-only subclasses, not BaseOutlier/
WinsorizerBase directly - parametrizing them now would fail on reasons
unrelated to this file.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* Adapt BaseOutlier and WinsorizerBase to narwhals-returning check_X

check_X now returns a narwhals frame (#1019). The outlier base classes
rebound X = check_X(X) and then used it as a native frame (X.columns,
X[self.feature_names_in_], nw.from_native(X)), which broke every outlier
transformer on narwhals-migration. Mirror the imputation and encoding
modules instead:

- fit(): bind nw_X = check_X(X), keep passing the native X to the
  variable and NA/inf checks, compute on nw_X, and set feature_names_in_
  and n_features_in_ from it.
- _check_transform_input_and_state(): return the narwhals frame,
  reordered to the train set columns.
- _transform(): compute on that frame and return the native frame.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Add shared outlier test data fixtures

data_normal_dist and data_na, shared by the OutlierTrimmer and Winsoriser
tests, as fixtures returning plain dicts built with make_df(data).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Add tests for the outlier base classes

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Give variables without variation infinite caps instead of raising an error

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
* Migrate Winsoriser/Winsorizer to narwhals, add polars support

Removed the module-level `import pandas as pd` and `import numpy as np`;
X type hints now use narwhals' IntoDataFrame. WinsorizerBase.fit/transform
(shared base) were already migrated on origin/narwhals-outliers-base; this
change covers the Winsoriser-specific piece: transform()'s add_indicators
path, which compares the capped output against the original input to build
per-tail boolean flag columns and previously only worked on pandas.

Benchmarked the add_indicators comparison+concat step at 10k/50k/100k rows
x 1/2/10 columns: pandas-native (boolean comparison + pd.concat) is up to
~3x faster than the narwhals with_columns equivalent on pandas input, and
the loss grows with column count (1 col: narwhals-on-pandas was actually
faster; 10 cols: ~2-3x slower). That crosses the "keep pandas fast path"
threshold, so transform() splits on `nwd.is_pandas_dataframe`, matching
MissingIndicator's precedent for its own indicator-building step: pandas
keeps its existing comparison+concat logic (now obtaining the `pd` module
via `nw.from_native(...).__native_namespace__()` instead of importing it),
and a new narwhals with_columns path (per-column Series comparison, cast to
Float64) covers polars and other backends.

Preserved the Winsoriser/Winsorizer deprecation exactly as-is: Winsoriser
is the current public name (renamed to the British spelling in #967);
Winsorizer is a deprecated subclass that raises the same FutureWarning on
__init__ and will be removed in 2.1.0. Note this is the reverse of what
one might guess from the class names alone.

Tests: converted tests/test_outliers/test_winsorizer.py from pandas-only
fixtures (df_normal_dist, df_vartypes, df_na) to local dicts parametrized
over `make_df` in [pd.DataFrame, pl.DataFrame], asserting identical capping
values, indicator columns, and get_feature_names_out() on both backends for
the same input. Missing-value dicts use None instead of np.nan in string
columns, since polars' DataFrame constructor rejects a float NaN mixed into
a string column. A helper filters both pandas' NaN and polars' None
representations of a missing value when comparing outputs cross-backend.

Docs: verified every doc example in docs/user_guide/outliers/Winsoriser.rst
against actual output (network access to fetch_openml's house_prices
dataset was available; outputs matched exactly, no changes needed) and
added a "With polars" section covering add_indicators, matching the
pattern used in other migrated user guides. Added a verified "With polars"
example to the class docstring.

Verified: tests/test_outliers/test_winsorizer.py 93 passed. Full
tests/test_outliers suite: 123 passed / 3 pre-existing failures in
test_check_estimator_outliers.py (confirmed identical against a baseline
run of origin/narwhals-outliers-base: 83 passed / same 3 failures -
sklearn's check_estimator feeds raw numpy arrays, which check_X() has
always rejected per the narwhals migration's dataframe-only contract;
predates this change). flake8 and mypy clean. sphinx -W build clean (only
the pre-existing unrelated linkcode_resolve warning, confirmed present on
the base branch too). Confirmed winsorizer.py and base_outlier.py import
successfully and a full polars fit_transform (including add_indicators)
runs correctly with pandas' own import blocked at the builtins level.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* Adapt Winsoriser indicators to narwhals-returning check_X

With add_indicators=True, transform() compared the capped output against
check_X(X), which is now a narwhals frame, so the pandas path mixed pandas
and narwhals objects (broadcast errors, wrong indicators). Compare against
the user's native X instead; _transform() already validates it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Use shared backend test fixtures and helpers in Winsoriser tests

Replace the file-local data dicts and _col/_cols/_shape/_drop_missing
helpers with the shared test structure: make_df and data_normal_dist /
data_na fixtures, isinstance(X, make_df) plus to_dict() checks, and
pytest.raises(match=...).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Use frame_to_dict after the shared helper rename in #1045

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Call is_pandas_dataframe in the condition instead of storing it in Winsorizer

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Document and test infinite caps for variables without variation

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Align Winsoriser and its tests with the repo conventions

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
…orm checks (#1060)

_fit_setup, _fit_from_dict and _check_transform_input_and_state now return the
narwhals frame from check_X, so the numerical transformers no longer wrap X a
second time. Also fixes pandas integer column names in the transformation
transformers, CyclicalFeatures and MeanNormalisationScaler, and the inf check
on polars when there are no variables.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Migrate ArbitraryOutlierCapper to narwhals, add polars support

fit() only builds dicts from user input and validates variables/dtypes
via check_numerical_variables (already narwhals-generic) - no numeric
computation, so nothing to branch on there. The only pandas-specific
lines were the feature_names_in_ assignment (X.columns.to_list(), a
pandas-Index method), replaced with the same is_pandas-guarded pattern
WinsorizerBase.fit() already uses (list(X.columns) for pandas,
nw.from_native(X).columns - already list[str] - otherwise). transform()
was already dataframe-agnostic via BaseOutlier._transform(); only its
type hints changed (pd.DataFrame -> IntoDataFrame).

Benchmarked fit+transform end-to-end at 10k/50k/100k rows x 1/2/10
columns: pandas-native (pre-migration) vs the migrated code on pandas
were within noise of each other (~0.9-1.1x), and polars ran 2-4x faster
than pandas on both. No pandas/polars branch needed - merged single
path, consistent with the is_pandas-only-for-.columns precedent already
set in WinsorizerBase.

Confirmed the module needs zero pandas: reloaded artbitrary.py in
isolation with sys.modules["pandas"] = None (simulating an uninstalled
pandas) and ran fit/transform end-to-end on a polars frame - works,
and int64 stays int64 for a same-dtype capping dict (the class
docstring's own x1 example).

Found, while doing so, a real dtype-preservation bug in the already-
merged BaseOutlier._transform() (base_outlier.py, commit 71bf7cf on
this branch's base) that predates this migration and is not introduced
here: when a capping-dict spans columns of different dtypes that land
in the same bound-group (e.g. max_capping_dict={"age": 50, "fare": 200}
with age int64 and fare float64 - both "right_only"), the group's
columns are stacked into one 2D array via to_numpy() before np.clip,
which forces a common dtype and upcasts age to float64. The pre-
narwhals code (verified against 71bf7cf^) clipped each column
independently (X[feature] = X[feature].clip(...)), so int columns
never picked up a neighboring float column's dtype. Confirmed this
reproduces identically on both pandas and polars (same merged code
path) and is untouched by this commit - it lives in base_outlier.py,
shared with Winsoriser/OutlierTrimmer, out of this file's scope.
Flagged separately rather than fixed here.

Rewrote test_arbitrary_capper.py to one parametrized test per behavior
over pd.DataFrame/pl.DataFrame (previously pandas-only), using
nw.from_native(...).to_dict(as_series=False) for backend-agnostic
assertions in place of pd.testing.assert_frame_equal, following the
same pattern used for ReciprocalTransformer/ArcsinTransformer. Added a
verified "With polars" section to the docs (float dtypes throughout, to
sidestep the dtype-upcast issue above rather than put an unexplained
surprise in a user-facing example); left the pre-existing pandas
Titanic walkthrough untouched - no network access in this environment
to re-verify the fetch_openml/CSV-backed output.

Verified: tests/test_outliers full suite - 88 passed (up from 83, all
5 new instances are the added polars parametrizations), same 3
pre-existing check_estimator failures as the pre-migration baseline
(numpy-array input, unrelated to this change). flake8 and mypy clean.
sphinx -W build clean (only the pre-existing linkcode_resolve warning).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* Adapt ArbitraryOutlierCapper to narwhals-returning check_X

Bind the narwhals frame returned by check_X and set feature_names_in_ and
n_features_in_ from it, instead of treating the check_X result as a native
frame, mirroring the imputation and encoding modules.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Use shared backend test fixtures and helpers in ArbitraryOutlierCapper tests

Replace the file-local _to_dict helper and parametrize decorators with the
shared test structure: make_df fixture, isinstance(X, make_df) plus to_dict()
checks, missing values written as None, and pytest.raises(match=re.escape(msg)).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Use frame_to_dict after the shared helper rename in #1045

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Align ArbitraryOutlierCapper and its tests with the repo conventions

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Give the expected caps explicitly in the ArbitraryOutlierCapper capping test

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
* Migrate OutlierTrimmer to narwhals, add polars support

transform() now filters rows via a single narwhals .filter() call built
from a combined boolean expression (AND of each variable's right/left
cap conditions), instead of a pandas .loc masking loop. Benchmarked
against pandas-native and a numpy boolean-mask extraction at 10k/50k/
100k rows x 1/2/10 columns: the narwhals filter is within 1.4-1.75x of
pandas-native at 10k rows (sub-millisecond absolute difference) and
becomes faster than pandas-native from 50k rows up (0.58x-0.97x),
so a single merged code path (no is_pandas branching) is the right
call here - unlike BaseOutlier's elementwise capping, which benefits
from numpy grouping, row-filtering is exactly what narwhals .filter()
already pushes down to the native backend efficiently.

Also fixes a latent bug in TransformXyMixin.transform_x_y()
(_base_transformers/mixins.py): the non-pandas branch added a
row-index marker column via with_row_index() and passed it straight
to self.transform(), but never widened feature_names_in_/
n_features_in_ to account for it. Any transform() that validates
column count (BaseOutlier._check_transform_input_and_state, via
_check_X_matches_training_df) then raised a ValueError on the extra
column. This was latent because no narwhals-migrated class on this
branch previously combined TransformXyMixin with a column-count-
checking transform() on a non-pandas backend - OutlierTrimmer is the
first. The fix (guarded widen/restore of feature_names_in_ around the
transform() call) is carried over verbatim from the same fix already
applied to this file on branch narwhals-drop-missing-data (commit
fd99caf), which hadn't been merged into this branch yet.

Tests rewritten to one parametrized test per behavior over
make_df in [pd.DataFrame, pl.DataFrame], plus a new test asserting
that caps on two different variables combine with AND (each variable
drops a distinct row) - a code path the old sequential-loop version
exercised implicitly but no test isolated directly.

Docs verified against live output: the class docstring's pandas
examples were already accurate; the user guide's Titanic-based
numbers had drifted from the current openml dataset (predates this
migration, e.g. the IQR section's age max was already wrong against
the old pandas-loop transform()) and are corrected here, plus a "With
polars" section is added.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* Adapt OutlierTrimmer to narwhals-returning check_X

_check_transform_input_and_state() now returns the narwhals frame, so
filter it directly instead of re-wrapping it with nw.from_native().

TransformXyMixin.transform_x_y() (rebased onto #1024) widens
n_features_in_ for the row-index tag column; also widen
feature_names_in_, since BaseOutlier reorders X to feature_names_in_ and
would otherwise drop the tag column on non-pandas backends.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Use shared backend test fixtures and helpers in OutlierTrimmer tests

Replace the file-local data dicts and _cols/_to_list/_make_series helpers
with the shared test structure: make_df and data_normal_dist / data_na
fixtures, y built with make_series, isinstance(X, make_df) plus to_dict()
checks, and pytest.raises(match=...).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Use frame_to_dict after the shared helper rename in #1045

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Document and test infinite caps for variables without variation

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Align OutlierTrimmer and its tests with the repo conventions

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Keep transform_x_y row alignment in OutlierTrimmer instead of the shared mixin

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
# Conflicts:
#	tests/test_creation/test_check_estimator_creation.py
#	tests/test_datetime/test_datetime_features.py
#	tests/test_discretisation/test_check_estimator_discretisers.py
#	tests/test_encoding/test_check_estimator_encoders.py
#	tests/test_imputation/test_check_estimator_imputers.py
#	tests/test_outliers/test_check_estimator_outliers.py
#	tests/test_preprocessing/test_check_estimator_preprocessing.py
#	tests/test_selection/test_check_estimator_selectors.py
#	tests/test_time_series/test_forecasting/test_check_estimator_forecasting.py
#	tests/test_transformation/test_check_estimator_transformers.py
#	tests/test_wrappers/test_check_estimator_wrappers.py
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Migrate MatchVariables to narwhals, add polars support

fit() and transform() take any narwhals-supported dataframe and return
the same backend. pandas adds, drops and reorders the columns with a
single reindex (faster than the previous drop/setitem/select and than
narwhals at 10k-500k rows); other backends use with_columns + select.

With match_dtypes, pandas keeps its dtypes and astype, since narwhals
dtypes don't hold the categories of pandas categoricals. Other
backends store narwhals dtypes and cast with narwhals, turning values
outside Enum categories into nulls and parsing strings into dates, as
pandas does.

With polars, np.nan fill values add null Float64 columns and integer
fill values add Int64 columns. The verbose messages list the variables
in a fixed order: training order for added variables, input order for
dropped ones. Init error messages now end with "Got {param} instead.".

Rewrite the tests to the make_df conventions and add a polars example
to the docstring and the user guide, refreshing its outputs.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Keep the dtypes learned by MatchVariables private and allow missing data in integer and boolean variables

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Mark polars output blocks in the user guide as text

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Migrate MatchCategories to narwhals, add polars support

MatchCategories now accepts pandas and polars dataframes. With pandas it
keeps casting to the category dtype; with polars it casts to Enum with the
categories learned in fit. Unseen categories become missing values in both.

Also fixes the warning and error message when several integer-named pandas
columns get missing values (it raised a TypeError).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Mark polars output blocks in the user guide as text

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Remove the transformer printout after fit() from the MatchCategories docstring

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Migrate TextFeatures to narwhals, add polars support

TextFeatures now accepts pandas, polars and other dataframes supported by
narwhals, and returns the same type it receives. All features are defined once
in terms of a few text statistics, computed with pandas string methods and
Python loops for pandas, polars string methods for polars, and narwhals
expressions for other backends. Pandas outputs are identical to before and
the transform is about 3x faster.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Exclude spaces from avg_word_length and use the shared get_feature_names_out in TextFeatures

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
solegalli and others added 10 commits November 21, 2026 08:53
…s conversion (#1126)

* Add dataframe helpers and use them in the creation transformers

Transformers should not check which dataframe library they receive. This adds
feature_engine/_dataframe_helpers.py, which holds the code that depends on the
library, and converts the creation transformers to it:

- to_numpy returns the variables as numpy arrays
- add_arrays adds numpy arrays to the dataframe as columns
- select_columns reorders or subsets the columns

The creation transformers take the columns as numpy arrays, compute with numpy
and add the results back. The pandas checks in the creation module go from 16
to 3. The 3 left are in MathFeatures, which keeps its current behaviour for
nullable dtypes, custom functions and uncommon aggregation names.

The helpers keep a pandas fast path. With pandas, narwhals' with_columns and
select take time for every column of the dataframe, and Series.to_numpy scans
the column for nulls. With 10,000 rows and 1,000 columns, the transform of
MathFeatures, RelativeFeatures and CyclicalFeatures took 14 ms and now takes
about 1 ms, against 3 to 4 ms on main.

Outputs are the same as before on pandas and polars, up to the last bit of
float32 values in CyclicalFeatures with polars. CyclicalFeatures learns an
integer maximum for integer variables that are fitted together with float
variables (12 instead of 12.0).

CyclicalFeatures now computes the sine and cosine with numpy, which runs on
one thread: with polars its transform goes from 4.9 to 12.3 ms on 500,000
rows.

AGENTS.md describes the helpers and asks to benchmark wide dataframes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* Return the pandas dataframe from check_X when the input is pandas

check_X returns the same pandas dataframe it receives and a narwhals dataframe
for other libraries. On pandas it checks duplicated column names natively,
instead of wrapping the dataframe with narwhals, which takes 16 us with 30
columns and 105 us with 1,000. check_X_y reads the index from the pandas
dataframe directly.

Transformers that use the result as a narwhals dataframe are not adapted yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Make the dataframe helpers take the dataframe returned by check_X

Rename the module to dataframe_helpers, add_arrays to add_arrays_to_df and
select_columns to subset_df, and add drop_variables_from_df. The helpers take
and return a pandas dataframe, or a narwhals dataframe for other libraries,
so they no longer convert between the two. Update the creation transformers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Simplify to_numpy for pandas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Replace add_arrays_to_df with add_columns_to_df

The helper only adds new columns, with one pandas concat, so the input is not
modified and the dataframe is not fragmented.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Apply suggestion from @solegalli

* Update AGENTS.md for the new check_X and dataframe helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* update df checks

* update df helpers

* refactor variable handling module

* final update to creation module

* polish comments

* remove get_column_names from AGENTS.md

* move datetime module to check_X and numpy (#1127)

* move datetime module to check_X and numpy

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* remove author line

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* Update discretisation module to the new check_X and dataframe helpers (#1128)

Add replace_columns_in_df to the dataframe helpers. Discretisers now take
values with to_numpy, bin them with numpy and replace the columns with the
helper, on the dataframe returned by check_X.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* move encoding module to check_X and dataframe helpers (#1129)

* move encoding module to check_X and dataframe helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* remove int cast from map_categories_in_df

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* move imputation module to check_X and dataframe helpers (#1130)

* Update imputation module to the new check_X and dataframe helpers

Imputers use check_X and new dataframe helpers for missing values,
statistics and row selection. Transform reorders the columns as in the
train set again. RandomSampleImputer draws its values with numpy, so polars
gets the same values as pandas.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* remove sentence from RandomSampleImputer polars example

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shorten comment in TransformXyMixin

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* move helpers used by one transformer into their modules

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* cast to object inside CategoricalImputer.transform

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* move outliers module to check_X and dataframe helpers (#1131)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* move scaling and preprocessing modules to check_X and dataframe helpers (#1132)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* move transformation module to check_X and dataframe helpers (#1133)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* update AGENTS.md and subset_df docstring

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* move text module to check_X and dataframe helpers (#1134)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* fix filling pandas categorical variables (#1135)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
* Migrate BaseForecastTransformer to narwhals, add polars support

The base of the forecasting transformers takes pandas and polars dataframes.
With pandas the index keeps ordering the rows in time; with polars the rows
are used in the order given, sort_index does not apply and freq raises an
error. The base now provides transform() and transform_x_y() on top of an
_add_features() hook for the subclasses.

Also sets AGENTS.md's priorities to speed, readability and simplicity.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* move base forecast transformer to check_X and dataframe helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* remove transform and small wrapper methods from base forecast transformer

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* check freq only in fit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
…#1124)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
* Migrate BaseTargetMeanEstimator to narwhals, add polars support

Compute the target mean per bin and per category directly instead of
through a Pipeline of discretiser and MeanEncoders, reusing the fitted
discretiser's bin edges and labels. Fit and _predict accept pandas and
polars dataframes, and pandas integer column names now work.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* update BaseTargetMeanEstimator to the new check_X and dataframe helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* compute bin target means in a helper with a pandas fast path

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reuse the discretiser and MeanEncoder in BaseTargetMeanEstimator

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* encode bin numbers instead of interval labels

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Drop the pandas import and type fit and predict for any narwhals-supported
dataframe, with the target as a series, numpy array or list. Rewrite the
tests to run on pandas and polars, and add a polars example to the docstring.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Migrate TargetMeanClassifier to narwhals, add polars support

- fit, predict, predict_proba and predict_log_proba accept pandas and polars
  dataframes, and the target as a Series of any backend, numpy array or list.
- Find the target labels once instead of twice when remapping them to 0 and 1.
- Fix fit with a list of string labels, which raised a TypeError.
- Rewrite the tests to the conventions, with both backends.
- Add pandas and polars examples to the docstring.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* remove the unused prediction conftest and compare the labels explicitly

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* restore the original comment and label check in TargetMeanClassifier.fit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Migrate SklearnWrapper to narwhals, add polars support

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>

* Shorten a comment in SklearnWrapper

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Update SklearnWrapper to the new check_X and dataframe helpers

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>

* Return what scikit-learn returns from SklearnWrapper

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>

* Remove the FunctionTransformer note from the polars user guide section

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Inline the scikit-learn input in SklearnWrapper

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Bring back the SklearnWrapper tests lost in the rewrite

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>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Migrate LagFeatures to narwhals, add polars support

LagFeatures takes pandas and polars dataframes. It implements the
_add_features() hook of BaseForecastTransformer and inherits transform()
and transform_x_y() from it.

With pandas, the output is identical: the lags are joined on the index,
freq shifts on the DatetimeIndex and fill_value works as before. Joining
one freq lag at a time is 1.5-3x faster with lists of freq. With polars,
the rows are lagged in the order given, and fill_value only fills the rows
that have no past values.

Also adds the missing space in the sort_index error message, and a polars
example to the docstring and user guide.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* move LagFeatures to check_X and the forecasting helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* simplify the pandas lags and drop the join suffixes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* validate periods and freq in LagFeatures init, accept pandas offsets in freq

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Migrate WindowFeatures to narwhals, add polars support

WindowFeatures takes pandas and polars dataframes. With pandas it keeps
rolling on the index, including time-span windows, freq and all rolling
functions; it now calls the rolling methods instead of agg(), which is
faster with many variables. With polars the rows are used in the order
given, windows are numbers of rows, and mean, sum, std, var and count use
narwhals, while min, max, median, skew and kurt use polars expressions.
Time-span windows, other functions and min_periods=0 raise a clear error
with polars.

Also stops return_empty=True from failing with lists of windows or
functions when there are no numerical variables.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Mark polars output blocks in the user guide as text

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* move WindowFeatures to check_X and the forecasting helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* validate WindowFeatures parameters in init and simplify the window helper

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* support first, last, nunique, rank and sem in WindowFeatures with polars

With polars, WindowFeatures now takes the 15 pandas rolling functions that
aggregate one variable. sem uses narwhals, rank uses polars' rolling_rank,
and first, last and nunique aggregate the rows of each window. The docstring
and the user guide list the polars functions and link to the pandas ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* simplify WindowFeatures code and add the tests missing from main

check_window_parameters runs its checks in an if block instead of returning
early, and transform and _get_new_features_name turn window and functions into
lists inline instead of calling _functions_list.

Adds the cases main tested that the migrated tests missed: several windows with
periods and freq, drop_original with drop_na, the column order with several
windows and functions, and input_features as the pandas columns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* validate sort_index in WindowFeatures and align the tests with the conventions

WindowFeatures now rejects a sort_index that isn't a boolean, as LagFeatures
does. The tests check the column order of the window helper, vary every
init parameter, check the type of every output and parametrize the duplicated
functions error. The polars row-order test is removed, because the base tests
cover it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
* Fix integer overflow in geographic distance features

* Cast coordinates to float64 once, fix range checks on int minimums

Converting the coordinates to float64 in transform fixes the overflow for
all methods, and haversine no longer computes in float16 or float32 with
small integer dtypes. The range checks compare against both bounds, since
np.abs overflows on the minimum of signed integer dtypes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Soledad Galli <solegalli@protonmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

5 participants