Skip to content

Fix px crash on missing values in nullable grouping columns - #5896

Open
sohailshk wants to merge 1 commit into
plotly:mainfrom
sohailshk:fix/px-nullable-group-columns
Open

sohailshk wants to merge 1 commit into
plotly:mainfrom
sohailshk:fix/px-nullable-group-columns

Conversation

@sohailshk

Copy link
Copy Markdown

Link to issue

Closes #5895

Description of change

Plotly Express raised TypeError: boolean value of NA is ambiguous when a column with a nullable pandas dtype (string, boolean, Int64, Float64, pyarrow-backed) had missing values and was used for color, symbol, line_dash, line_group, facet_row, facet_col, animation_frame or pattern_shape. The missing value is pd.NA, and the group sort and the animation frame sort compared it with == through list.index and in. They now look the position up in a dict.

Demo

import pandas as pd, plotly.express as px
df = pd.DataFrame({"x": [1, 2, 3, 4], "y": [4, 3, 2, 1],
                   "g": pd.array(["u", None, "v", "u"], dtype="string")})
px.scatter(df, x="x", y="y", color="g")  # TypeError before, two traces now

After the change the example returns two traces (u: x=[1, 4], v: x=[3]). The row with the missing group is dropped. Figures for object, float and polars columns with missing values are the same as before (colors, facets, axis order).

The implementation and tests were written with AI assistance.

Testing strategy

New tests cover five nullable dtypes with five grouping arguments, animation_frame, pattern_shape and facet_row, and columns where every value is missing (pandas and polars). The nullable cases fail on main and pass with the change. tests/test_optional/test_px passes.

Additional information (optional)

The CHANGELOG entry uses #NNNN for the PR number.

Guidelines

Missing values in nullable pandas dtypes are pd.NA, and the group sort and the animation frame sort compared them with == via list.index and in, which raised. Look the positions up in a dict instead. Output for object, float and polars columns is unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: px raises "boolean value of NA is ambiguous" for nullable-dtype grouping columns with missing values

1 participant