Skip to content

Commit e9967d9

Browse files
rhshadrachpull[bot]
authored andcommitted
TYP: Misc groupby typing (#37066)
1 parent 7db128a commit e9967d9

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,
@@ -465,7 +466,7 @@ def f(self):
465466

466467

467468
@contextmanager
468-
def group_selection_context(groupby: "BaseGroupBy"):
469+
def group_selection_context(groupby: "BaseGroupBy") -> Iterator["BaseGroupBy"]:
469470
"""
470471
Set / reset the group_selection_context.
471472
"""
@@ -486,7 +487,7 @@ def group_selection_context(groupby: "BaseGroupBy"):
486487

487488

488489
class BaseGroupBy(PandasObject, SelectionMixin, Generic[FrameOrSeries]):
489-
_group_selection = None
490+
_group_selection: Optional[IndexLabel] = None
490491
_apply_allowlist: FrozenSet[str] = frozenset()
491492

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

572573
@property
573-
def ngroups(self):
574+
def ngroups(self) -> int:
574575
self._assure_grouper()
575576
return self.grouper.ngroups
576577

@@ -649,7 +650,7 @@ def _selected_obj(self):
649650
else:
650651
return self.obj[self._selection]
651652

652-
def _reset_group_selection(self):
653+
def _reset_group_selection(self) -> None:
653654
"""
654655
Clear group based selection.
655656
@@ -661,7 +662,7 @@ def _reset_group_selection(self):
661662
self._group_selection = None
662663
self._reset_cache("_selected_obj")
663664

664-
def _set_group_selection(self):
665+
def _set_group_selection(self) -> None:
665666
"""
666667
Create group based selection.
667668
@@ -686,7 +687,9 @@ def _set_group_selection(self):
686687
self._group_selection = ax.difference(Index(groupers), sort=False).tolist()
687688
self._reset_cache("_selected_obj")
688689

689-
def _set_result_index_ordered(self, result):
690+
def _set_result_index_ordered(
691+
self, result: "OutputFrameOrSeries"
692+
) -> "OutputFrameOrSeries":
690693
# set the result index on the passed values object and
691694
# return the new object, xref 8046
692695

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

703-
def _dir_additions(self):
706+
def _dir_additions(self) -> Set[str]:
704707
return self.obj._dir_additions() | self._apply_allowlist
705708

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

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

821-
def __iter__(self):
824+
def __iter__(self) -> Iterator[Tuple[Label, FrameOrSeries]]:
822825
"""
823826
Groupby iterator.
824827

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)