Skip to content

TYP: selection and groups type-hinting in groupby #36643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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