@@ -1052,6 +1052,15 @@ def process_args_into_dataframe(args, wide_mode, var_name, value_name):
1052
1052
else :
1053
1053
df_output [df_input .columns ] = df_input [df_input .columns ]
1054
1054
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
+
1055
1064
# hover_data is a dict
1056
1065
hover_data_is_dict = (
1057
1066
"hover_data" in args
@@ -1095,6 +1104,7 @@ def process_args_into_dataframe(args, wide_mode, var_name, value_name):
1095
1104
if argument is None :
1096
1105
continue
1097
1106
col_name = None
1107
+
1098
1108
# Case of multiindex
1099
1109
if isinstance (argument , pd .MultiIndex ):
1100
1110
raise TypeError (
@@ -1114,7 +1124,19 @@ def process_args_into_dataframe(args, wide_mode, var_name, value_name):
1114
1124
ranges .append (col_name )
1115
1125
# ----------------- argument is likely a col name ----------------------
1116
1126
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 (
1118
1140
field_name == "hover_data"
1119
1141
and hover_data_is_dict
1120
1142
and args ["hover_data" ][str (argument )][1 ] is not None
@@ -1484,12 +1506,15 @@ def process_dataframe_hierarchy(args):
1484
1506
_check_dataframe_all_leaves (df [path [::- 1 ]])
1485
1507
discrete_color = False
1486
1508
1509
+ reserved_names = ["labels" , "parent" , "id" ]
1510
+
1487
1511
new_path = []
1488
1512
for col_name in path :
1489
1513
new_col_name = col_name + "_path_copy"
1490
1514
new_path .append (new_col_name )
1491
1515
df [new_col_name ] = df [col_name ]
1492
1516
path = new_path
1517
+
1493
1518
# ------------ Define aggregation functions --------------------------------
1494
1519
1495
1520
def aggfunc_discrete (x ):
@@ -1547,9 +1572,14 @@ def aggfunc_continuous(x):
1547
1572
if col not in agg_f :
1548
1573
agg_f [col ] = aggfunc_discrete
1549
1574
# 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
+
1551
1581
# ----------------------------------------------------------------------------
1552
- df_all_trees = pd .DataFrame (columns = [ "labels" , "parent" , "id" ] + cols )
1582
+ df_all_trees = pd .DataFrame (columns = reserved_names + cols )
1553
1583
# Set column type here (useful for continuous vs discrete colorscale)
1554
1584
for col in cols :
1555
1585
df_all_trees [col ] = df_all_trees [col ].astype (df [col ].dtype )
0 commit comments