Closed
Description
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"
)
Metadata
Metadata
Assignees
Labels
No labels