8
8
# SPDX-License-Identifier: BSD-3-Clause
9
9
10
10
import warnings
11
- from collections import Counter , UserList
11
+ from collections import Counter
12
12
from functools import partial
13
13
from itertools import chain
14
14
from numbers import Integral , Real
@@ -161,11 +161,8 @@ class ColumnTransformer(TransformerMixin, _BaseComposition):
161
161
.. versionchanged:: 1.6
162
162
`verbose_feature_names_out` can be a callable or a string to be formatted.
163
163
164
- force_int_remainder_cols : bool, default=True
165
- Force the columns of the last entry of `transformers_`, which
166
- corresponds to the "remainder" transformer, to always be stored as
167
- indices (int) rather than column names (str). See description of the
168
- `transformers_` attribute for details.
164
+ force_int_remainder_cols : bool, default=False
165
+ This parameter has no effect.
169
166
170
167
.. note::
171
168
If you do not access the list of columns for the remainder columns
@@ -178,6 +175,9 @@ class ColumnTransformer(TransformerMixin, _BaseComposition):
178
175
The default value for `force_int_remainder_cols` will change from
179
176
`True` to `False` in version 1.7.
180
177
178
+ .. deprecated:: 1.7
179
+ `force_int_remainder_cols` is deprecated and will be removed in 1.9.
180
+
181
181
Attributes
182
182
----------
183
183
transformers_ : list
@@ -192,16 +192,12 @@ class ColumnTransformer(TransformerMixin, _BaseComposition):
192
192
``len(transformers_)==len(transformers)+1``, otherwise
193
193
``len(transformers_)==len(transformers)``.
194
194
195
- .. versionchanged:: 1.5
196
- If there are remaining columns and `force_int_remainder_cols` is
197
- True, the remaining columns are always represented by their
198
- positional indices in the input `X` (as in older versions). If
199
- `force_int_remainder_cols` is False, the format attempts to match
200
- that of the other transformers: if all columns were provided as
201
- column names (`str`), the remaining columns are stored as column
202
- names; if all columns were provided as mask arrays (`bool`), so are
203
- the remaining columns; in all other cases the remaining columns are
204
- stored as indices (`int`).
195
+ .. versionadded:: 1.7
196
+ The format of the remaining columns now attempts to match that of the other
197
+ transformers: if all columns were provided as column names (`str`), the
198
+ remaining columns are stored as column names; if all columns were provided
199
+ as mask arrays (`bool`), so are the remaining columns; in all other cases
200
+ the remaining columns are stored as indices (`int`).
205
201
206
202
named_transformers_ : :class:`~sklearn.utils.Bunch`
207
203
Read-only attribute to access any transformer by given name.
@@ -300,7 +296,7 @@ class ColumnTransformer(TransformerMixin, _BaseComposition):
300
296
"transformer_weights" : [dict , None ],
301
297
"verbose" : ["verbose" ],
302
298
"verbose_feature_names_out" : ["boolean" , str , callable ],
303
- "force_int_remainder_cols" : ["boolean" ],
299
+ "force_int_remainder_cols" : ["boolean" , Hidden ( StrOptions ({ "deprecated" })) ],
304
300
}
305
301
306
302
def __init__ (
@@ -313,7 +309,7 @@ def __init__(
313
309
transformer_weights = None ,
314
310
verbose = False ,
315
311
verbose_feature_names_out = True ,
316
- force_int_remainder_cols = True ,
312
+ force_int_remainder_cols = "deprecated" ,
317
313
):
318
314
self .transformers = transformers
319
315
self .remainder = remainder
@@ -477,13 +473,6 @@ def _iter(self, fitted, column_as_labels, skip_drop, skip_empty_columns):
477
473
if self ._remainder [2 ]:
478
474
transformers = chain (transformers , [self ._remainder ])
479
475
480
- # We want the warning about the future change of the remainder
481
- # columns dtype to be shown only when a user accesses them
482
- # directly, not when they are used by the ColumnTransformer itself.
483
- # We disable warnings here; they are enabled when setting
484
- # self.transformers_.
485
- transformers = _with_dtype_warning_enabled_set_to (False , transformers )
486
-
487
476
get_weight = (self .transformer_weights or {}).get
488
477
489
478
for name , trans , columns in transformers :
@@ -578,8 +567,6 @@ def _get_remainder_cols_dtype(self):
578
567
579
568
def _get_remainder_cols (self , indices ):
580
569
dtype = self ._get_remainder_cols_dtype ()
581
- if self .force_int_remainder_cols and dtype != "int" :
582
- return _RemainderColsList (indices , future_dtype = dtype )
583
570
if dtype == "str" :
584
571
return list (self .feature_names_in_ [indices ])
585
572
if dtype == "bool" :
@@ -753,7 +740,7 @@ def _update_fitted_transformers(self, transformers):
753
740
754
741
# sanity check that transformers is exhausted
755
742
assert not list (fitted_transformers )
756
- self .transformers_ = _with_dtype_warning_enabled_set_to ( True , transformers_ )
743
+ self .transformers_ = transformers_
757
744
758
745
def _validate_output (self , result ):
759
746
"""
@@ -984,6 +971,14 @@ def fit_transform(self, X, y=None, **params):
984
971
_raise_for_params (params , self , "fit_transform" )
985
972
_check_feature_names (self , X , reset = True )
986
973
974
+ if self .force_int_remainder_cols != "deprecated" :
975
+ warnings .warn (
976
+ "The parameter `force_int_remainder_cols` is deprecated and will be "
977
+ "removed in 1.9. It has no effect. Leave it to its default value to "
978
+ "avoid this warning." ,
979
+ FutureWarning ,
980
+ )
981
+
987
982
X = _check_X (X )
988
983
# set n_features_in_ attribute
989
984
_check_n_features (self , X , reset = True )
@@ -1380,7 +1375,7 @@ def make_column_transformer(
1380
1375
n_jobs = None ,
1381
1376
verbose = False ,
1382
1377
verbose_feature_names_out = True ,
1383
- force_int_remainder_cols = True ,
1378
+ force_int_remainder_cols = "deprecated" ,
1384
1379
):
1385
1380
"""Construct a ColumnTransformer from the given transformers.
1386
1381
@@ -1454,10 +1449,7 @@ def make_column_transformer(
1454
1449
.. versionadded:: 1.0
1455
1450
1456
1451
force_int_remainder_cols : bool, default=True
1457
- Force the columns of the last entry of `transformers_`, which
1458
- corresponds to the "remainder" transformer, to always be stored as
1459
- indices (int) rather than column names (str). See description of the
1460
- :attr:`ColumnTransformer.transformers_` attribute for details.
1452
+ This parameter has no effect.
1461
1453
1462
1454
.. note::
1463
1455
If you do not access the list of columns for the remainder columns
@@ -1470,6 +1462,9 @@ def make_column_transformer(
1470
1462
The default value for `force_int_remainder_cols` will change from
1471
1463
`True` to `False` in version 1.7.
1472
1464
1465
+ .. deprecated:: 1.7
1466
+ `force_int_remainder_cols` is deprecated and will be removed in version 1.9.
1467
+
1473
1468
Returns
1474
1469
-------
1475
1470
ct : ColumnTransformer
@@ -1596,105 +1591,6 @@ def __call__(self, df):
1596
1591
return cols .tolist ()
1597
1592
1598
1593
1599
- class _RemainderColsList (UserList ):
1600
- """A list that raises a warning whenever items are accessed.
1601
-
1602
- It is used to store the columns handled by the "remainder" entry of
1603
- ``ColumnTransformer.transformers_``, ie ``transformers_[-1][-1]``.
1604
-
1605
- For some values of the ``ColumnTransformer`` ``transformers`` parameter,
1606
- this list of indices will be replaced by either a list of column names or a
1607
- boolean mask; in those cases we emit a ``FutureWarning`` the first time an
1608
- element is accessed.
1609
-
1610
- Parameters
1611
- ----------
1612
- columns : list of int
1613
- The remainder columns.
1614
-
1615
- future_dtype : {'str', 'bool'}, default=None
1616
- The dtype that will be used by a ColumnTransformer with the same inputs
1617
- in a future release. There is a default value because providing a
1618
- constructor that takes a single argument is a requirement for
1619
- subclasses of UserList, but we do not use it in practice. It would only
1620
- be used if a user called methods that return a new list such are
1621
- copying or concatenating `_RemainderColsList`.
1622
-
1623
- warning_was_emitted : bool, default=False
1624
- Whether the warning for that particular list was already shown, so we
1625
- only emit it once.
1626
-
1627
- warning_enabled : bool, default=True
1628
- When False, the list never emits the warning nor updates
1629
- `warning_was_emitted``. This is used to obtain a quiet copy of the list
1630
- for use by the `ColumnTransformer` itself, so that the warning is only
1631
- shown when a user accesses it directly.
1632
- """
1633
-
1634
- def __init__ (
1635
- self ,
1636
- columns ,
1637
- * ,
1638
- future_dtype = None ,
1639
- warning_was_emitted = False ,
1640
- warning_enabled = True ,
1641
- ):
1642
- super ().__init__ (columns )
1643
- self .future_dtype = future_dtype
1644
- self .warning_was_emitted = warning_was_emitted
1645
- self .warning_enabled = warning_enabled
1646
-
1647
- def __getitem__ (self , index ):
1648
- self ._show_remainder_cols_warning ()
1649
- return super ().__getitem__ (index )
1650
-
1651
- def _show_remainder_cols_warning (self ):
1652
- if self .warning_was_emitted or not self .warning_enabled :
1653
- return
1654
- self .warning_was_emitted = True
1655
- future_dtype_description = {
1656
- "str" : "column names (of type str)" ,
1657
- "bool" : "a mask array (of type bool)" ,
1658
- # shouldn't happen because we always initialize it with a
1659
- # non-default future_dtype
1660
- None : "a different type depending on the ColumnTransformer inputs" ,
1661
- }.get (self .future_dtype , self .future_dtype )
1662
-
1663
- # TODO(1.7) Update the warning to say that the old behavior will be
1664
- # removed in 1.9.
1665
- warnings .warn (
1666
- (
1667
- "\n The format of the columns of the 'remainder' transformer in"
1668
- " ColumnTransformer.transformers_ will change in version 1.7 to"
1669
- " match the format of the other transformers.\n At the moment the"
1670
- " remainder columns are stored as indices (of type int). With the same"
1671
- " ColumnTransformer configuration, in the future they will be stored"
1672
- f" as { future_dtype_description } .\n To use the new behavior now and"
1673
- " suppress this warning, use"
1674
- " ColumnTransformer(force_int_remainder_cols=False).\n "
1675
- ),
1676
- category = FutureWarning ,
1677
- )
1678
-
1679
- def _repr_pretty_ (self , printer , * _ ):
1680
- """Override display in ipython console, otherwise the class name is shown."""
1681
- printer .text (repr (self .data ))
1682
-
1683
-
1684
- def _with_dtype_warning_enabled_set_to (warning_enabled , transformers ):
1685
- result = []
1686
- for name , trans , columns in transformers :
1687
- if isinstance (columns , _RemainderColsList ):
1688
- columns = _RemainderColsList (
1689
- columns .data ,
1690
- future_dtype = columns .future_dtype ,
1691
- warning_was_emitted = columns .warning_was_emitted ,
1692
- warning_enabled = warning_enabled ,
1693
- )
1694
- result .append ((name , trans , columns ))
1695
- return result
1696
-
1697
-
1698
1594
def _feature_names_out_with_str_format (
1699
1595
transformer_name : str , feature_name : str , str_format : str
1700
1596
) -> str :
0 commit comments