Skip to content

Commit

Permalink
TYP: selection and groups type-hinting in groupby (pandas-dev#36643)
Browse files Browse the repository at this point in the history
* TYP: selection and groups type-hinting in groupby

* PrettyDict -> Dict

* Cleanup from merge
  • Loading branch information
rhshadrach authored and Kevin D Smith committed Nov 2, 2020
1 parent 80e02c5 commit 29ddbc8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np

import pandas._libs.lib as lib
from pandas._typing import AggFuncType, AggFuncTypeBase, Label
from pandas._typing import AggFuncType, AggFuncTypeBase, IndexLabel, Label
from pandas.compat import PYPY
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
Expand Down Expand Up @@ -136,7 +136,7 @@ class SelectionMixin:
object sub-classes need to define: obj, exclusions
"""

_selection = None
_selection: Optional[IndexLabel] = None
_internal_names = ["_cache", "__setstate__"]
_internal_names_set = set(_internal_names)

Expand Down
17 changes: 12 additions & 5 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ class providing the base-class of operations.

from pandas._libs import Timestamp, lib
import pandas._libs.groupby as libgroupby
from pandas._typing import F, FrameOrSeries, FrameOrSeriesUnion, Label, Scalar
from pandas._typing import (
F,
FrameOrSeries,
FrameOrSeriesUnion,
IndexLabel,
Label,
Scalar,
)
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import Appender, Substitution, cache_readonly, doc
Expand Down Expand Up @@ -487,10 +494,10 @@ def __init__(
obj: FrameOrSeries,
keys: Optional[_KeysArgType] = None,
axis: int = 0,
level=None,
level: Optional[IndexLabel] = None,
grouper: Optional["ops.BaseGrouper"] = None,
exclusions: Optional[Set[Label]] = None,
selection=None,
selection: Optional[IndexLabel] = None,
as_index: bool = True,
sort: bool = True,
group_keys: bool = True,
Expand Down Expand Up @@ -547,15 +554,15 @@ def __repr__(self) -> str:
# TODO: Better repr for GroupBy object
return object.__repr__(self)

def _assure_grouper(self):
def _assure_grouper(self) -> None:
"""
We create the grouper on instantiation sub-classes may have a
different policy.
"""
pass

@property
def groups(self):
def groups(self) -> Dict[Hashable, np.ndarray]:
"""
Dict {group name -> group labels}.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

import collections
from typing import List, Optional, Sequence, Tuple, Type
from typing import Dict, Hashable, List, Optional, Sequence, Tuple, Type

import numpy as np

Expand Down Expand Up @@ -249,7 +249,7 @@ def size(self) -> Series:
return Series(out, index=self.result_index, dtype="int64")

@cache_readonly
def groups(self):
def groups(self) -> Dict[Hashable, np.ndarray]:
""" dict {group name -> group labels} """
if len(self.groupings) == 1:
return self.groupings[0].groups
Expand Down

0 comments on commit 29ddbc8

Please sign in to comment.