Skip to content

Commit 773b669

Browse files
special case for wide-var=columns
1 parent 0b442b1 commit 773b669

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,13 @@ def build_dataframe(args, constructor):
12391239
elif wide_x != wide_y:
12401240
wide_mode = True
12411241
args["wide_variable"] = args["y"] if wide_y else args["x"]
1242-
var_name = "variable"
1242+
if df_provided and args["wide_variable"] is df_input.columns:
1243+
var_name = df_input.columns.name
1244+
args["wide_variable"] = list(args["wide_variable"])
1245+
if var_name in [None, "value", "index"] or (
1246+
df_provided and var_name in df_input
1247+
):
1248+
var_name = "variable"
12431249
if constructor == go.Histogram:
12441250
wide_orientation = "v" if wide_x else "h"
12451251
else:

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,19 @@ def append_special_case(df_in, args_in, args_expect, df_expect):
663663
),
664664
)
665665

666+
# y = columns
667+
df = pd.DataFrame(dict(a=[1, 2], b=[3, 4]), index=[7, 8])
668+
df.index.name = "c"
669+
df.columns.name = "d"
670+
append_special_case(
671+
df_in=df,
672+
args_in=dict(x=df.index, y=df.columns, color=None),
673+
args_expect=dict(x="c", y="value", color="d"),
674+
df_expect=pd.DataFrame(
675+
dict(c=[7, 8, 7, 8], d=["a", "a", "b", "b"], value=[1, 2, 3, 4])
676+
),
677+
)
678+
666679

667680
@pytest.mark.parametrize("df_in, args_in, args_expect, df_expect", special_cases)
668681
def test_wide_mode_internal_special_cases(df_in, args_in, args_expect, df_expect):

0 commit comments

Comments
 (0)