Skip to content

Commit d9fbf78

Browse files
solegalliclaude
andcommitted
Adapt MeanEncoder to narwhals-returning check_X
check_X_y now returns a narwhals frame, so bind that to nw_X and keep the original native X for _check_or_select_variables, _check_na, _get_feature_names_in and the nwd.is_pandas_dataframe(X) fast-path check (those helpers still expect native input, matching the CategoricalImputer migration on narwhals-migration). The pandas value_counts/groupby fast path is unchanged - X stays native so no rehydration is needed. The narwhals branch reuses nw_X from check_X_y instead of nw.from_native(X). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent bb9d673 commit d9fbf78

1 file changed

Lines changed: 2 additions & 5 deletions

File tree

feature_engine/encoding/mean_encoding.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def fit(self, X: IntoDataFrame, y: IntoSeries):
219219
The target.
220220
"""
221221

222-
X, y = check_X_y(X, y)
222+
nw_X, y = check_X_y(X, y)
223223
variables_ = self._check_or_select_variables(X)
224224
self._check_na(X, variables_)
225225

@@ -230,9 +230,7 @@ def fit(self, X: IntoDataFrame, y: IntoSeries):
230230
# at low column counts (the common case), so pandas keeps its native
231231
# groupby/value_counts fast path and only polars (and other
232232
# backends) go through narwhals.
233-
is_pandas = nwd.is_pandas_dataframe(X)
234-
235-
if is_pandas is True:
233+
if nwd.is_pandas_dataframe(X):
236234
y_prior = y.mean()
237235

238236
if self.unseen == "encode":
@@ -265,7 +263,6 @@ def fit(self, X: IntoDataFrame, y: IntoSeries):
265263
+ (1.0 - _lambda) * y_prior
266264
).to_dict()
267265
else:
268-
nw_X = nw.from_native(X, eager_only=True)
269266
target_name = "__feature_engine_mean_target__"
270267
if nwd.is_into_series(y):
271268
y_nw = nw.from_native(y, series_only=True).alias(target_name)

0 commit comments

Comments
 (0)