Skip to content

TYP: Misc groupby typing #37066

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 1 commit into from
Oct 12, 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
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5417,7 +5417,7 @@ def __setattr__(self, name: str, value) -> None:
)
object.__setattr__(self, name, value)

def _dir_additions(self):
def _dir_additions(self) -> Set[str]:
"""
add the string-like attributes from the info_axis.
If info_axis is a MultiIndex, it's first level values are used.
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import numpy as np

from pandas._libs import lib, reduction as libreduction
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label
from pandas.util._decorators import Appender, Substitution, doc

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -1131,7 +1131,7 @@ def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
axis = self.axis
obj = self._obj_with_exclusions

result: Dict[Union[int, str], Union[NDFrame, np.ndarray]] = {}
result: Dict[Label, Union[NDFrame, np.ndarray]] = {}
if axis != obj._info_axis_number:
for name, data in self:
fres = func(data, *args, **kwargs)
Expand Down
19 changes: 11 additions & 8 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class providing the base-class of operations.
Generic,
Hashable,
Iterable,
Iterator,
List,
Mapping,
Optional,
Expand Down Expand Up @@ -465,7 +466,7 @@ def f(self):


@contextmanager
def group_selection_context(groupby: "BaseGroupBy"):
def group_selection_context(groupby: "BaseGroupBy") -> Iterator["BaseGroupBy"]:
"""
Set / reset the group_selection_context.
"""
Expand All @@ -486,7 +487,7 @@ def group_selection_context(groupby: "BaseGroupBy"):


class BaseGroupBy(PandasObject, SelectionMixin, Generic[FrameOrSeries]):
_group_selection = None
_group_selection: Optional[IndexLabel] = None
_apply_allowlist: FrozenSet[str] = frozenset()

def __init__(
Expand Down Expand Up @@ -570,7 +571,7 @@ def groups(self) -> Dict[Hashable, np.ndarray]:
return self.grouper.groups

@property
def ngroups(self):
def ngroups(self) -> int:
self._assure_grouper()
return self.grouper.ngroups

Expand Down Expand Up @@ -649,7 +650,7 @@ def _selected_obj(self):
else:
return self.obj[self._selection]

def _reset_group_selection(self):
def _reset_group_selection(self) -> None:
"""
Clear group based selection.

Expand All @@ -661,7 +662,7 @@ def _reset_group_selection(self):
self._group_selection = None
self._reset_cache("_selected_obj")

def _set_group_selection(self):
def _set_group_selection(self) -> None:
"""
Create group based selection.

Expand All @@ -686,7 +687,9 @@ def _set_group_selection(self):
self._group_selection = ax.difference(Index(groupers), sort=False).tolist()
self._reset_cache("_selected_obj")

def _set_result_index_ordered(self, result):
def _set_result_index_ordered(
self, result: "OutputFrameOrSeries"
) -> "OutputFrameOrSeries":
# set the result index on the passed values object and
# return the new object, xref 8046

Expand All @@ -700,7 +703,7 @@ def _set_result_index_ordered(self, result):
result.set_axis(self.obj._get_axis(self.axis), axis=self.axis, inplace=True)
return result

def _dir_additions(self):
def _dir_additions(self) -> Set[str]:
return self.obj._dir_additions() | self._apply_allowlist

def __getattr__(self, attr: str):
Expand Down Expand Up @@ -818,7 +821,7 @@ def get_group(self, name, obj=None):

return obj._take_with_is_copy(inds, axis=self.axis)

def __iter__(self):
def __iter__(self) -> Iterator[Tuple[Label, FrameOrSeries]]:
"""
Groupby iterator.

Expand Down
16 changes: 14 additions & 2 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
"""

import collections
from typing import Dict, Generic, Hashable, List, Optional, Sequence, Tuple, Type
from typing import (
Dict,
Generic,
Hashable,
Iterator,
List,
Optional,
Sequence,
Tuple,
Type,
)

import numpy as np

Expand Down Expand Up @@ -116,7 +126,9 @@ def __iter__(self):
def nkeys(self) -> int:
return len(self.groupings)

def get_iterator(self, data: FrameOrSeries, axis: int = 0):
def get_iterator(
self, data: FrameOrSeries, axis: int = 0
) -> Iterator[Tuple[Label, FrameOrSeries]]:
"""
Groupby iterator

Expand Down