Skip to content

Commit b3a4583

Browse files
committed
🧹 avoid unnecessary one_group groupby operations
1 parent cdae77c commit b3a4583

File tree

1 file changed

+20
-11
lines changed
  • packages/python/plotly/plotly/express

1 file changed

+20
-11
lines changed

Diff for: packages/python/plotly/plotly/express/_core.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -1938,21 +1938,30 @@ def get_groups_and_orders(args, grouper):
19381938
# we have a single group, so we can skip all group-by operations!
19391939
groups = {tuple(single_group_name): df}
19401940
else:
1941-
grouped = df.groupby(grouper, sort=False)
1941+
required_grouper = [g for g in grouper if g != one_group]
1942+
grouped = df.groupby(required_grouper, sort=False) # skip one_group groupers
19421943
group_indices = grouped.indices
1943-
sorted_group_names = [g if len(grouper) != 1 else (g,) for g in group_indices]
1944+
sorted_group_names = [
1945+
g if len(required_grouper) != 1 else (g,) for g in group_indices
1946+
]
19441947

1945-
for i, col in reversed(list(enumerate(grouper))):
1946-
if col != one_group:
1947-
sorted_group_names = sorted(
1948-
sorted_group_names,
1949-
key=lambda g: orders[col].index(g[i])
1950-
if g[i] in orders[col]
1951-
else -1,
1952-
)
1948+
for i, col in reversed(list(enumerate(required_grouper))):
1949+
sorted_group_names = sorted(
1950+
sorted_group_names,
1951+
key=lambda g: orders[col].index(g[i]) if g[i] in orders[col] else -1,
1952+
)
1953+
1954+
# calculate the full group_names by inserting "" in the tuple index for one_group groups
1955+
full_sorted_group_names = [list(t) for t in sorted_group_names]
1956+
for i, col in enumerate(grouper):
1957+
if col == one_group:
1958+
for g in full_sorted_group_names:
1959+
g.insert(i, "")
1960+
full_sorted_group_names = [tuple(g) for g in full_sorted_group_names]
19531961

19541962
groups = {
1955-
s: grouped.get_group(s if len(s) > 1 else s[0]) for s in sorted_group_names
1963+
sf: grouped.get_group(s if len(s) > 1 else s[0])
1964+
for sf, s in zip(full_sorted_group_names, sorted_group_names)
19561965
}
19571966
return groups, orders
19581967

0 commit comments

Comments
 (0)