diff --git a/sklearn/base.py b/sklearn/base.py index 0bd9327c38166..3ef2a908cdadf 100644 --- a/sklearn/base.py +++ b/sklearn/base.py @@ -417,8 +417,7 @@ def _check_feature_names(self, X, *, reset): fitted_feature_names != X_feature_names ): message = ( - "The feature names should match those that were " - "passed during fit. Starting version 1.2, an error will be raised.\n" + "The feature names should match those that were passed during fit.\n" ) fitted_feature_names_set = set(fitted_feature_names) X_feature_names_set = set(X_feature_names) @@ -449,7 +448,7 @@ def add_names(names): "Feature names must be in the same order as they were in fit.\n" ) - warnings.warn(message, FutureWarning) + raise ValueError(message) def _validate_data( self, diff --git a/sklearn/tests/test_base.py b/sklearn/tests/test_base.py index 6e17721046e9b..934bf7719163c 100644 --- a/sklearn/tests/test_base.py +++ b/sklearn/tests/test_base.py @@ -615,7 +615,7 @@ def transform(self, X): trans.fit(df) msg = "The feature names should match those that were passed" df_bad = pd.DataFrame(X_np, columns=iris.feature_names[::-1]) - with pytest.warns(FutureWarning, match=msg): + with pytest.raises(ValueError, match=msg): trans.transform(df_bad) # warns when fitted on dataframe and transforming a ndarray diff --git a/sklearn/utils/estimator_checks.py b/sklearn/utils/estimator_checks.py index 90efa06073487..8a1f4eb95583a 100644 --- a/sklearn/utils/estimator_checks.py +++ b/sklearn/utils/estimator_checks.py @@ -3913,22 +3913,14 @@ def check_dataframe_column_names_consistency(name, estimator_orig): X_bad = pd.DataFrame(X, columns=invalid_name) expected_msg = re.escape( - "The feature names should match those that were passed " - "during fit. Starting version 1.2, an error will be raised.\n" + "The feature names should match those that were passed during fit.\n" f"{additional_message}" ) for name, method in check_methods: - # TODO In 1.2, this will be an error. - with warnings.catch_warnings(): - warnings.filterwarnings( - "error", - category=FutureWarning, - module="sklearn", - ) - with raises( - FutureWarning, match=expected_msg, err_msg=f"{name} did not raise" - ): - method(X_bad) + with raises( + ValueError, match=expected_msg, err_msg=f"{name} did not raise" + ): + method(X_bad) # partial_fit checks on second call # Do not call partial fit if early_stopping is on @@ -3942,10 +3934,8 @@ def check_dataframe_column_names_consistency(name, estimator_orig): else: estimator.partial_fit(X, y) - with warnings.catch_warnings(): - warnings.filterwarnings("error", category=FutureWarning, module="sklearn") - with raises(FutureWarning, match=expected_msg): - estimator.partial_fit(X_bad, y) + with raises(ValueError, match=expected_msg): + estimator.partial_fit(X_bad, y) def check_transformer_get_feature_names_out(name, transformer_orig):