Skip to content

Migrate YeoJohnsonTransformer to narwhals, add polars support - #1007

Merged
solegalli merged 1 commit into
narwhals-migrationfrom
narwhals-yeojohnson-transformer
Aug 25, 2026
Merged

solegalli merged 1 commit into
narwhals-migrationfrom
narwhals-yeojohnson-transformer

Conversation

@solegalli

Copy link
Copy Markdown
Collaborator

fit() learns a per-column lambda via scipy.stats.yeojohnson's optimizer search - that search dominates the runtime (10-800x the cost of transform/inverse_transform at 10k-100k rows), so the narwhals extraction overhead there is noise: benchmarked narwhals-on-pandas vs old pandas-native fit at 10k-100k rows x 1-10 cols and got ~0.97x-1.01x of the old runtime, i.e. parity.

transform() still calls scipy.stats.yeojohnson per column (its formula branches on a scalar lmbda, so it can't be vectorized across columns with different lambdas in one call) but now extracts to a single numpy array via to_numpy() first and reassigns via nw.new_series + with_columns. Benchmarked narwhals-on-pandas vs old .loc-assignment: 0.97x-1.2x of old runtime at realistic sizes (>=50k rows), degrading to ~1.9x at the smallest case tested (10k rows x 1 col) where both absolute times are sub-millisecond and dominated by call overhead rather than real work - in line with every other merged sibling in this module (Power/Reciprocal), so no pandas/polars branch was added.

inverse_transform()'s hand-written pos/neg-lambda formula no longer needs pandas.Series/.loc boolean-mask assignment - it now operates on a extracted numpy array per column instead, which benchmarked 1.4x-3.5x faster than the old code across the same size grid, on top of adding polars support for free.

Rewrote test_yeojohnson_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 DATA/DATA_NA dicts, same pattern as test_reciprocal_transformer.py). All expected values recomputed and verified against actual output. Kept test_inverse_with_non_linear_index pandas-only since it specifically exercises pandas Index-preserving behaviour with no polars equivalent.

Found the class docstring's pandas example values were already stale before this migration (verified against git-stashed pre-migration code: old code prints -267042.661354 for the first row, not the documented -267042.906453) - a scipy version drift in the yeojohnson lambda optimizer, unrelated to this migration. Fixed both the pandas example and added a verified "With polars" section to
docs/user_guide/transformation/YeoJohnsonTransformer.rst.

Left the pre-existing Ames-housing fetch_openml walkthrough in the docs untouched: the OpenML house_prices snapshot/sklearn parser now returns different row order than when the doc was written (X_train.head() shows different indices/houses than documented), which is upstream drift unrelated to this migration and would require regenerating the large embedded data table and histogram PNGs to fix properly - flagging for a separate follow-up rather than doing it here.

Verified: pytest tests/test_transformation (140 passed, same 8 pre-existing check_estimator failures as baseline, zero new failures), flake8 and mypy clean on the touched files, sphinx -W build clean (only the pre-existing unrelated linkcode_resolve warning), and the module imports standalone with pandas import blocked.

fit() learns a per-column lambda via scipy.stats.yeojohnson's optimizer
search - that search dominates the runtime (10-800x the cost of
transform/inverse_transform at 10k-100k rows), so the narwhals
extraction overhead there is noise: benchmarked narwhals-on-pandas vs
old pandas-native fit at 10k-100k rows x 1-10 cols and got ~0.97x-1.01x
of the old runtime, i.e. parity.

transform() still calls scipy.stats.yeojohnson per column (its formula
branches on a scalar lmbda, so it can't be vectorized across columns
with different lambdas in one call) but now extracts to a single numpy
array via to_numpy() first and reassigns via nw.new_series +
with_columns. Benchmarked narwhals-on-pandas vs old .loc-assignment:
0.97x-1.2x of old runtime at realistic sizes (>=50k rows), degrading to
~1.9x at the smallest case tested (10k rows x 1 col) where both
absolute times are sub-millisecond and dominated by call overhead
rather than real work - in line with every other merged sibling in
this module (Power/Reciprocal), so no pandas/polars branch was added.

inverse_transform()'s hand-written pos/neg-lambda formula no longer
needs pandas.Series/.loc boolean-mask assignment - it now operates on
a extracted numpy array per column instead, which benchmarked 1.4x-3.5x
*faster* than the old code across the same size grid, on top of adding
polars support for free.

