Skip to content

Commit 9d45dc8

Browse files
wip
1 parent 7e129b2 commit 9d45dc8

File tree

1 file changed

+38
-14
lines changed
  • packages/python/plotly/plotly/express

1 file changed

+38
-14
lines changed

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
+ ["ids", "error_x", "error_x_minus", "error_y", "error_y_minus", "error_z"]
2424
+ ["error_z_minus", "lat", "lon", "locations", "animation_group"]
2525
)
26-
array_attrables = ["dimensions", "custom_data", "hover_data", "path"]
26+
array_attrables = ["dimensions", "custom_data", "hover_data", "path", "wide_cols"]
2727
group_attrables = ["animation_frame", "facet_row", "facet_col", "line_group"]
2828
renameable_group_attrables = [
2929
"color", # renamed to marker.color or line.color in infer_config
@@ -942,7 +942,6 @@ def build_dataframe(args, constructor):
942942
df_provided = args["data_frame"] is not None
943943
if df_provided and not isinstance(args["data_frame"], pd.DataFrame):
944944
args["data_frame"] = pd.DataFrame(args["data_frame"])
945-
946945
df_input = args["data_frame"]
947946

948947
no_x = args.get("x", None) is None
@@ -952,10 +951,35 @@ def build_dataframe(args, constructor):
952951
wide_id_vars = set()
953952

954953
if wide_mode:
955-
df_output = df_input
956-
var_name = df_output.columns.name or "_column_"
957-
else:
958-
df_output = pd.DataFrame()
954+
# currently assuming that df_provided == True
955+
args["wide_cols"] = [df_input.index] + list(df_input.columns)
956+
var_name = df_input.columns.name or "_column_"
957+
index_name = df_input.index.name or "index"
958+
wide_id_vars.add(index_name)
959+
960+
"""
961+
wide_x detection
962+
- if scalar = False
963+
- else if list of lists = True
964+
- else if not df_provided = False
965+
- else if contents are unique and are contained in columns = True
966+
- else = False
967+
968+
969+
wide detection:
970+
- if no_x and no_y = wide mode
971+
- else if wide_x and wide_y = error
972+
- else if wide_x xor wide_y = wide mode
973+
- else = long mode
974+
975+
so what we want is:
976+
- y = [col col] -> melt just those
977+
- x = [col col] -> melt just those but swap the orientation? except in hist mode
978+
- y = [col col] / x=col -> melt just those and force x to not be the index ... what about hist
979+
- y = [col col] / x=[col col] -> error
980+
"""
981+
982+
df_output = pd.DataFrame()
959983

960984
missing_bar_dim = None
961985
if constructor in [go.Scatter, go.Bar] and (no_x != no_y):
@@ -1110,7 +1134,8 @@ def build_dataframe(args, constructor):
11101134
args[field_name] = str(col_name)
11111135
else:
11121136
args[field_name][i] = str(col_name)
1113-
wide_id_vars.add(str(col_name))
1137+
if field_name != "wide_cols":
1138+
wide_id_vars.add(str(col_name))
11141139

11151140
if missing_bar_dim and constructor == go.Bar:
11161141
# now that we've populated df_output, we check to see if the non-missing
@@ -1134,14 +1159,13 @@ def build_dataframe(args, constructor):
11341159
df_output[col_name] = constants[col_name]
11351160

11361161
if wide_mode:
1137-
# TODO multi-level index
1138-
# TODO multi-level columns
1139-
index_name = df_output.index.name or "index"
1140-
wide_id_vars.add(index_name)
1141-
if index_name not in df_output.columns:
1142-
df_output = df_output.reset_index()
1162+
wide_value_vars = [c for c in args["wide_cols"] if c not in wide_id_vars]
1163+
del args["wide_cols"]
11431164
df_output = df_output.melt(
1144-
id_vars=wide_id_vars, var_name=var_name, value_name="_value_"
1165+
id_vars=wide_id_vars,
1166+
value_vars=wide_value_vars,
1167+
var_name=var_name,
1168+
value_name="_value_",
11451169
)
11461170
df_output[var_name] = df_output[var_name].astype(str)
11471171
args["orientation"] = args.get("orientation", None) or "v"

0 commit comments

Comments
 (0)