Skip to content

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

Open

Description

@sohailshk

Description

Plotly Express raises TypeError: boolean value of NA is ambiguous when a column with a nullable pandas dtype contains a missing value and is used as a grouping argument. This happens for color, symbol, line_dash, line_group, facet_row, facet_col, animation_frame and pattern_shape, and for the dtypes string, boolean, Int64, Float64, string[pyarrow] and pd.ArrowDtype(pa.string()). Columns of dtype object, float, category and the pandas 3 default str work. A common way to get these dtypes is df.convert_dtypes().

Screenshots/Video

None.

Steps to reproduce

Tested with plotly 7.1.0 and pandas 2.3.3 and 3.1.1.

import pandas as pd
import 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")

Actual: TypeError: boolean value of NA is ambiguous.

Expected: two traces, u (x=[1, 4]) and v (x=[3]). The row with the missing value is dropped, as it is when g has dtype object.

Notes

The cause is in plotly/express/_core.py. The sort of the groups and of the animation frames uses list.index and in on the unique values, which compare pd.NA with ==.

A fix is ready in a PR.

Activity

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

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