[BUG]: px.sunburst / px.treemap / px.icicle with path give a different sector order on every run for Polars DataFrames
還沒有人認領這個 Issue。
評估
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- python
研究方向
從 plotly/express/_core.py 中的 process_dataframe_hierarchy 開始,追蹤用於路徑型圖表的 group_by 呼叫。比較 Polars、pandas 和 PyArrow 的階層順序,然後檢查回報者附上的或提出的迴歸測試。當重複執行 Polars 能產生穩定的首次出現順序,並且與其他受支援的輸入一致時,即視為完成。
由索引模型根據 Issue 內容生成。
描述
Description
When path= is used with a Polars DataFrame, px.sunburst, px.treemap and px.icicle build their ids / labels / parents / values arrays in a different order every time the script is run. The same data as a pandas DataFrame or a PyArrow table always gives the same order: the order in which the sectors first appear in the data.
The cause is in process_dataframe_hierarchy (plotly/express/_core.py). Each level of the hierarchy is built with df.group_by(path[i:]).agg(...). With pandas (narwhals uses sort=False) and PyArrow the groups come back in order of first appearance, but Polars' group_by does not guarantee any order, so the order of the output changes from run to run.
Consequences:
fig.to_json()/fig.write_html()output is not reproducible with Polars input (snapshot tests, caching, diffs of generated HTML).- With
sort=False, or when sectors have equal values, the chart itself is laid out differently on each run. - Polars results differ from pandas / PyArrow results for identical data.
Screenshots/Video
N/A: the difference is in the figure data; see the output below.
Steps to reproduce
import plotly
import plotly.express as px
import polars as pl
df = pl.DataFrame(
{
"region": ["South", "North", "South", "West", "North", "West"],
"sector": ["Tech", "Finance", "Finance", "Tech", "Tech", "Finance"],
"sales": [1, 2, 3, 4, 5, 6],
}
)
fig = px.sunburst(df, path=["region", "sector"], values="sales")
print(plotly.__version__, pl.__version__, list(fig.data[0].ids))
Running the script three times (plotly 7.1.0, polars 1.44.2):
7.1.0 1.44.2 ['West/Tech', 'West/Finance', 'North/Finance', 'South/Finance', 'North/Tech', 'South/Tech', 'South', 'North', 'West']
7.1.0 1.44.2 ['South/Tech', 'West/Tech', 'North/Tech', 'South/Finance', 'North/Finance', 'West/Finance', 'West', 'North', 'South']
7.1.0 1.44.2 ['South/Tech', 'West/Tech', 'North/Tech', 'North/Finance', 'South/Finance', 'West/Finance', 'West', 'South', 'North']
With pd.DataFrame(...) instead, every run prints:
['South/Tech', 'North/Finance', 'South/Finance', 'West/Tech', 'North/Tech', 'West/Finance', 'South', 'North', 'West']
Notes
I have a small fix with a regression test and will open a PR for it.
- 主要語言
- Python
- 星號
- 18.8k
- 分支
- 2.8k
- 平均合併
- 16 小時 26 分鐘
- 30 天內合併 PR
- 21
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
plotly/plotly.py 的其他 Issue
-
P3 size: 1 task
難度 2/5 1-3 小時 新手友好度 72/100
-
bug P1
難度 1/5 1 小時以內 新手友好度 68/100
-
feature P3
難度 2/5 1-3 小時 新手友好度 62/100
-
feature P3
難度 2/5 1-3 小時 新手友好度 72/100
-
bug P1 size: 1
難度 4/5 3-5 天 新手友好度 35/100
相似的 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 86/100
zostera/django-bootstrap4#894 ·
-
難度 2/5 1-3 小時 新手友好度 78/100
use-agent-os/agent-os#3276 ·
-
難度 2/5 1-3 小時 新手友好度 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
難度 2/5 1-3 小時 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
難度 2/5 1-3 小時 新手友好度 82/100
zilliztech/memsearch#759 ·