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
26
26
from pandas .core .arrays import Categorical , ExtensionArray
27
27
import pandas .core .common as com
28
28
from pandas .core .frame import DataFrame
29
- from pandas .core .generic import NDFrame
30
29
from pandas .core .groupby .categorical import recode_for_groupby , recode_from_groupby
31
30
from pandas .core .groupby .ops import BaseGrouper
32
31
from pandas .core .index import CategoricalIndex , Index , MultiIndex
@@ -134,7 +133,7 @@ def _get_grouper(self, obj, validate=True):
134
133
"""
135
134
136
135
self ._set_grouper (obj )
137
- self .grouper , exclusions , self .obj = _get_grouper (
136
+ self .grouper , exclusions , self .obj = get_grouper (
138
137
self .obj ,
139
138
[self .key ],
140
139
axis = self .axis ,
@@ -429,18 +428,18 @@ def groups(self) -> dict:
429
428
return self .index .groupby (Categorical .from_codes (self .codes , self .group_index ))
430
429
431
430
432
- def _get_grouper (
433
- obj : NDFrame ,
431
+ def get_grouper (
432
+ obj : FrameOrSeries ,
434
433
key = None ,
435
434
axis : int = 0 ,
436
435
level = None ,
437
436
sort = True ,
438
437
observed = False ,
439
438
mutated = False ,
440
439
validate = True ,
441
- ):
440
+ ) -> Tuple [ BaseGrouper , List [ Hashable ], FrameOrSeries ] :
442
441
"""
443
- create and return a BaseGrouper, which is an internal
442
+ Create and return a BaseGrouper, which is an internal
444
443
mapping of how to create the grouper indexers.
445
444
This may be composed of multiple Grouping objects, indicating
446
445
multiple groupers
@@ -456,9 +455,9 @@ def _get_grouper(
456
455
a BaseGrouper.
457
456
458
457
If observed & we have a categorical grouper, only show the observed
459
- values
458
+ values.
460
459
461
- If validate, then check for key/level overlaps
460
+ If validate, then check for key/level overlaps.
462
461
463
462
"""
464
463
group_axis = obj ._get_axis (axis )
@@ -517,7 +516,7 @@ def _get_grouper(
517
516
if key .key is None :
518
517
return grouper , [], obj
519
518
else :
520
- return grouper , { key .key } , obj
519
+ return grouper , [ key .key ] , obj
521
520
522
521
# already have a BaseGrouper, just return it
523
522
elif isinstance (key , BaseGrouper ):
@@ -530,10 +529,8 @@ def _get_grouper(
530
529
# unhashable elements of `key`. Any unhashable elements implies that
531
530
# they wanted a list of keys.
532
531
# 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 :
532
+ if isinstance (key , tuple ):
533
+ all_hashable = is_hashable (key )
537
534
if (
538
535
all_hashable and key not in obj and set (key ).issubset (obj )
539
536
) or not all_hashable :
@@ -573,7 +570,8 @@ def _get_grouper(
573
570
all_in_columns_index = all (
574
571
g in obj .columns or g in obj .index .names for g in keys
575
572
)
576
- elif isinstance (obj , Series ):
573
+ else :
574
+ assert isinstance (obj , 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