Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoGorelli What's the reason for using np.arange here rather than range? Is there a performance difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, @FBruzzesi had noted that it was generally more performant, and numpy is still required in plotly express anyway - no objections to changing this back of course!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, makes sense to me, thanks!

.alias(token)
)
.sort(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was doing a similar fix 🙈

You could also add the comparison with go.Pie:

    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, fig)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, thanks for reviewing!


def test_sunburst_treemap_colorscales():
labels = ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"]
parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"]
Expand Down