Skip to content

Commit 18f1516

Browse files
committed
type up core.groupby.grouper.get_grouper
1 parent e131b21 commit 18f1516

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

pandas/core/groupby/groupby.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,9 @@ def __init__(
379379
self.mutated = kwargs.pop("mutated", False)
380380

381381
if grouper is None:
382-
from pandas.core.groupby.grouper import _get_grouper
382+
from pandas.core.groupby.grouper import get_grouper
383383

384-
grouper, exclusions, obj = _get_grouper(
384+
grouper, exclusions, obj = get_grouper(
385385
obj,
386386
keys,
387387
axis=axis,
@@ -1802,9 +1802,9 @@ def nth(self, n: Union[int, List[int]], dropna: Optional[str] = None) -> DataFra
18021802

18031803
# create a grouper with the original parameters, but on dropped
18041804
# object
1805-
from pandas.core.groupby.grouper import _get_grouper
1805+
from pandas.core.groupby.grouper import get_grouper
18061806

1807-
grouper, _, _ = _get_grouper(
1807+
grouper, _, _ = get_grouper(
18081808
dropped,
18091809
key=self.keys,
18101810
axis=self.axis,

pandas/core/groupby/grouper.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
split-apply-combine paradigm.
44
"""
55

6-
from typing import Optional, Tuple
6+
from typing import Hashable, List, Optional, Tuple
77
import warnings
88

99
import numpy as np
@@ -134,7 +134,7 @@ def _get_grouper(self, obj, validate=True):
134134
"""
135135

136136
self._set_grouper(obj)
137-
self.grouper, exclusions, self.obj = _get_grouper(
137+
self.grouper, exclusions, self.obj = get_grouper(
138138
self.obj,
139139
[self.key],
140140
axis=self.axis,
@@ -429,7 +429,7 @@ def groups(self) -> dict:
429429
return self.index.groupby(Categorical.from_codes(self.codes, self.group_index))
430430

431431

432-
def _get_grouper(
432+
def get_grouper(
433433
obj: NDFrame,
434434
key=None,
435435
axis: int = 0,
@@ -438,9 +438,9 @@ def _get_grouper(
438438
observed=False,
439439
mutated=False,
440440
validate=True,
441-
):
441+
) -> Tuple[BaseGrouper, List[Hashable], NDFrame]:
442442
"""
443-
create and return a BaseGrouper, which is an internal
443+
Create and return a BaseGrouper, which is an internal
444444
mapping of how to create the grouper indexers.
445445
This may be composed of multiple Grouping objects, indicating
446446
multiple groupers
@@ -456,9 +456,9 @@ def _get_grouper(
456456
a BaseGrouper.
457457
458458
If observed & we have a categorical grouper, only show the observed
459-
values
459+
values.
460460
461-
If validate, then check for key/level overlaps
461+
If validate, then check for key/level overlaps.
462462
463463
"""
464464
group_axis = obj._get_axis(axis)
@@ -517,7 +517,7 @@ def _get_grouper(
517517
if key.key is None:
518518
return grouper, [], obj
519519
else:
520-
return grouper, {key.key}, obj
520+
return grouper, [key.key], obj
521521

522522
# already have a BaseGrouper, just return it
523523
elif isinstance(key, BaseGrouper):
@@ -530,10 +530,8 @@ def _get_grouper(
530530
# unhashable elements of `key`. Any unhashable elements implies that
531531
# they wanted a list of keys.
532532
# 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)
537535
if (
538536
all_hashable and key not in obj and set(key).issubset(obj)
539537
) or not all_hashable:
@@ -573,7 +571,7 @@ def _get_grouper(
573571
all_in_columns_index = all(
574572
g in obj.columns or g in obj.index.names for g in keys
575573
)
576-
elif isinstance(obj, Series):
574+
else: # Series
577575
all_in_columns_index = all(g in obj.index.names for g in keys)
578576

579577
if not all_in_columns_index:
@@ -586,8 +584,8 @@ def _get_grouper(
586584
else:
587585
levels = [level] * len(keys)
588586

589-
groupings = []
590-
exclusions = []
587+
groupings = [] # type: List[Grouping]
588+
exclusions = [] # type: List[Hashable]
591589

592590
# if the actual grouper should be obj[key]
593591
def is_in_axis(key) -> bool:

0 commit comments

Comments
 (0)