Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fix `plotly.io.write_images` ignoring `layout.width`/`layout.height` (and the template's) and exporting every figure at the default 700x500 size; it now uses the same width/height priority as `write_image` and `to_image` [[#5784](https://github.com/plotly/plotly.py/pull/5784)], with thanks to @breken-ai for the contribution!
- Fix color utilities mutating the list of colors passed to them [[#5836](https://github.com/plotly/plotly.py/pull/5836)], with thanks to @Enesuygurs for the contribution!
- Fix Plotly Express (including `px.imshow`) ignoring `facet_row_spacing=0` and `facet_col_spacing=0` and using the default spacing between facets instead [[#5881](https://github.com/plotly/plotly.py/pull/5881)], with thanks to @rishi02102017 for the contribution!
- Fix Plotly Express raising `TypeError: boolean value of NA is ambiguous` when a column used for `color`, `symbol`, `line_dash`, `line_group`, `facet_row`, `facet_col`, `animation_frame` or `pattern_shape` has a nullable dtype (`string`, `boolean`, `Int64`, `Float64`, pyarrow-backed) with missing values; rows with a missing group value are dropped, as for other dtypes [[#NNNN](https://github.com/plotly/plotly.py/pull/NNNN)], with thanks to @sohailshk for the contribution!


## [7.1.0] - 2026-09-15
Expand Down
16 changes: 12 additions & 4 deletions plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2501,10 +2501,16 @@ def get_groups_and_orders(args, grouper):
required_grouper = [group for group in orders if group in grouper]
grouped = dict(df.group_by(required_grouper, drop_null_keys=True).__iter__())

# dict lookups instead of `in`/`index`, which compare with `==` and
# raise for missing values such as pd.NA
positions = {
group: {value: i for i, value in reversed(list(enumerate(orders[group])))}
for group in required_grouper
}
sorted_group_names = sorted(
grouped.keys(),
key=lambda values: [
orders[group].index(value) if value in orders[group] else -1
positions[group].get(value, -1)
for group, value in zip(required_grouper, values)
],
)
Expand Down Expand Up @@ -2738,9 +2744,11 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
frames[frame_name]["data"].append(trace)
frame_list = [f for f in frames.values()]
if len(frame_list) > 1:
frame_list = sorted(
frame_list, key=lambda f: orders[args["animation_frame"]].index(f["name"])
)
frame_positions = {
value: i
for i, value in reversed(list(enumerate(orders[args["animation_frame"]])))
}
frame_list = sorted(frame_list, key=lambda f: frame_positions[f["name"]])

if show_colorbar:
colorvar = "z" if constructor in [go.Histogram2d, go.Densitymap] else "color"
Expand Down
81 changes: 81 additions & 0 deletions tests/test_optional/test_px/test_px.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,84 @@ def test_no_warn_on_update_template():
with warnings.catch_warnings():
warnings.simplefilter("error")
fig.update_layout(template="plotly_white")


_NULLABLE_VALUES = {
"string": ["u", None, "v", "u"],
"boolean": [True, None, False, True],
"Int64": [1, None, 3, 1],
"Float64": [1.5, None, 3.5, 1.5],
"string[pyarrow]": ["u", None, "v", "u"],
}
_NULLABLE_NAMES = {
"string": ["u", "v"],
"boolean": ["True", "False"],
"Int64": ["1", "3"],
"Float64": ["1.5", "3.5"],
"string[pyarrow]": ["u", "v"],
}


@pytest.mark.parametrize("dtype", list(_NULLABLE_VALUES))
@pytest.mark.parametrize(
"arg", ["color", "symbol", "line_dash", "facet_col", "line_group"]
)
def test_missing_values_in_nullable_grouping_column(dtype, arg):
# Missing values in nullable pandas dtypes are pd.NA, which cannot be
# compared with `==`; rows with a missing group are dropped.
df = pd.DataFrame(
{
"x": [1, 2, 3, 4],
"y": [4, 3, 2, 1],
"g": pd.array(_NULLABLE_VALUES[dtype], dtype=dtype),
}
)
fig = px.line(df, x="x", y="y", **{arg: "g"})
assert sorted(x for trace in fig.data for x in trace.x) == [1, 3, 4]
if arg in ("color", "symbol", "line_dash"):
assert [trace.name for trace in fig.data] == _NULLABLE_NAMES[dtype]


@pytest.mark.parametrize("arg", ["animation_frame", "pattern_shape", "facet_row"])
def test_missing_values_in_nullable_string_grouping_column_other_args(arg):
df = pd.DataFrame(
{
"x": [1, 2, 3, 4],
"y": [4, 3, 2, 1],
"g": pd.array(["u", None, "v", "u"], dtype="string"),
}
)
fn = px.bar if arg == "pattern_shape" else px.scatter
fig = fn(df, x="x", y="y", **{arg: "g"})
if arg == "animation_frame":
assert [f.name for f in fig.frames] == ["u", "v"]
else:
assert sorted(x for trace in fig.data for x in trace.x) == [1, 3, 4]


@pytest.mark.parametrize("arg", ["color", "facet_col"])
@pytest.mark.parametrize(
"values",
[
pd.array([None] * 3, dtype=object),
pd.array([None] * 3, dtype="string"),
pd.array([None] * 3, dtype="Int64"),
[float("nan")] * 3,
],
ids=["object", "string", "Int64", "float"],
)
def test_all_missing_grouping_column(values, arg):
# An entirely missing grouping column still gives a single trace
df = pd.DataFrame({"x": [1, 2, 3], "y": [3, 2, 1], "g": values})
fig = px.scatter(df, x="x", y="y", **{arg: "g"})
assert [list(trace.x) for trace in fig.data] == [[1, 2, 3]]


@pytest.mark.parametrize("arg", ["color", "facet_col"])
def test_all_missing_polars_grouping_column(arg):
pl = pytest.importorskip("polars")
df = pl.DataFrame(
{"x": [1, 2, 3], "y": [3, 2, 1], "g": pl.Series([None] * 3, dtype=pl.String)}
)
fig = px.scatter(df, x="x", y="y", **{arg: "g"})
assert [list(trace.x) for trace in fig.data] == [[1, 2, 3]]