I recently updated an environment Iām working in. The issue is with Pandas or Plotly. I hope someone can shed some light on this.
The problem is that when filtering a grouped by data. If I use this object on a px figure, the figure breaks. Here is an example.
# Code outside px.bar
old_df2 = pd.DataFrame({'name': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'C'],
'id1': [18, 22, 19, 14, 14, 11, 20, 28],
'id2': [5, 7, 7, 9, 12, 9, 9, 4],
'id3': [11, 8, 10, 6, 6, 7, 9, 12]})
new_df = old_df2.groupby([pd.Categorical(old_df2.name),'id2'])['id3'].count().fillna(0)
# Transforms count from series to data frame
new_df = new_df.to_frame()
# rowname to index
new_df.reset_index(inplace=True)
new_df = new_df[new_df["level_0"].isin(["A","B"])]
new_df .rename(columns={'level_0': 'name'}, inplace=True)
# Not working
fig_bar = px.bar(new_df.loc[::-1], x="id2", y="id3", color = "name", barmode="group")
# Working version identical data
new_df_list = new_df.to_dict("records")
unlinked_df = pd.DataFrame(new_df_list )
fig_bar = px.bar(unlinked_df.loc[::-1], x="id2", y="id3", color = "name", barmode="group")
the traceback error for the first px.bar is the following:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/marco/python-wsl/project_folder/venv/lib/python3.8/site-packages/plotly/express/_chart_types.py", line 373, in bar
return make_figure(
File "/home/marco/python-wsl/project_folder/venv/lib/python3.8/site-packages/plotly/express/_core.py", line 2003, in make_figure
groups, orders = get_groups_and_orders(args, grouper)
File "/home/marco/python-wsl/project_folder/venv/lib/python3.8/site-packages/plotly/express/_core.py", line 1978, in get_groups_and_orders
groups = {
File "/home/marco/python-wsl/project_folder/venv/lib/python3.8/site-packages/plotly/express/_core.py", line 1979, in <dictcomp>
sf: grouped.get_group(s if len(s) > 1 else s[0])
File "/home/marco/python-wsl/project_folder/venv/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 811, in get_group
raise KeyError(name)
KeyError: 'C'