Skip to content

Commit 4fd8736

Browse files
authored
Sunburst parent copy (#2640)
* wip: remove part of the bug * added test * handle more cases * black * add tests * debug * added tests and solved name conflict * fine-tuning * Revert "added test" This reverts commit 79ac151. * empty commit * debug
1 parent 2e99103 commit 4fd8736

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,8 +1550,9 @@ def aggfunc_continuous(x):
15501550
for col in cols: # for hover_data, custom_data etc.
15511551
if col not in agg_f:
15521552
agg_f[col] = aggfunc_discrete
1553+
# Avoid collisions with reserved names - columns in the path have been copied already
1554+
cols = list(set(cols) - set(["labels", "parent", "id"]))
15531555
# ----------------------------------------------------------------------------
1554-
15551556
df_all_trees = pd.DataFrame(columns=["labels", "parent", "id"] + cols)
15561557
# Set column type here (useful for continuous vs discrete colorscale)
15571558
for col in cols:

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,26 @@ def test_sunburst_treemap_with_path_color():
250250
assert np.all(np.array(colors[:8]) == np.array(calls))
251251

252252

253+
def test_sunburst_treemap_column_parent():
254+
vendors = ["A", "B", "C", "D", "E", "F", "G", "H"]
255+
sectors = [
256+
"Tech",
257+
"Tech",
258+
"Finance",
259+
"Finance",
260+
"Tech",
261+
"Tech",
262+
"Finance",
263+
"Finance",
264+
]
265+
regions = ["North", "North", "North", "North", "South", "South", "South", "South"]
266+
values = [1, 3, 2, 4, 2, 2, 1, 4]
267+
df = pd.DataFrame(dict(id=vendors, sectors=sectors, parent=regions, values=values,))
268+
path = ["parent", "sectors", "id"]
269+
# One column of the path is a reserved name - this is ok and should not raise
270+
fig = px.sunburst(df, path=path, values="values")
271+
272+
253273
def test_sunburst_treemap_with_path_non_rectangular():
254274
vendors = ["A", "B", "C", "D", None, "E", "F", "G", "H", None]
255275
sectors = [

0 commit comments

Comments
 (0)