Migrate ArbitraryImputer to narwhals, add polars support - #1003
Merged
Conversation
solegalli
force-pushed
the
narwhals-migration
branch
from
August 30, 2026 15:40
04c88dc to
e9f7d69
Compare
solegalli
force-pushed
the
narwhals-arbitrary-imputer
branch
from
August 30, 2026 15:42
4498c69 to
e356c3b
Compare
solegalli
force-pushed
the
narwhals-migration
branch
from
August 30, 2026 15:49
e9f7d69 to
04c88dc
Compare
solegalli
force-pushed
the
narwhals-arbitrary-imputer
branch
from
August 30, 2026 15:49
e356c3b to
4498c69
Compare
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
force-pushed
the
narwhals-arbitrary-imputer
branch
from
August 30, 2026 16:00
4498c69 to
c500cfb
Compare
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.
No description provided.