@@ -1938,21 +1938,30 @@ def get_groups_and_orders(args, grouper):
1938
1938
# we have a single group, so we can skip all group-by operations!
1939
1939
groups = {tuple (single_group_name ): df }
1940
1940
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
1942
1943
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
+ ]
1944
1947
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 ]
1953
1961
1954
1962
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 )
1956
1965
}
1957
1966
return groups , orders
1958
1967
0 commit comments