3
3
split-apply-combine paradigm.
4
4
"""
5
5
6
- from typing import Optional , Tuple
6
+ from typing import Hashable , List , Optional , Tuple
7
7
import warnings
8
8
9
9
import numpy as np
@@ -134,7 +134,7 @@ def _get_grouper(self, obj, validate=True):
134
134
"""
135
135
136
136
self ._set_grouper (obj )
137
- self .grouper , exclusions , self .obj = _get_grouper (
137
+ self .grouper , exclusions , self .obj = get_grouper (
138
138
self .obj ,
139
139
[self .key ],
140
140
axis = self .axis ,
@@ -429,7 +429,7 @@ def groups(self) -> dict:
429
429
return self .index .groupby (Categorical .from_codes (self .codes , self .group_index ))
430
430
431
431
432
- def _get_grouper (
432
+ def get_grouper (
433
433
obj : NDFrame ,
434
434
key = None ,
435
435
axis : int = 0 ,
@@ -438,9 +438,9 @@ def _get_grouper(
438
438
observed = False ,
439
439
mutated = False ,
440
440
validate = True ,
441
- ):
441
+ ) -> Tuple [ BaseGrouper , List [ Hashable ], NDFrame ] :
442
442
"""
443
- create and return a BaseGrouper, which is an internal
443
+ Create and return a BaseGrouper, which is an internal
444
444
mapping of how to create the grouper indexers.
445
445
This may be composed of multiple Grouping objects, indicating
446
446
multiple groupers
@@ -456,9 +456,9 @@ def _get_grouper(
456
456
a BaseGrouper.
457
457
458
458
If observed & we have a categorical grouper, only show the observed
459
- values
459
+ values.
460
460
461
- If validate, then check for key/level overlaps
461
+ If validate, then check for key/level overlaps.
462
462
463
463
"""
464
464
group_axis = obj ._get_axis (axis )
@@ -517,7 +517,7 @@ def _get_grouper(
517
517
if key .key is None :
518
518
return grouper , [], obj
519
519
else :
520
- return grouper , { key .key } , obj
520
+ return grouper , [ key .key ] , obj
521
521
522
522
# already have a BaseGrouper, just return it
523
523
elif isinstance (key , BaseGrouper ):
@@ -530,10 +530,8 @@ def _get_grouper(
530
530
# unhashable elements of `key`. Any unhashable elements implies that
531
531
# they wanted a list of keys.
532
532
# https://github.com/pandas-dev/pandas/issues/18314
533
- is_tuple = isinstance (key , tuple )
534
- all_hashable = is_tuple and is_hashable (key )
535
-
536
- if is_tuple :
533
+ if isinstance (key , tuple ):
534
+ all_hashable = is_hashable (key )
537
535
if (
538
536
all_hashable and key not in obj and set (key ).issubset (obj )
539
537
) or not all_hashable :
@@ -573,7 +571,7 @@ def _get_grouper(
573
571
all_in_columns_index = all (
574
572
g in obj .columns or g in obj .index .names for g in keys
575
573
)
576
- elif isinstance ( obj , Series ):
574
+ else : # Series
577
575
all_in_columns_index = all (g in obj .index .names for g in keys )
578
576
579
577
if not all_in_columns_index :
@@ -586,8 +584,8 @@ def _get_grouper(
586
584
else :
587
585
levels = [level ] * len (keys )
588
586
589
- groupings = []
590
- exclusions = []
587
+ groupings = [] # type: List[Grouping]
588
+ exclusions = [] # type: List[Hashable]
591
589
592
590
# if the actual grouper should be obj[key]
593
591
def is_in_axis (key ) -> bool :
0 commit comments