Skip to content

Category Orders broke in v6 #5033

Closed
Closed
@DrMagPie

Description

@DrMagPie

Hi,

category_orders option broke in v6 release, using polars and plotly express in python 3.13.

Following code works in v5.24.1

import polars as pl
import plotly.express as px


data = [
	pl.Series(
		'level',
		['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e'],
		dtype=pl.String,
	),
	pl.Series('name', ['v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9', 'v10', 'v11', 'v12', 'v13', 'v14', 'v16'], dtype=pl.String),
]

df = pl.DataFrame(data)
pie = px.pie(
	df['level'].value_counts(),
	names='level',
	values='count',
	color='level',
	color_discrete_map={
		'e': 'rgb(255, 43, 43)',
		'd': 'rgb(255, 171, 171)',
		'c': 'rgb(0, 104, 201)',
		'b': 'rgb(131, 201, 255)',
		'a': 'rgb(125, 239, 161)',
	},
	category_orders={'level': ['a', 'b', 'c', 'd', 'e']},
)

and produces this plot

Image

But the same code in v6.0.0 produces following error

Image

Full trace:

Traceback (most recent call last):
  File "path/to/project/app/main.py", line 438, in draw_pie
    px.pie(
    ~~~~~~^
        df['level'].value_counts(),
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<10 lines>...
        category_orders={'level': ['a', 'b', 'c', 'd', 'e']},
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ),
    ^
  File "path/to/project/.venv/lib/python3.13/site-packages/plotly/express/_chart_types.py", line 1701, in pie
    return make_figure(
        args=locals(),
    ...<2 lines>...
        layout_patch=layout_patch,
    )
  File "path/to/project/.venv/lib/python3.13/site-packages/plotly/express/_core.py", line 2481, in make_figure
    args, trace_patch = process_dataframe_pie(args, trace_patch)
                        ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "path/to/project/.venv/lib/python3.13/site-packages/plotly/express/_core.py", line 2165, in process_dataframe_pie
    df.with_columns(
    ~~~~~~~~~~~~~~~^
        nw.col("b")
        ^^^^^^^^^^^
        .replace_strict(order, range(len(order)), return_dtype=nw.UInt32)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        .alias(token)
        ^^^^^^^^^^^^^
    )
    ^
  File "path/to/project/.venv/lib/python3.13/site-packages/narwhals/dataframe.py", line 1302, in with_columns
    return super().with_columns(*exprs, **named_exprs)
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "path/to/project/.venv/lib/python3.13/site-packages/narwhals/dataframe.py", line 139, in with_columns
    self._compliant_frame.with_columns(*compliant_exprs, **compliant_named_exprs),
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "path/to/project/.venv/lib/python3.13/site-packages/narwhals/_polars/dataframe.py", line 124, in func
    raise ColumnNotFoundError(msg) from e
narwhals.exceptions.ColumnNotFoundError: b

Resolved plan until failure:

        ---> FAILED HERE RESOLVING 'with_columns' <---
DF ["level", "count"]; PROJECT */2 COLUMNS

Hint: Did you mean one of these columns: ['level', 'count']?

Could you please look into it.
Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions