Migrate PowerTransformer to narwhals, add polars support - #1005
Merged
Conversation
Pure elementwise math (x ** exp), so followed the same precedent as ArcsinTransformer/ReciprocalTransformer (same module, same shape of problem): extract the transform columns to a single numpy array via narwhals' to_numpy(), apply np.power 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 ran in 0.47x-0.77x of the old runtime (avg 0.58x, i.e. ~1.7x faster), narwhals-on-polars faster still (avg 0.42x) - consistent with both sibling transformers, so no pandas/polars branch was added; both transform() and inverse_transform() use the same merged narwhals path. Rewrote test_power_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. Verified every code example already in docs/user_guide/transformation/PowerTransformer.rst against current output (including the fetch_openml/Ames-housing walkthrough - network was available this run) - all matched exactly, no doc fixes needed. Added a verified "With polars" section before the Considerations heading. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
solegalli
added a commit
that referenced
this pull request
Aug 30, 2026
Pure elementwise math (x ** exp), so followed the same precedent as ArcsinTransformer/ReciprocalTransformer (same module, same shape of problem): extract the transform columns to a single numpy array via narwhals' to_numpy(), apply np.power 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 ran in 0.47x-0.77x of the old runtime (avg 0.58x, i.e. ~1.7x faster), narwhals-on-polars faster still (avg 0.42x) - consistent with both sibling transformers, so no pandas/polars branch was added; both transform() and inverse_transform() use the same merged narwhals path. Rewrote test_power_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. Verified every code example already in docs/user_guide/transformation/PowerTransformer.rst against current output (including the fetch_openml/Ames-housing walkthrough - network was available this run) - all matched exactly, no doc fixes needed. Added a verified "With polars" section before the Considerations heading. 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.
Pure elementwise math (x ** exp), so followed the same precedent as ArcsinTransformer/ReciprocalTransformer (same module, same shape of problem): extract the transform columns to a single numpy array via narwhals' to_numpy(), apply np.power 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 ran in 0.47x-0.77x of the old runtime (avg 0.58x, i.e. ~1.7x faster), narwhals-on-polars faster still (avg 0.42x) - consistent with both sibling transformers, so no pandas/polars branch was added; both transform() and inverse_transform() use the same merged narwhals path.
Rewrote test_power_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.
Verified every code example already in
docs/user_guide/transformation/PowerTransformer.rst against current output (including the fetch_openml/Ames-housing walkthrough - network was available this run) - all matched exactly, no doc fixes needed. Added a verified "With polars" section before the Considerations heading.