Skip to content

CLN Removes duplicated or unneeded code in ColumnTransformer #19261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions sklearn/compose/_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ..utils import Bunch
from ..utils import _safe_indexing
from ..utils import _get_column_indices
from ..utils import _determine_key_type
from ..utils.metaestimators import _BaseComposition
from ..utils.validation import check_array, check_is_fitted
from ..utils.validation import _deprecate_positional_args
Expand Down Expand Up @@ -320,12 +319,6 @@ def _validate_remainder(self, X):
"'passthrough', or estimator. '%s' was passed instead" %
self.remainder)

# Make it possible to check for reordered named columns on transform
self._has_str_cols = any(_determine_key_type(cols) == 'str'
for cols in self._columns)
if hasattr(X, 'columns'):
self._df_columns = X.columns

self._n_features = X.shape[1]
cols = []
for columns in self._columns:
Expand Down Expand Up @@ -362,12 +355,12 @@ def get_feature_names(self):
hasattr(column, '__len__') and not len(column)):
continue
if trans == 'passthrough':
if hasattr(self, '_df_columns'):
if self._feature_names_in is not None:
if ((not isinstance(column, slice))
and all(isinstance(col, str) for col in column)):
feature_names.extend(column)
else:
feature_names.extend(self._df_columns[column])
feature_names.extend(self._feature_names_in[column])
else:
indices = np.arange(self._n_features)
feature_names.extend(['x%d' % i for i in indices[column]])
Expand Down Expand Up @@ -441,7 +434,7 @@ def _fit_transform(self, X, y, func, fitted=False):
message_clsname='ColumnTransformer',
message=self._log_message(name, idx, len(transformers)))
for idx, (name, trans, column, weight) in enumerate(
self._iter(fitted=fitted, replace_strings=True), 1))
transformers, 1))
except ValueError as e:
if "Expected 2D array, got 1D array instead" in str(e):
raise ValueError(_ERR_MSG_1DCOLUMN) from e
Expand Down Expand Up @@ -606,9 +599,9 @@ def _sk_visual_block_(self):
transformers = self.transformers
elif hasattr(self, "_remainder"):
remainder_columns = self._remainder[2]
if hasattr(self, '_df_columns'):
if self._feature_names_in is not None:
remainder_columns = (
self._df_columns[remainder_columns].tolist()
self._feature_names_in[remainder_columns].tolist()
)
transformers = chain(self.transformers,
[('remainder', self.remainder,
Expand Down