56
56
needs_i8_conversion ,
57
57
)
58
58
from pandas .core .dtypes .dtypes import ExtensionDtype
59
+ from pandas .core .dtypes .generic import ABCCategoricalIndex
59
60
from pandas .core .dtypes .missing import (
60
61
isna ,
61
62
maybe_fill ,
88
89
grouper ,
89
90
)
90
91
from pandas .core .indexes .api import (
91
- CategoricalIndex ,
92
92
Index ,
93
93
MultiIndex ,
94
94
ensure_index ,
@@ -676,6 +676,7 @@ def __init__(
676
676
):
677
677
assert isinstance (axis , Index ), axis
678
678
679
+ self ._filter_empty_groups = self .compressed = len (groupings ) != 1
679
680
self .axis = axis
680
681
self ._groupings : list [grouper .Grouping ] = list (groupings )
681
682
self .sort = sort
@@ -821,7 +822,9 @@ def apply(self, f: F, data: FrameOrSeries, axis: int = 0):
821
822
@cache_readonly
822
823
def indices (self ):
823
824
""" dict {group name -> group indices} """
824
- if len (self .groupings ) == 1 and isinstance (self .result_index , CategoricalIndex ):
825
+ if len (self .groupings ) == 1 and isinstance (
826
+ self .result_index , ABCCategoricalIndex
827
+ ):
825
828
# This shows unused categories in indices GH#38642
826
829
return self .groupings [0 ].indices
827
830
codes_list = [ping .codes for ping in self .groupings ]
@@ -910,7 +913,7 @@ def reconstructed_codes(self) -> list[np.ndarray]:
910
913
911
914
@cache_readonly
912
915
def result_index (self ) -> Index :
913
- if len (self .groupings ) == 1 :
916
+ if not self . compressed and len (self .groupings ) == 1 :
914
917
return self .groupings [0 ].result_index .rename (self .names [0 ])
915
918
916
919
codes = self .reconstructed_codes
@@ -921,9 +924,7 @@ def result_index(self) -> Index:
921
924
922
925
@final
923
926
def get_group_levels (self ) -> list [Index ]:
924
- # Note: only called from _insert_inaxis_grouper_inplace, which
925
- # is only called for BaseGrouper, never for BinGrouper
926
- if len (self .groupings ) == 1 :
927
+ if not self .compressed and len (self .groupings ) == 1 :
927
928
return [self .groupings [0 ].result_index ]
928
929
929
930
name_list = []
@@ -1090,6 +1091,7 @@ def __init__(
1090
1091
):
1091
1092
self .bins = ensure_int64 (bins )
1092
1093
self .binlabels = ensure_index (binlabels )
1094
+ self ._filter_empty_groups = False
1093
1095
self .mutated = mutated
1094
1096
self .indexer = indexer
1095
1097
@@ -1199,9 +1201,10 @@ def names(self) -> list[Hashable]:
1199
1201
1200
1202
@property
1201
1203
def groupings (self ) -> list [grouper .Grouping ]:
1202
- lev = self .binlabels
1203
- ping = grouper .Grouping (lev , lev , in_axis = False , level = None , name = lev .name )
1204
- return [ping ]
1204
+ return [
1205
+ grouper .Grouping (lvl , lvl , in_axis = False , level = None , name = name )
1206
+ for lvl , name in zip (self .levels , self .names )
1207
+ ]
1205
1208
1206
1209
def _aggregate_series_fast (self , obj : Series , func : F ) -> np .ndarray :
1207
1210
# -> np.ndarray[object]
0 commit comments