Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotly Express error with subset categorical pandas column #4274

Closed
kvanderwijst opened this issue Jul 11, 2023 · 0 comments · Fixed by #4437
Closed

Plotly Express error with subset categorical pandas column #4274

kvanderwijst opened this issue Jul 11, 2023 · 0 comments · Fixed by #4437

Comments

@kvanderwijst
Copy link

Plotly Express gives a KeyError when using a subselection of a dataframe with a categorical column. The categorical column is useful for custom ordering of values, but sometimes you only want to plot a subset of that dataframe. When not all the values of the Categorical column are present in the subselection, Plotly Express gives an error:

df = pd.DataFrame({
    "x": [0, 0, 1, 0, 1],
    "Value": [10, 5, 7, 3, 8],
    "Category": pd.Categorical(["one", "two", "one", "three", "three"], categories=["one", "two", "three"], ordered=True)
})

px.scatter(
    df[df["Category"].isin(["one", "three"])],  # Works fine with just `df` here
    x="x",
    y="Value",
    color="Category"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant