Skip to content

Commit 78ead34

Browse files
committed
fallback if no select_columns_by_name is present
1 parent d12dc1f commit 78ead34

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

Diff for: packages/python/plotly/plotly/express/_core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ def build_dataframe(args, constructor):
13961396

13971397
if needs_interchanging:
13981398
try:
1399-
if wide_mode:
1399+
if wide_mode or not hasattr(args["data_frame"], "select_columns_by_name"):
14001400
args["data_frame"] = pd.api.interchange.from_dataframe(
14011401
args["data_frame"]
14021402
)

Diff for: packages/python/plotly/plotly/tests/test_optional/test_px/test_px_input.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)