Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e93aa7

Browse files
committed
handle more cases
1 parent 79ac151 commit 4e93aa7

File tree

1 file changed

+33
-3
lines changed
  • packages/python/plotly/plotly/express

1 file changed

+33
-3
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,15 @@ def process_args_into_dataframe(args, wide_mode, var_name, value_name):
10521052
else:
10531053
df_output[df_input.columns] = df_input[df_input.columns]
10541054

1055+
# Case of sunburst and treemap, used with the `path` argument
1056+
# in which case the dataframe is massaged into a new format
1057+
# and column with reserved names will be created
1058+
uses_path = False
1059+
reserved_names = []
1060+
if "path" in args and args["path"] is not None:
1061+
uses_path = True
1062+
reserved_names = ["id", "labels", "parent"]
1063+
10551064
# hover_data is a dict
10561065
hover_data_is_dict = (
10571066
"hover_data" in args
@@ -1095,6 +1104,7 @@ def process_args_into_dataframe(args, wide_mode, var_name, value_name):
10951104
if argument is None:
10961105
continue
10971106
col_name = None
1107+
10981108
# Case of multiindex
10991109
if isinstance(argument, pd.MultiIndex):
11001110
raise TypeError(
@@ -1114,7 +1124,19 @@ def process_args_into_dataframe(args, wide_mode, var_name, value_name):
11141124
ranges.append(col_name)
11151125
# ----------------- argument is likely a col name ----------------------
11161126
elif isinstance(argument, str) or not hasattr(argument, "__len__"):
1117-
if (
1127+
if uses_path and argument in reserved_names and field_name != 'path':
1128+
if (args["labels"] is None or argument not in args["labels"]):
1129+
raise ValueError(
1130+
"%s is a reserved name for px.sunburst and px.treemap. "
1131+
"Please use the labels argument to provide another name "
1132+
"for the column, for example "
1133+
"labels={'%s': '%s_col'}"
1134+
% (argument, argument, argument)
1135+
)
1136+
else:
1137+
col_name = args["labels"][argument]
1138+
df_output[col_name] = df_input[argument]
1139+
elif (
11181140
field_name == "hover_data"
11191141
and hover_data_is_dict
11201142
and args["hover_data"][str(argument)][1] is not None
@@ -1484,12 +1506,15 @@ def process_dataframe_hierarchy(args):
14841506
_check_dataframe_all_leaves(df[path[::-1]])
14851507
discrete_color = False
14861508

1509+
reserved_names = ["labels", "parent", "id"]
1510+
14871511
new_path = []
14881512
for col_name in path:
14891513
new_col_name = col_name + "_path_copy"
14901514
new_path.append(new_col_name)
14911515
df[new_col_name] = df[col_name]
14921516
path = new_path
1517+
14931518
# ------------ Define aggregation functions --------------------------------
14941519

14951520
def aggfunc_discrete(x):
@@ -1547,9 +1572,14 @@ def aggfunc_continuous(x):
15471572
if col not in agg_f:
15481573
agg_f[col] = aggfunc_discrete
15491574
# Avoid collisions with reserved names - columns in the path have been copied already
1550-
cols = list(set(cols) - set(["labels", "parent", "id"]))
1575+
#for col in cols:
1576+
# if col in reserved_names and args["labels"] is not None and col in args["labels"]:
1577+
# df[args["labels"][col]] = df[col]
1578+
cols = list(set(cols) - set(reserved_names))
1579+
1580+
15511581
# ----------------------------------------------------------------------------
1552-
df_all_trees = pd.DataFrame(columns=["labels", "parent", "id"] + cols)
1582+
df_all_trees = pd.DataFrame(columns=reserved_names + cols)
15531583
# Set column type here (useful for continuous vs discrete colorscale)
15541584
for col in cols:
15551585
df_all_trees[col] = df_all_trees[col].astype(df[col].dtype)

0 commit comments

Comments
 (0)