Skip to content

Commit

Permalink
type up core.groupby.grouper.get_grouper
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed Nov 7, 2019
1 parent e131b21 commit 18f1516
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
8 changes: 4 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ def __init__(
self.mutated = kwargs.pop("mutated", False)

if grouper is None:
from pandas.core.groupby.grouper import _get_grouper
from pandas.core.groupby.grouper import get_grouper

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

# create a grouper with the original parameters, but on dropped
# object
from pandas.core.groupby.grouper import _get_grouper
from pandas.core.groupby.grouper import get_grouper

grouper, _, _ = _get_grouper(
grouper, _, _ = get_grouper(
dropped,
key=self.keys,
axis=self.axis,
Expand Down
28 changes: 13 additions & 15 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
split-apply-combine paradigm.
"""

from typing import Optional, Tuple
from typing import Hashable, List, Optional, Tuple
import warnings

import numpy as np
Expand Down Expand Up @@ -134,7 +134,7 @@ def _get_grouper(self, obj, validate=True):
"""

self._set_grouper(obj)
self.grouper, exclusions, self.obj = _get_grouper(
self.grouper, exclusions, self.obj = get_grouper(
self.obj,
[self.key],
axis=self.axis,
Expand Down Expand Up @@ -429,7 +429,7 @@ def groups(self) -> dict:
return self.index.groupby(Categorical.from_codes(self.codes, self.group_index))


def _get_grouper(
def get_grouper(
obj: NDFrame,
key=None,
axis: int = 0,
Expand All @@ -438,9 +438,9 @@ def _get_grouper(
observed=False,
mutated=False,
validate=True,
):
) -> Tuple[BaseGrouper, List[Hashable], NDFrame]:
"""
create and return a BaseGrouper, which is an internal
Create and return a BaseGrouper, which is an internal
mapping of how to create the grouper indexers.
This may be composed of multiple Grouping objects, indicating
multiple groupers
Expand All @@ -456,9 +456,9 @@ def _get_grouper(
a BaseGrouper.
If observed & we have a categorical grouper, only show the observed
values
values.
If validate, then check for key/level overlaps
If validate, then check for key/level overlaps.
"""
group_axis = obj._get_axis(axis)
Expand Down Expand Up @@ -517,7 +517,7 @@ def _get_grouper(
if key.key is None:
return grouper, [], obj
else:
return grouper, {key.key}, obj
return grouper, [key.key], obj

# already have a BaseGrouper, just return it
elif isinstance(key, BaseGrouper):
Expand All @@ -530,10 +530,8 @@ def _get_grouper(
# unhashable elements of `key`. Any unhashable elements implies that
# they wanted a list of keys.
# https://github.com/pandas-dev/pandas/issues/18314
is_tuple = isinstance(key, tuple)
all_hashable = is_tuple and is_hashable(key)

if is_tuple:
if isinstance(key, tuple):
all_hashable = is_hashable(key)
if (
all_hashable and key not in obj and set(key).issubset(obj)
) or not all_hashable:
Expand Down Expand Up @@ -573,7 +571,7 @@ def _get_grouper(
all_in_columns_index = all(
g in obj.columns or g in obj.index.names for g in keys
)
elif isinstance(obj, Series):
else: # Series
all_in_columns_index = all(g in obj.index.names for g in keys)

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

groupings = []
exclusions = []
groupings = [] # type: List[Grouping]
exclusions = [] # type: List[Hashable]

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

0 comments on commit 18f1516

Please sign in to comment.