Conversation
process_dataframe_hierarchy groups each level of the path with group_by, whose output order is not guaranteed: pandas and PyArrow return groups in order of first appearance, but Polars returns them in a different order on every run. Track the original row index, aggregate its minimum per group and sort each level by it, so that sunburst, treemap and icicle sectors follow their order of first appearance for all dataframe backends. Closes plotly#5765
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to issue
Closes #5765
Description of change
process_dataframe_hierarchynow adds a temporary row-index column, takes its minimum in each group, and sorts every level of the hierarchy by it after thegroup_by. Sectors are therefore always listed in order of first appearance for every dataframe backend. pandas and PyArrow already produced that order; Polars, whosegroup_bydoes not keep row order, produced a different order on every run.Demo
Before, three runs of the script from the issue with a Polars DataFrame:
After, every run (and pandas / PyArrow):
Testing strategy
Added
test_sunburst_treemap_with_path_order, parametrized over all the dataframe constructors and overpx.sunburst,px.treemapandpx.icicle. It checks the exact order ofidsandvalues. It fails for the Polars constructor onmainand passes with this change. The rest oftests/test_optional/test_pxpasses locally, apart from 3 tests that fail the same way on unmodifiedmainin my Windows environment (test_timezonesandtest_date_in_hoverfor the PyArrow constructor, and onetest_imshow_source_dtype_zmaxcase). The hierarchy tests also pass with the minimum supported narwhals (1.15.1).Additional information (optional)
group_byusessort=False). This matches how Plotly Express orders categories elsewhere (unique(maintain_order=True)).group_byin_core.pyis only used for key lookups, and trace order there already comes fromunique(maintain_order=True).Guidelines