Description
process_dataframe_hierarchy (plotly/express/_core.py), which builds the data for px.sunburst, px.treemap and px.icicle when path is used, adds a temporary row index column, aggregates its minimum per group and sorts each level by it. This is there only to get a deterministic sector order out of group_by, whose row order is not guaranteed for every backend (#5765, #5766).
narwhals is planning to expose maintain_order on group_by (narwhals-dev/narwhals#3309). Once that lands and the minimum supported narwhals version in pyproject.toml includes it, the temporary column, its aggregation and the per-level sort can be dropped in favour of df.group_by(path[i:], drop_null_keys=True, maintain_order=True).
The existing test_sunburst_treemap_with_path_order test covers the behaviour, so it should keep passing after the switch.
Filed as a follow up to #5766, as suggested by @camdecoster in #5765.
Description
process_dataframe_hierarchy(plotly/express/_core.py), which builds the data forpx.sunburst,px.treemapandpx.iciclewhenpathis used, adds a temporary row index column, aggregates its minimum per group and sorts each level by it. This is there only to get a deterministic sector order out ofgroup_by, whose row order is not guaranteed for every backend (#5765, #5766).narwhals is planning to expose
maintain_orderongroup_by(narwhals-dev/narwhals#3309). Once that lands and the minimum supported narwhals version inpyproject.tomlincludes it, the temporary column, its aggregation and the per-level sort can be dropped in favour ofdf.group_by(path[i:], drop_null_keys=True, maintain_order=True).The existing
test_sunburst_treemap_with_path_ordertest covers the behaviour, so it should keep passing after the switch.Filed as a follow up to #5766, as suggested by @camdecoster in #5765.