Skip to content

migrate missing indicator to narwhals, add polar support - #1001

Merged
solegalli merged 3 commits into
narwhals-migrationfrom
narwhals-missing-indicator
Aug 30, 2026
Merged

migrate missing indicator to narwhals, add polar support#1001
solegalli merged 3 commits into
narwhals-migrationfrom
narwhals-missing-indicator

Conversation

@solegalli

Copy link
Copy Markdown
Collaborator

No description provided.

@solegalli
solegalli force-pushed the narwhals-migration branch 2 times, most recently from e9f7d69 to 04c88dc Compare August 30, 2026 15:49
solegalli and others added 2 commits August 30, 2026 18:06
…support

Removed the module-level `import pandas as pd`; X/y type hints now use
narwhals' IntoDataFrame/IntoSeries. This file overrides transform() rather
than extending BaseImputer's, so both the fit() null-count filter and the
transform() indicator-column step needed their own narwhals path.

Benchmarked both operations at 10k/50k/100k rows x 1/2/10 columns (varying
how many columns need indicators), plus a mixed string+numeric-dtype
dataset matching MissingIndicator's real "all variable types" usage:

- fit()'s `[var for var in variables_ if X[var].isnull().sum() > 0]` loop
  is ~2-5x faster on pandas than a narwhals-generic `null_count()` call
  (e.g. 100k rows x 10 cols: 0.41ms loop vs 0.78ms narwhals-on-pandas).
  A vectorized `X[variables_].isnull().sum()` alternative didn't beat the
  loop either. narwhals-on-polars was consistently fastest of all (its own
  native path), so the split is pandas-loop vs narwhals-generic (used for
  polars/other backends), matching BaseImputer's is_pandas branch pattern.

- transform()'s `X[vars].isna().astype("int8").add_suffix("_na")` +
  `pd.concat` is ~2-5x faster on pandas than narwhals' with_columns
  equivalent (100k rows x 10 cols: 0.28ms concat vs 1.27ms narwhals-on-
  pandas), and also beats `assign()`-per-column (0.91ms) and `join()`
  (0.44ms) alternatives - concat already batches all new columns in one
  op. So transform() keeps the same pandas fast path, split from a
  narwhals with_columns path for other backends.

Both losses are >1.7x, past the "keep pandas fast path" threshold, so
merging into one narwhals-generic path (as BaseImputer's docstring
discusses for its own fillna step) was not justified here either.

Numpy: converting columns via `.to_numpy()` + `pd.isna()` (the only numpy
op that works across MissingIndicator's mixed string/numeric columns,
since np.isnan raises on object arrays) was consistently ~1.7-2x slower
than pandas-native isnull()/isna() for both fit and transform on mixed
dtypes - the extra .to_numpy() copy plus pd.isna() dispatch outweighs any
gain, same conclusion as BaseImputer's fillna numpy experiment.

Tests: converted tests/test_imputation/test_missing_indicator.py from the
pandas-only `df_na` fixture to a plain DATA dict parametrized over
`make_df` in [pd.DataFrame, pl.DataFrame], asserting identical variables_
selection and identical `<var>_na` column values on both backends for the
same input (one cross-backend PerformanceWarning regression test stays
pandas-only, since it targets the pandas fast path specifically).

Docs: docs/user_guide/imputation/MissingIndicator.rst has no inline
printed output to go stale (it references a screenshot image instead of
doctest-style text) - verified its house_prices code example's logic
against the migrated transformer with a synthetic stand-in dataset (no
network access in this environment) and it behaves identically. Added a
verified "With polars" example to the class docstring.

Verified: tests/test_imputation/test_missing_indicator.py 29 passed.
tests/test_imputation full suite: 107 passed / 7 pre-existing failures
in test_check_estimator_imputers.py (confirmed identical failures against
a baseline run of origin/narwhals-imputation-base: 95 passed / same 7
failures - sklearn's check_estimator feeds raw numpy arrays, which
check_X() has always rejected per the narwhals migration's dataframe-only
contract; predates this change). 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
find/check_all_variables, the is_pandas null-count fast path and
_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-missing-indicator branch from 2bc5c86 to 2fc0cd4 Compare August 30, 2026 16:07
@solegalli
solegalli merged commit 614493f into narwhals-migration Aug 30, 2026
3 of 10 checks passed
@solegalli
solegalli deleted the narwhals-missing-indicator branch August 30, 2026 17:22
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