@@ -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,31 @@ 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 = [
2447
+ key for key in orders if key in grouper and key != one_group
2448
+ ]
2449
+ order_mapping = {key : orders [key ] for key in required_grouper }
2450
2450
grouped = dict (df .group_by (required_grouper , drop_null_keys = True ).__iter__ ())
2451
- sorted_group_names = list (grouped .keys ())
2452
2451
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
- )
2452
+ sorted_group_names = sorted (
2453
+ grouped .keys (),
2454
+ key = lambda group : [
2455
+ order_mapping [key ].index (value ) if value in order_mapping [key ] else - 1
2456
+ for key , value in zip (required_grouper , group )
2457
+ ],
2458
+ )
2458
2459
2459
2460
# calculate the full group_names by inserting "" in the tuple index for one_group groups
2460
2461
full_sorted_group_names = [
2461
2462
tuple (
2462
2463
[
2463
- "" if col == one_group else sub_group_names [grp_to_idx [col ]]
2464
+ ""
2465
+ if col == one_group
2466
+ else sub_group_names [required_grouper .index (col )]
2464
2467
for col in grouper
2465
2468
]
2466
2469
)
0 commit comments