Commit 04c88dc
Migrate BaseImputer to narwhals, add polars support (#1002)
* Migrate BaseImputer to narwhals, add polars support
Shared base for the imputation module: _transform() (fit-state checks +
column reorder) and transform() (fillna via imputer_dict_) are now
dataframe-agnostic, with _get_feature_names_in() reading columns through
narwhals on non-pandas input.
Benchmarked the fillna step (select + fill from a per-column value dict)
at 10k/100k/1M rows x 1/2/10 columns: pandas-native fillna runs ~1.3-1.6x
faster than the narwhals-generic fill_null equivalent at the 10k-100k
row sizes imputers are normally used at (the gap narrows to ~1.0x only
past ~1M rows) - a real, not minimal, loss, so pandas keeps its own fast
path (is_pandas = nwd.is_pandas_dataframe(X); if is_pandas is True: ...
else narwhals fill_null per column). Also benchmarked a numpy rewrite
(to_numpy + np.where per column, mirroring RelativeFeatures) but it did
not beat pandas-native and was consistently slower than narwhals
fill_null on polars, so it wasn't adopted here - unlike RelativeFeatures'
arithmetic, a plain value fill is already close to a no-op for both
pandas and narwhals/polars, leaving no room for a numpy win.
The pandas<3 fillna-downcasting workaround (option_context +
infer_objects) is preserved on the pandas branch but no longer imports
pandas at module level - the module is fetched via
nw.from_native(X).__native_namespace__() only once X is already
confirmed to be a pandas dataframe, so no import is attempted on a
polars-only install.
Verified: tests/test_imputation full suite unchanged (95 passed, 7
pre-existing failures in test_check_estimator_imputers.py - 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 on the file. 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>
* tidy code
* restore infer object
* remove reordering of the df
* Adapt BaseImputer to narwhals-returning check_X
Since #1019, check_X returns a narwhals DataFrame instead of the native
frame. BaseImputer._transform rebinds `X = check_X(X)` and returns it, so
transform() then sees a narwhals frame: nwd.is_pandas_dataframe(X) is always
False (and emits a UserWarning), skipping the pandas-native fillna fast path.
check_X is pure validation, so drop the rebinding and keep returning the
native X. transform()'s pandas / narwhals split then works as before, with
no warning.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>1 parent 0fca9c6 commit 04c88dc
1 file changed
Lines changed: 27 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
9 | | - | |
10 | | - | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | 33 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 34 | + | |
40 | 35 | | |
41 | 36 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | 37 | | |
46 | 38 | | |
47 | | - | |
| 39 | + | |
48 | 40 | | |
49 | 41 | | |
50 | 42 | | |
51 | 43 | | |
52 | 44 | | |
53 | | - | |
| 45 | + | |
54 | 46 | | |
55 | 47 | | |
56 | 48 | | |
57 | 49 | | |
58 | | - | |
| 50 | + | |
59 | 51 | | |
60 | 52 | | |
61 | | - | |
62 | 53 | | |
63 | 54 | | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
71 | 58 | | |
72 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
73 | 69 | | |
74 | 70 | | |
75 | 71 | | |
76 | 72 | | |
77 | | - | |
78 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
79 | 77 | | |
80 | 78 | | |
81 | 79 | | |
| |||
0 commit comments