Skip to content

♻️ check for all same groups #3767

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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@ def infer_config(args, constructor, trace_patch, layout_patch):
return trace_specs, grouped_mappings, sizeref, show_colorbar


def get_orderings(args, grouper, grouped):
def get_orderings(args, grouper, grouped, all_same_group):
"""
`orders` is the user-supplied ordering with the remaining data-frame-supplied
ordering appended if the column is used for grouping. It includes anything the user
Expand All @@ -1916,10 +1916,17 @@ def get_orderings(args, grouper, grouped):
of a single dimension-group
"""
orders = {} if "category_orders" not in args else args["category_orders"].copy()
sorted_group_names = []

if _all_one_group(grouper):
sorted_group_names = [("",) * len(grouper)]
return orders, sorted_group_names
if all_same_group:
for col in grouper:
if col != one_group:
single_val = args["data_frame"][col].iloc[0]
sorted_group_names.append(single_val)
orders[col] = [single_val]
else:
sorted_group_names.append("")
return orders, [tuple(sorted_group_names)]

for col in grouper:
if col != one_group:
Expand All @@ -1929,7 +1936,6 @@ def get_orderings(args, grouper, grouped):
else:
orders[col] = list(OrderedDict.fromkeys(list(orders[col]) + uniques))

sorted_group_names = []
for group_name in grouped.groups:
if len(grouper) == 1:
group_name = (group_name,)
Expand All @@ -1944,10 +1950,12 @@ def get_orderings(args, grouper, grouped):
return orders, sorted_group_names


def _all_one_group(grouper):
for g in grouper:
def _all_same_group(args, grouper):
for g in set(grouper):
if g != one_group:
return False
arr = args["data_frame"][g].values
if not (arr[0] == arr).all(axis=0):
return False
return True


Expand All @@ -1968,10 +1976,11 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
)
grouper = [x.grouper or one_group for x in grouped_mappings] or [one_group]
grouped = None
if not _all_one_group(grouper):
all_same_group = _all_same_group(args, grouper)
if not all_same_group:
grouped = args["data_frame"].groupby(grouper, sort=False)

orders, sorted_group_names = get_orderings(args, grouper, grouped)
orders, sorted_group_names = get_orderings(args, grouper, grouped, all_same_group)

col_labels = []
row_labels = []
Expand Down