Migrate YeoJohnsonTransformer to narwhals, add polars support - #1007
Merged
Merged
Conversation
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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.