Rewrote test_yeojohnson_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 DATA/DATA_NA
dicts, same pattern as test_reciprocal_transformer.py). All expected
values recomputed and verified against actual output. Kept
test_inverse_with_non_linear_index pandas-only since it specifically
exercises pandas Index-preserving behaviour with no polars equivalent.

Found the class docstring's pandas example values were already stale
before this migration (verified against git-stashed pre-migration code:
old code prints -267042.661354 for the first row, not the documented
-267042.906453) - a scipy version drift in the yeojohnson lambda
optimizer, unrelated to this migration. Fixed both the pandas example
and added a verified "With polars" section to
docs/user_guide/transformation/YeoJohnsonTransformer.rst.

Left the pre-existing Ames-housing fetch_openml walkthrough in the docs
untouched: the OpenML house_prices snapshot/sklearn parser now returns
different row order than when the doc was written (X_train.head()
shows different indices/houses than documented), which is upstream
drift unrelated to this migration and would require regenerating the
large embedded data table and histogram PNGs to fix properly - flagging
for a separate follow-up rather than doing it here.

Verified: pytest tests/test_transformation (140 passed, same 8
pre-existing check_estimator failures as baseline, zero new failures),
flake8 and mypy clean on the touched files, sphinx -W build clean
(only the pre-existing unrelated linkcode_resolve warning), and the
module imports standalone with pandas import blocked.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@solegalli
solegalli merged commit 5695fa7 into narwhals-migration Aug 25, 2026
3 of 9 checks passed
@solegalli
solegalli deleted the narwhals-yeojohnson-transformer branch August 25, 2026 19:47
solegalli added a commit that referenced this pull request Aug 30, 2026
fit() learns a per-column lambda via scipy.stats.yeojohnson's optimizer
search - that search dominates the runtime (10-800x the cost of
transform/inverse_transform at 10k-100k rows), so the narwhals
extraction overhead there is noise: benchmarked narwhals-on-pandas vs
old pandas-native fit at 10k-100k rows x 1-10 cols and got ~0.97x-1.01x
of the old runtime, i.e. parity.

transform() still calls scipy.stats.yeojohnson per column (its formula
branches on a scalar lmbda, so it can't be vectorized across columns
with different lambdas in one call) but now extracts to a single numpy
array via to_numpy() first and reassigns via nw.new_series +
with_columns. Benchmarked narwhals-on-pandas vs old .loc-assignment:
0.97x-1.2x of old runtime at realistic sizes (>=50k rows), degrading to
~1.9x at the smallest case tested (10k rows x 1 col) where both
absolute times are sub-millisecond and dominated by call overhead
rather than real work - in line with every other merged sibling in
this module (Power/Reciprocal), so no pandas/polars branch was added.

inverse_transform()'s hand-written pos/neg-lambda formula no longer
needs pandas.Series/.loc boolean-mask assignment - it now operates on
a extracted numpy array per column instead, which benchmarked 1.4x-3.5x
*faster* than the old code across the same size grid, on top of adding
polars support for free.

Rewrote test_yeojohnson_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 DATA/DATA_NA
dicts, same pattern as test_reciprocal_transformer.py). All expected
values recomputed and verified against actual output. Kept
test_inverse_with_non_linear_index pandas-only since it specifically
exercises pandas Index-preserving behaviour with no polars equivalent.

Found the class docstring's pandas example values were already stale
before this migration (verified against git-stashed pre-migration code:
old code prints -267042.661354 for the first row, not the documented
-267042.906453) - a scipy version drift in the yeojohnson lambda
optimizer, unrelated to this migration. Fixed both the pandas example
and added a verified "With polars" section to
docs/user_guide/transformation/YeoJohnsonTransformer.rst.

Left the pre-existing Ames-housing fetch_openml walkthrough in the docs
untouched: the OpenML house_prices snapshot/sklearn parser now returns
different row order than when the doc was written (X_train.head()
shows different indices/houses than documented), which is upstream
drift unrelated to this migration and would require regenerating the
large embedded data table and histogram PNGs to fix properly - flagging
for a separate follow-up rather than doing it here.

Verified: pytest tests/test_transformation (140 passed, same 8
pre-existing check_estimator failures as baseline, zero new failures),
flake8 and mypy clean on the touched files, sphinx -W build clean
(only the pre-existing unrelated linkcode_resolve warning), and the
module imports standalone with pandas import blocked.

Co-authored-by: Claude Sonnet 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.

1 participant