@@ -251,6 +251,51 @@ def transform(self, X, y=None):
251
251
assert_array_equal (ct .transformers_ [- 1 ][2 ], [1 ])
252
252
253
253
254
+ @pytest .mark .parametrize ("pandas" , [True , False ], ids = ['pandas' , 'numpy' ])
255
+ @pytest .mark .parametrize ("column" , [[], np .array ([False , False ])],
256
+ ids = ['list' , 'bool' ])
257
+ def test_column_transformer_empty_columns (pandas , column ):
258
+ # test case that ensures that the column transformer does also work when
259
+ # a given transformer doesn't have any columns to work on
260
+ X_array = np .array ([[0 , 1 , 2 ], [2 , 4 , 6 ]]).T
261
+ X_res_both = X_array
262
+
263
+ if pandas :
264
+ pd = pytest .importorskip ('pandas' )
265
+ X = pd .DataFrame (X_array , columns = ['first' , 'second' ])
266
+ else :
267
+ X = X_array
268
+
269
+ ct = ColumnTransformer ([('trans1' , Trans (), [0 , 1 ]),
270
+ ('trans2' , Trans (), column )])
271
+ assert_array_equal (ct .fit_transform (X ), X_res_both )
272
+ assert_array_equal (ct .fit (X ).transform (X ), X_res_both )
273
+ assert len (ct .transformers_ ) == 2
274
+ assert isinstance (ct .transformers_ [1 ][1 ], Trans )
275
+
276
+ ct = ColumnTransformer ([('trans1' , Trans (), column ),
277
+ ('trans2' , Trans (), [0 , 1 ])])
278
+ assert_array_equal (ct .fit_transform (X ), X_res_both )
279
+ assert_array_equal (ct .fit (X ).transform (X ), X_res_both )
280
+ assert len (ct .transformers_ ) == 2
281
+ assert isinstance (ct .transformers_ [0 ][1 ], Trans )
282
+
283
+ ct = ColumnTransformer ([('trans' , Trans (), column )],
284
+ remainder = 'passthrough' )
285
+ assert_array_equal (ct .fit_transform (X ), X_res_both )
286
+ assert_array_equal (ct .fit (X ).transform (X ), X_res_both )
287
+ assert len (ct .transformers_ ) == 2 # including remainder
288
+ assert isinstance (ct .transformers_ [0 ][1 ], Trans )
289
+
290
+ fixture = np .array ([[], [], []])
291
+ ct = ColumnTransformer ([('trans' , Trans (), column )],
292
+ remainder = 'drop' )
293
+ assert_array_equal (ct .fit_transform (X ), fixture )
294
+ assert_array_equal (ct .fit (X ).transform (X ), fixture )
295
+ assert len (ct .transformers_ ) == 2 # including remainder
296
+ assert isinstance (ct .transformers_ [0 ][1 ], Trans )
297
+
298
+
254
299
def test_column_transformer_sparse_array ():
255
300
X_sparse = sparse .eye (3 , 2 ).tocsr ()
256
301
0 commit comments