@@ -2422,7 +2422,6 @@ def get_groups_and_orders(args, grouper):
2422
2422
# figure out orders and what the single group name would be if there were one
2423
2423
single_group_name = []
2424
2424
unique_cache = dict ()
2425
- grp_to_idx = dict ()
2426
2425
2427
2426
for i , col in enumerate (grouper ):
2428
2427
if col == one_group :
@@ -2440,27 +2439,28 @@ def get_groups_and_orders(args, grouper):
2440
2439
else :
2441
2440
orders [col ] = list (OrderedDict .fromkeys (list (orders [col ]) + uniques ))
2442
2441
2443
- grp_to_idx = {k : i for i , k in enumerate (orders )}
2444
-
2445
2442
if len (single_group_name ) == len (grouper ):
2446
2443
# we have a single group, so we can skip all group-by operations!
2447
2444
groups = {tuple (single_group_name ): df }
2448
2445
else :
2449
- required_grouper = list ( orders . keys ())
2446
+ required_grouper = [ group for group in orders if group in grouper ]
2450
2447
grouped = dict (df .group_by (required_grouper , drop_null_keys = True ).__iter__ ())
2451
- sorted_group_names = list (grouped .keys ())
2452
2448
2453
- for i , col in reversed (list (enumerate (required_grouper ))):
2454
- sorted_group_names = sorted (
2455
- sorted_group_names ,
2456
- key = lambda g : orders [col ].index (g [i ]) if g [i ] in orders [col ] else - 1 ,
2457
- )
2449
+ sorted_group_names = sorted (
2450
+ grouped .keys (),
2451
+ key = lambda values : [
2452
+ orders [group ].index (value ) if value in orders [group ] else - 1
2453
+ for group , value in zip (required_grouper , values )
2454
+ ],
2455
+ )
2458
2456
2459
2457
# calculate the full group_names by inserting "" in the tuple index for one_group groups
2460
2458
full_sorted_group_names = [
2461
2459
tuple (
2462
2460
[
2463
- "" if col == one_group else sub_group_names [grp_to_idx [col ]]
2461
+ ""
2462
+ if col == one_group
2463
+ else sub_group_names [required_grouper .index (col )]
2464
2464
for col in grouper
2465
2465
]
2466
2466
)
0 commit comments