Skip to content

Commit dff2c11

Browse files
raise errors on multi-index in column/index in wide mode
1 parent a85acf6 commit dff2c11

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/python/plotly/plotly/express/_core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,12 @@ def build_dataframe(args, constructor):
12151215
)
12161216
if df_provided and no_x and no_y:
12171217
wide_mode = True
1218+
if isinstance(df_input.columns, pd.MultiIndex):
1219+
raise TypeError(
1220+
"Data frame columns is a pandas MultiIndex. "
1221+
"pandas MultiIndex is not supported by plotly express "
1222+
"at the moment."
1223+
)
12181224
args["wide_variable"] = list(df_input.columns)
12191225
var_name = df_input.columns.name or "variable"
12201226
if constructor == go.Funnel:
@@ -1251,6 +1257,12 @@ def build_dataframe(args, constructor):
12511257
if no_x != no_y and args["orientation"] is None:
12521258
args["orientation"] = "v" if no_x else "h"
12531259
if df_provided:
1260+
if isinstance(df_input.index, pd.MultiIndex):
1261+
raise TypeError(
1262+
"Data frame index is a pandas MultiIndex. "
1263+
"pandas MultiIndex is not supported by plotly express "
1264+
"at the moment."
1265+
)
12541266
args["wide_cross"] = df_input.index
12551267
wide_cross_name = df_input.index.name or "index"
12561268
else:

packages/python/plotly/plotly/tests/test_core/test_px/test_px_wide.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,21 @@ def test_wide_mode_internal_special_cases(df_in, args_in, args_expect, df_expect
628628
df_out.sort_index(axis=1), df_expect.sort_index(axis=1),
629629
)
630630
assert args_out == args_expect
631+
632+
633+
def test_multi_index():
634+
df = pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6], [1, 2, 3, 4], [3, 4, 5, 6]])
635+
df.index = [["a", "a", "b", "b"], ["c", "d", "c", "d"]]
636+
with pytest.raises(TypeError) as err_msg:
637+
px.scatter(df)
638+
assert "pandas MultiIndex is not supported by plotly express" in str(
639+
err_msg.value
640+
)
641+
642+
df = pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6], [1, 2, 3, 4], [3, 4, 5, 6]])
643+
df.columns = [["e", "e", "f", "f"], ["g", "h", "g", "h"]]
644+
with pytest.raises(TypeError) as err_msg:
645+
px.scatter(df)
646+
assert "pandas MultiIndex is not supported by plotly express" in str(
647+
err_msg.value
648+
)

0 commit comments

Comments
 (0)