Skip to content

Commit c3ff9bf

Browse files
rhshadrachJulianWgs
authored andcommitted
TYP: Misc groupby typing (pandas-dev#37066)
1 parent a609874 commit c3ff9bf

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5417,7 +5417,7 @@ def __setattr__(self, name: str, value) -> None:
54175417
)
54185418
object.__setattr__(self, name, value)
54195419

5420-
def _dir_additions(self):
5420+
def _dir_additions(self) -> Set[str]:
54215421
"""
54225422
add the string-like attributes from the info_axis.
54235423
If info_axis is a MultiIndex, it's first level values are used.

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import numpy as np
3131

3232
from pandas._libs import lib, reduction as libreduction
33-
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion
33+
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label
3434
from pandas.util._decorators import Appender, Substitution, doc
3535

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

1134-
result: Dict[Union[int, str], Union[NDFrame, np.ndarray]] = {}
1134+
result: Dict[Label, Union[NDFrame, np.ndarray]] = {}
11351135
if axis != obj._info_axis_number:
11361136
for name, data in self:
11371137
fres = func(data, *args, **kwargs)

pandas/core/groupby/groupby.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class providing the base-class of operations.
2020
Generic,
2121
Hashable,
2222
Iterable,
23+
Iterator,
2324
List,
2425
Mapping,
2526
Optional,
@@ -467,7 +468,7 @@ def f(self):
467468

468469

469470
@contextmanager
470-
def group_selection_context(groupby: "BaseGroupBy"):
471+
def group_selection_context(groupby: "BaseGroupBy") -> Iterator["BaseGroupBy"]:
471472
"""
472473
Set / reset the group_selection_context.
473474
"""
@@ -488,7 +489,7 @@ def group_selection_context(groupby: "BaseGroupBy"):
488489

489490

490491
class BaseGroupBy(PandasObject, SelectionMixin, Generic[FrameOrSeries]):
491-
_group_selection = None
492+
_group_selection: Optional[IndexLabel] = None
492493
_apply_allowlist: FrozenSet[str] = frozenset()
493494

494495
def __init__(
@@ -576,7 +577,7 @@ def groups(self) -> Dict[Hashable, np.ndarray]:
576577
return self.grouper.groups
577578

578579
@property
579-
def ngroups(self):
580+
def ngroups(self) -> int:
580581
self._assure_grouper()
581582
return self.grouper.ngroups
582583

@@ -655,7 +656,7 @@ def _selected_obj(self):
655656
else:
656657
return self.obj[self._selection]
657658

658-
def _reset_group_selection(self):
659+
def _reset_group_selection(self) -> None:
659660
"""
660661
Clear group based selection.
661662
@@ -667,7 +668,7 @@ def _reset_group_selection(self):
667668
self._group_selection = None
668669
self._reset_cache("_selected_obj")
669670

670-
def _set_group_selection(self):
671+
def _set_group_selection(self) -> None:
671672
"""
672673
Create group based selection.
673674
@@ -692,7 +693,9 @@ def _set_group_selection(self):
692693
self._group_selection = ax.difference(Index(groupers), sort=False).tolist()
693694
self._reset_cache("_selected_obj")
694695

695-
def _set_result_index_ordered(self, result):
696+
def _set_result_index_ordered(
697+
self, result: "OutputFrameOrSeries"
698+
) -> "OutputFrameOrSeries":
696699
# set the result index on the passed values object and
697700
# return the new object, xref 8046
698701

@@ -706,7 +709,7 @@ def _set_result_index_ordered(self, result):
706709
result.set_axis(self.obj._get_axis(self.axis), axis=self.axis, inplace=True)
707710
return result
708711

709-
def _dir_additions(self):
712+
def _dir_additions(self) -> Set[str]:
710713
return self.obj._dir_additions() | self._apply_allowlist
711714

712715
def __getattr__(self, attr: str):
@@ -824,7 +827,7 @@ def get_group(self, name, obj=None):
824827

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

827-
def __iter__(self):
830+
def __iter__(self) -> Iterator[Tuple[Label, FrameOrSeries]]:
828831
"""
829832
Groupby iterator.
830833

pandas/core/groupby/ops.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
"""
88

99
import collections
10-
from typing import Dict, Generic, Hashable, List, Optional, Sequence, Tuple, Type
10+
from typing import (
11+
Dict,
12+
Generic,
13+
Hashable,
14+
Iterator,
15+
List,
16+
Optional,
17+
Sequence,
18+
Tuple,
19+
Type,
20+
)
1121

1222
import numpy as np
1323

@@ -116,7 +126,9 @@ def __iter__(self):
116126
def nkeys(self) -> int:
117127
return len(self.groupings)
118128

119-
def get_iterator(self, data: FrameOrSeries, axis: int = 0):
129+
def get_iterator(
130+
self, data: FrameOrSeries, axis: int = 0
131+
) -> Iterator[Tuple[Label, FrameOrSeries]]:
120132
"""
121133
Groupby iterator
122134

0 commit comments

Comments
 (0)