Reproducer (modified from the original):
import plotly.express as px
df = px.data.tips()
df["sex"][: len(df) // 2] = None
fig = px.scatter(df, x="total_bill", y="tip", color="smoker", facet_col="sex")
fig.write_html("test.html")
The above produces the following error:
Traceback (most recent call last):
File "plotlyscatter.py", line 5, in
fig = px.scatter(df, x="total_bill", y="tip", color="smoker", facet_col="sex")
File "/opt/envs/myenv/lib/python3.8/site-packages/plotly/express/_chart_types.py", line 66, in scatter
return make_figure(args=locals(), constructor=go.Scatter)
File "/opt/envs/myenv/lib/python3.8/site-packages/plotly/express/_core.py", line 1976, in make_figure
group = grouped.get_group(group_name if len(group_name) > 1 else group_name[0])
File "/opt/envs/myenv/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 754, in get_group
raise KeyError(name)
KeyError: ('No', '', '', '', nan)
Environment:
- plotly==5.3.1
- pandas==1.3.4
The simplest fix (which discards points with nan values) is to use grouped.indices instead of grouped.groups in this line:
|
for group_name in grouped.groups: |
However it would be useful to allow nan values in grouping using dropna=False in DataFrame.groupby:
https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.groupby.html?highlight=groupby#pandas.DataFrame.groupby
Reproducer (modified from the original):
The above produces the following error:
Environment:
The simplest fix (which discards points with nan values) is to use
grouped.indicesinstead ofgrouped.groupsin this line:plotly.py/packages/python/plotly/plotly/express/_core.py
Line 1914 in 5240301
However it would be useful to allow nan values in grouping using
dropna=FalseinDataFrame.groupby:https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.groupby.html?highlight=groupby#pandas.DataFrame.groupby