@@ -425,14 +425,22 @@ class Grouping:
425
425
If we are a Categorical, use the observed values
426
426
in_axis : if the Grouping is a column in self.obj and hence among
427
427
Groupby.exclusions list
428
+ dropna : bool, default True
429
+ Whether to drop NA groups.
430
+ uniques : Array-like, optional
431
+ When specified, will be used for unique values. Enables including empty groups
432
+ in the result for a BinGrouper. Must not contain duplicates.
428
433
429
- Returns
434
+ Attributes
430
435
-------
431
- **Attributes**:
432
- * indices : dict of {group -> index_list}
433
- * codes : ndarray, group codes
434
- * group_index : unique groups
435
- * groups : dict of {group -> label_list}
436
+ indices : dict
437
+ Mapping of {group -> index_list}
438
+ codes : ndarray
439
+ Group codes
440
+ group_index : Index or None
441
+ unique groups
442
+ groups : dict
443
+ Mapping of {group -> label_list}
436
444
"""
437
445
438
446
_codes : npt .NDArray [np .signedinteger ] | None = None
@@ -452,6 +460,7 @@ def __init__(
452
460
observed : bool = False ,
453
461
in_axis : bool = False ,
454
462
dropna : bool = True ,
463
+ uniques : ArrayLike | None = None ,
455
464
) -> None :
456
465
self .level = level
457
466
self ._orig_grouper = grouper
@@ -464,6 +473,7 @@ def __init__(
464
473
self ._observed = observed
465
474
self .in_axis = in_axis
466
475
self ._dropna = dropna
476
+ self ._uniques = uniques
467
477
468
478
self ._passed_categorical = False
469
479
@@ -653,6 +663,7 @@ def group_index(self) -> Index:
653
663
654
664
@cache_readonly
655
665
def _codes_and_uniques (self ) -> tuple [npt .NDArray [np .signedinteger ], ArrayLike ]:
666
+ uniques : ArrayLike
656
667
if self ._passed_categorical :
657
668
# we make a CategoricalIndex out of the cat grouper
658
669
# preserving the categories / ordered attributes;
@@ -697,11 +708,13 @@ def _codes_and_uniques(self) -> tuple[npt.NDArray[np.signedinteger], ArrayLike]:
697
708
elif isinstance (self .grouping_vector , ops .BaseGrouper ):
698
709
# we have a list of groupers
699
710
codes = self .grouping_vector .codes_info
700
- # error: Incompatible types in assignment (expression has type "Union
701
- # [ExtensionArray, ndarray[Any, Any]]", variable has type "Categorical")
702
- uniques = (
703
- self .grouping_vector .result_index ._values # type: ignore[assignment]
704
- )
711
+ uniques = self .grouping_vector .result_index ._values
712
+ elif self ._uniques is not None :
713
+ # GH#50486 Code grouping_vector using _uniques; allows
714
+ # including uniques that are not present in grouping_vector.
715
+ cat = Categorical (self .grouping_vector , categories = self ._uniques )
716
+ codes = cat .codes
717
+ uniques = self ._uniques
705
718
else :
706
719
# GH35667, replace dropna=False with use_na_sentinel=False
707
720
# error: Incompatible types in assignment (expression has type "Union[
0 commit comments