@@ -253,29 +253,57 @@ def test_build_df_using_interchange_protocol_mock(
253253 add_interchange_module_for_old_pandas ,
254254):
255255 class InterchangeDataFrame :
256- def column_names (self ):
257- return []
256+ def __init__ (self , columns ):
257+ self . _columns = columns
258258
259- def select_columns_by_name (self , column_names ):
260- return self
259+ def column_names (self ):
260+ return self . _columns
261261
262- interchange_dataframe = InterchangeDataFrame ()
262+ interchange_dataframe = InterchangeDataFrame (
263+ ["petal_width" , "sepal_length" , "sepal_width" ]
264+ )
265+ interchange_dataframe_reduced = InterchangeDataFrame (
266+ ["petal_width" , "sepal_length" ]
267+ )
268+ interchange_dataframe .select_columns_by_name = mock .MagicMock (
269+ return_value = interchange_dataframe_reduced
270+ )
271+ interchange_dataframe_reduced .select_columns_by_name = mock .MagicMock (
272+ return_value = interchange_dataframe_reduced
273+ )
263274
264275 class CustomDataFrame :
265276 def __dataframe__ (self ):
266277 return interchange_dataframe
267278
279+ class CustomDataFrameReduced :
280+ def __dataframe__ (self ):
281+ return interchange_dataframe_reduced
282+
268283 input_dataframe = CustomDataFrame ()
269- args = dict ( data_frame = input_dataframe , x = "petal_width" , y = "sepal_length" )
284+ input_dataframe_reduced = CustomDataFrameReduced ( )
270285
271286 iris_pandas = px .data .iris ()
272287
273288 with mock .patch ("pandas.__version__" , "2.0.2" ):
289+ args = dict (data_frame = input_dataframe , x = "petal_width" , y = "sepal_length" )
274290 with mock .patch (
275291 "pandas.api.interchange.from_dataframe" , return_value = iris_pandas
276292 ) as mock_from_dataframe :
277293 build_dataframe (args , go .Scatter )
278- mock_from_dataframe .assert_called_once_with (interchange_dataframe )
294+ mock_from_dataframe .assert_called_once_with (interchange_dataframe_reduced )
295+ interchange_dataframe .select_columns_by_name .assert_called_with (
296+ ["petal_width" , "sepal_length" ]
297+ )
298+
299+ args = dict (data_frame = input_dataframe_reduced , color = None )
300+ with mock .patch (
301+ "pandas.api.interchange.from_dataframe" ,
302+ return_value = iris_pandas [["petal_width" , "sepal_length" ]],
303+ ) as mock_from_dataframe :
304+ build_dataframe (args , go .Scatter )
305+ mock_from_dataframe .assert_called_once_with (interchange_dataframe_reduced )
306+ interchange_dataframe_reduced .select_columns_by_name .assert_not_called ()
279307
280308
281309@pytest .mark .skipif (
0 commit comments