Skip to content

Migrate PowerTransformer to narwhals, add polars support - #1005

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

Migrate PowerTransformer to narwhals, add polars support#1005
solegalli merged 1 commit into
narwhals-migrationfrom
narwhals-power-transformer

Conversation

@solegalli

Copy link
Copy Markdown
Collaborator

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.

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
solegalli merged commit 0e105a9 into narwhals-migration Aug 25, 2026
3 of 9 checks passed
@solegalli
solegalli deleted the narwhals-power-transformer branch August 25, 2026 19:30
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>
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