Skip to content

Commit 4d2ff3b

Browse files
authored
Update base_encoder.py
1 parent ae0a9c2 commit 4d2ff3b

1 file changed

Lines changed: 9 additions & 34 deletions

File tree

feature_engine/encoding/base_encoder.py

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,7 @@ def _get_feature_names_in(self, X: IntoDataFrame):
167167
standard for all transformers in the library.
168168
"""
169169
# save input features
170-
is_pandas = nwd.is_pandas_dataframe(X)
171-
if is_pandas is True:
172-
self.feature_names_in_ = list(X.columns)
173-
else:
174-
self.feature_names_in_ = nw.from_native(X, eager_only=True).columns
170+
self.feature_names_in_ = X.columns
175171

176172
# save train set shape
177173
self.n_features_in_ = X.shape[1]
@@ -203,23 +199,12 @@ def _check_transform_input_and_state(self, X: IntoDataFrame) -> IntoDataFrame:
203199
check_is_fitted(self)
204200

205201
# check that input is a dataframe
206-
X = check_X(X)
202+
nw_X = check_X(X)
207203

208204
# Check input data contains same number of columns as df used to fit
209205
_check_X_matches_training_df(X, self.n_features_in_)
210206

211-
# reorder df to match train set
212-
is_pandas = nwd.is_pandas_dataframe(X)
213-
if is_pandas is True:
214-
X = X[self.feature_names_in_]
215-
else:
216-
X = (
217-
nw.from_native(X, eager_only=True)
218-
.select(self.feature_names_in_)
219-
.to_native()
220-
)
221-
222-
return X
207+
return nw_X
223208

224209
def transform(self, X: IntoDataFrame) -> IntoDataFrame:
225210
"""Replace categories with the learned parameters.
@@ -235,44 +220,35 @@ def transform(self, X: IntoDataFrame) -> IntoDataFrame:
235220
The dataframe containing the categories replaced by numbers.
236221
"""
237222

238-
X = self._check_transform_input_and_state(X)
223+
nw_X = self._check_transform_input_and_state(X)
239224

240225
# check if dataset contains na
241226
if self.missing_values == "raise":
242227
_check_contains_na(X, self.variables_, error_msg="optional")
243228

244-
X = self._encode(X)
229+
X = self._encode(nw_X)
245230

246231
return X
247232

248233
def _encode(self, X: IntoDataFrame) -> IntoDataFrame:
249-
# replace categories by the learned parameters.
250-
# narwhals' replace_strict() lets one expression both map known
251-
# categories and fill unseen/missing ones via `default`, so the
252-
# pandas-only category-dtype fixup this used to need (map() leaves
253-
# category dtype behind) is no longer necessary: replace_strict
254-
# already resolves to a plain numeric dtype on both backends.
255-
# get_column()/Series.replace_strict() (rather than nw.col(), which
256-
# only accepts string names) is what lets this handle pandas
257-
# integer column names too, same as DecisionTreeFeatures.
258234
default = self._unseen if self.unseen == "encode" else None
259-
nw_X = nw.from_native(X, eager_only=True)
260235
new_series = [
261236
nw_X.get_column(feature).replace_strict(mapping, default=default)
262237
for feature, mapping in self.encoder_dict_.items()
263238
]
264-
X = nw_X.with_columns(*new_series).to_native()
239+
X = nw_X.with_columns(*new_series)
265240

266241
if self.unseen != "encode":
267242
# check if nan values were introduced by the transformation
268243
self._check_nan_values_after_transformation(X)
244+
245+
X = X.to_native()
269246

270247
return X
271248

272249
def _check_nan_values_after_transformation(self, X):
273250

274251
# check if NaN values were introduced by the encoding
275-
nw_X = nw.from_native(X, eager_only=True)
276252
nan_columns = [
277253
feature
278254
for feature in self.encoder_dict_.keys()
@@ -312,11 +288,10 @@ def inverse_transform(self, X: IntoDataFrame) -> IntoDataFrame:
312288
original values.
313289
"""
314290

315-
X = self._check_transform_input_and_state(X)
291+
nw_X = self._check_transform_input_and_state(X)
316292

317293
# replace encoded categories by the original values. get_column()
318294
# rather than nw.col() again, to support pandas integer column names.
319-
nw_X = nw.from_native(X, eager_only=True)
320295
new_series = [
321296
nw_X.get_column(feature).replace_strict(
322297
{v: k for k, v in mapping.items()}, default=None

0 commit comments

Comments
 (0)