#15475·bokeh

legend_group raises TypeError when the grouped column contains None (or NaN) alongside strings

Author: MohammadHijjawi97Created Sep 28, 2026Updated Sep 28, 2026

Software versions

Bokeh branch-4.0 (also 3.10.0), Python 3.13, numpy 2.x

Expected behavior

Using legend_group on a column that contains missing values should create a legend entry per group (the missing value becoming its own entry), like any other value.

Observed behavior

_handle_legend_group uses np.unique(column, return_index=1), which sorts the values. When a column mixes strings with None (or NaN, which is what a pandas object column with missing values becomes), the values cannot be ordered and the glyph call fails:

TypeError: '<' not supported between instances of 'str' and 'NoneType'

Example code

python
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure

source = ColumnDataSource(data=dict(x=[1, 2, 3], y=[1, 2, 3], c=["a", None, "b"]))
p = figure()
p.scatter("x", "y", source=source, legend_group="c")   # TypeError

The same happens with source=pd.DataFrame(dict(x=[1, 2, 3], y=[1, 2, 3], c=["a", None, "b"])).

Suggested fix

Keep np.unique (sorted groups) when the values are orderable, and fall back to grouping in order of first appearance when sorting raises TypeError. I have a small patch with a regression test ready if this approach is acceptable.

(I used an AI coding assistant while investigating this.)