Skip to content

Migrate ArbitraryImputer to narwhals, add polars support - #1003

Merged
solegalli merged 2 commits into
narwhals-migrationfrom
narwhals-arbitrary-imputer
Aug 30, 2026
Merged

Migrate ArbitraryImputer to narwhals, add polars support#1003
solegalli merged 2 commits into
narwhals-migrationfrom
narwhals-arbitrary-imputer

Conversation

@solegalli

Copy link
Copy Markdown
Collaborator

No description provided.

@solegalli
solegalli force-pushed the narwhals-arbitrary-imputer branch from 4498c69 to e356c3b Compare August 30, 2026 15:42
@solegalli
solegalli force-pushed the narwhals-arbitrary-imputer branch from e356c3b to 4498c69 Compare August 30, 2026 15:49
solegalli and others added 2 commits August 30, 2026 18:00
fit() never touches dataframe values - it only calls the already-
narwhals-migrated check_X/check_numerical_variables/find_numerical_variables
and builds imputer_dict_ via a plain dict comprehension over column names -
so the only change needed was dropping the module-level `import pandas as
pd` and swapping the X/y type hints for narwhals' IntoDataFrame/IntoSeries.
transform() is fully inherited from the already-migrated BaseImputer.

Benchmarked fit()+transform() (via fit_transform) at 10k/50k/100k rows x
1/2/10 cols, pandas vs polars, and old code vs migrated code on pandas
input: fit() takes ~0.06-0.13ms regardless of row count, column count, or
backend, both before and after the edit (within noise of each other) -
confirming fit() truly does no per-row work. No backend split was needed
or added; a single narwhals-agnostic path was kept (it already was one).

Numpy: not applicable - fit() has no numeric computation over data at all,
only dict/list building over variable names, so there is nothing for numpy
to accelerate.

While touching fit(), changed `if self.imputer_dict:` to
`if self.imputer_dict is not None:` per AGENTS.md's ban on truthy
container checks; this also fixes a latent edge case where imputer_dict={}
was silently treated as "not provided" and fell through to the
variables/arbitrary_number branch. Confirmed pre-existing on
origin/narwhals-imputation-base (unrelated to this migration, no test
previously covered it).

Rewrote tests/test_imputation/test_arbitrary_imputer.py to the
cross-backend parametrized style (@pytest.mark.parametrize("make_df",
[pd.DataFrame, pl.DataFrame])) in place, replacing the pandas-only df_na
fixture and pd.testing.assert_frame_equal/.isnull() assertions with a
plain DATA dict and narwhals-based null/value assertions. The
deprecation-warning test for ArbitraryNumberImputer and the
arbitrary_number-type-validation test stayed single-backend since they
never touch a dataframe.

Added a "With polars" section to both the class docstring and
docs/user_guide/imputation/ArbitraryImputer.rst, output verified by
actually running the transformer. No staleness found in the existing rst
(it builds its example from fetch_openml, no literal printed dataframe
values to go stale).

Verified: tests/test_imputation full suite 98 passed / 7 pre-existing
unrelated failures in test_check_estimator_imputers.py (same 7 as on
origin/narwhals-imputation-base's baseline of 95 passed - the 3 extra
passes here are the new cross-backend parametrization, no regressions).
flake8 and mypy clean. Module imports with pandas import blocked.
sphinx -W build clean (only the pre-existing unrelated linkcode_resolve
warning).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
check_X now returns a narwhals DataFrame; fit() passed it to
check_numerical_variables / find_numerical_variables / _get_feature_names_in,
which then took their non-pandas path (spurious is_pandas_dataframe warning,
hard failure on integer column names). check_X is pure validation, so stop
rebinding X and keep working with the native input.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@solegalli
solegalli force-pushed the narwhals-arbitrary-imputer branch from 4498c69 to c500cfb Compare August 30, 2026 16:00
@solegalli
solegalli merged commit 8049df6 into narwhals-migration Aug 30, 2026
4 of 10 checks passed
@solegalli
solegalli deleted the narwhals-arbitrary-imputer branch August 30, 2026 17:05
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