Skip to content

added: ignore facet args with empty dataset #4038

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

Merged
merged 8 commits into from
Jun 4, 2023
4 changes: 4 additions & 0 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,10 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
layout_patch = layout_patch or {}
apply_default_cascade(args)

# Ignore facet rows and columns when data frame is empty so as to prevent nrows/ncols equaling 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a reasonable fix, but probably belongs near the bottom of the infer_config function, just so it's in roughly the same place as most of the other similar mutations of args.

While we're on this topic of empty data frames though, are there any other args that fail when the df length is 0? would be good to understand why these specifically fail and if it impacts other categorical-type attributes like symbol or somethinng. Maybe there could be a more generic fix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: thanks for the PR! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that makes sense. And you're very welcome. :)

I've moved it into infer_config for now, but I'll look into whether there could be a more generic solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like facet_col and facet_row both fail with an empty dataset because they are used in the computation of ncols and nrows:

if m.facet == "col":
prefix = get_label(args, args["facet_col"]) + "="
col_labels = [prefix + str(s) for s in sorted_values]
ncols = len(col_labels)
if m.facet == "row":
prefix = get_label(args, args["facet_row"]) + "="
row_labels = [prefix + str(s) for s in sorted_values]
nrows = len(row_labels)

Neither ncols nor nrows can equal 0, hence the failure. As far as I can tell, nothing else in args effects either of the two in any other way.

To me, this seems like a fairly specific failure requiring a specific fix, but I'm still far from familiar with the codebase, so I could easily be wrong. :)

if len(args["data_frame"]) == 0:
args["facet_row"] = args["facet_col"] = None

args = build_dataframe(args, constructor)
if constructor in [go.Treemap, go.Sunburst, go.Icicle] and args["path"] is not None:
args = process_dataframe_hierarchy(args)
Expand Down