From 3abd6d4e67735d31103add02b6bd875897fefcd6 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Thu, 30 Jan 2025 08:48:01 +0000 Subject: [PATCH 1/2] fix: custom category order was hard-coded --- .../python/plotly/plotly/express/_core.py | 8 ++++--- .../test_px/test_px_functions.py | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 4dca8f4215..5f0eb53f95 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -2147,6 +2147,8 @@ def process_dataframe_timeline(args): def process_dataframe_pie(args, trace_patch): + import numpy as np + names = args.get("names") if names is None: return args, trace_patch @@ -2159,12 +2161,12 @@ def process_dataframe_pie(args, trace_patch): uniques = df.get_column(names).unique(maintain_order=True).to_list() order = [x for x in OrderedDict.fromkeys(list(order_in) + uniques) if x in uniques] - # Sort args['data_frame'] by column 'b' according to order `order`. + # Sort args['data_frame'] by column `names` according to order `order`. token = nw.generate_temporary_column_name(8, df.columns) args["data_frame"] = ( df.with_columns( - nw.col("b") - .replace_strict(order, range(len(order)), return_dtype=nw.UInt32) + nw.col(names) + .replace_strict(order, np.arange(len(order)), return_dtype=nw.UInt32) .alias(token) ) .sort(token) diff --git a/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_functions.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_functions.py index 84822a32e1..050c6cce84 100644 --- a/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_functions.py +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_functions.py @@ -56,6 +56,29 @@ def test_pie_like_px(): _compare_figures(trace, fig) +def test_pie_custom_category_order(constructor): + # https://github.com/plotly/plotly.py/issues/4999 + df = constructor( + { + "status": ["On Route", "Pending", "Waiting Result", "Delivered"], + "count": [28, 10, 73, 8], + } + ) + custom_order = ["Pending", "Waiting Result", "On Route", "Delivered"] + result = px.pie( + data_frame=df, + values="count", + names="status", + category_orders={"status": custom_order}, + ).to_dict() + assert list(result["data"][0]["labels"]) == [ + "Pending", + "Waiting Result", + "On Route", + "Delivered", + ] + + def test_sunburst_treemap_colorscales(): labels = ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"] parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"] From 9043fff3a286e36d8599029b94335fa830821801 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Thu, 30 Jan 2025 10:45:41 +0000 Subject: [PATCH 2/2] add go.Pie comparison --- .../test_px/test_px_functions.py | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_functions.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_functions.py index 050c6cce84..7cab028b21 100644 --- a/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_functions.py +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_functions.py @@ -58,25 +58,35 @@ def test_pie_like_px(): def test_pie_custom_category_order(constructor): # https://github.com/plotly/plotly.py/issues/4999 - df = constructor( - { - "status": ["On Route", "Pending", "Waiting Result", "Delivered"], - "count": [28, 10, 73, 8], - } - ) + data = { + "status": ["On Route", "Pending", "Waiting Result", "Delivered"], + "count": [28, 10, 73, 8], + } + df = constructor(data) custom_order = ["Pending", "Waiting Result", "On Route", "Delivered"] result = px.pie( data_frame=df, values="count", names="status", category_orders={"status": custom_order}, - ).to_dict() - assert list(result["data"][0]["labels"]) == [ + ) + assert list(result.to_dict()["data"][0]["labels"]) == [ "Pending", "Waiting Result", "On Route", "Delivered", ] + values_ = np.array( + [ + x[0] + for x in sorted( + zip(data["count"], data["status"]), + key=lambda t: custom_order.index(t[1]), + ) + ] + ) + trace = go.Pie(values=values_, labels=custom_order) + _compare_figures(trace, result) def test_sunburst_treemap_colorscales():