@@ -33,7 +33,9 @@ def test__wrap_in_pandas_container_dense_update_columns_and_index():
3333
3434 new_df = _wrap_in_pandas_container (X_df , columns = new_columns , index = new_index )
3535 assert_array_equal (new_df .columns , new_columns )
36- assert_array_equal (new_df .index , new_index )
36+
37+ # Index does not change when the input is a DataFrame
38+ assert_array_equal (new_df .index , X_df .index )
3739
3840
3941def test__wrap_in_pandas_container_error_validation ():
@@ -260,3 +262,33 @@ class C(A, B):
260262 pass
261263
262264 assert C ().transform (None ) == "B"
265+
266+
267+ class EstimatorWithSetOutputIndex (_SetOutputMixin ):
268+ def fit (self , X , y = None ):
269+ self .n_features_in_ = X .shape [1 ]
270+ return self
271+
272+ def transform (self , X , y = None ):
273+ import pandas as pd
274+
275+ # transform by giving output a new index.
276+ return pd .DataFrame (X .to_numpy (), index = [f"s{ i } " for i in range (X .shape [0 ])])
277+
278+ def get_feature_names_out (self , input_features = None ):
279+ return np .asarray ([f"X{ i } " for i in range (self .n_features_in_ )], dtype = object )
280+
281+
282+ def test_set_output_pandas_keep_index ():
283+ """Check that set_output does not override index.
284+
285+ Non-regression test for gh-25730.
286+ """
287+ pd = pytest .importorskip ("pandas" )
288+
289+ X = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], index = [0 , 1 ])
290+ est = EstimatorWithSetOutputIndex ().set_output (transform = "pandas" )
291+ est .fit (X )
292+
293+ X_trans = est .transform (X )
294+ assert_array_equal (X_trans .index , ["s0" , "s1" ])
0 commit comments