Skip to content

Commit 9af692f

Browse files
WillAydproost
authored andcommitted
CLN: Consistent and Annotated Return Type of _iterate_slices (pandas-dev#28958)
1 parent dd1b40b commit 9af692f

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

pandas/core/frame.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@
1414
import itertools
1515
import sys
1616
from textwrap import dedent
17-
from typing import FrozenSet, List, Optional, Sequence, Set, Tuple, Type, Union
17+
from typing import (
18+
FrozenSet,
19+
Hashable,
20+
Iterable,
21+
List,
22+
Optional,
23+
Sequence,
24+
Set,
25+
Tuple,
26+
Type,
27+
Union,
28+
)
1829
import warnings
1930

2031
import numpy as np
@@ -861,7 +872,7 @@ def style(self):
861872
"""
862873

863874
@Appender(_shared_docs["items"])
864-
def items(self):
875+
def items(self) -> Iterable[Tuple[Hashable, Series]]:
865876
if self.columns.is_unique and hasattr(self, "_item_cache"):
866877
for k in self.columns:
867878
yield k, self._get_item_cache(k)

pandas/core/groupby/generic.py

+24-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111
from functools import partial
1212
from textwrap import dedent
1313
import typing
14-
from typing import Any, Callable, FrozenSet, Sequence, Type, Union
14+
from typing import (
15+
Any,
16+
Callable,
17+
FrozenSet,
18+
Hashable,
19+
Iterable,
20+
Sequence,
21+
Tuple,
22+
Type,
23+
Union,
24+
)
1525
import warnings
1626

1727
import numpy as np
@@ -132,7 +142,7 @@ def pinner(cls):
132142
class SeriesGroupBy(GroupBy):
133143
_apply_whitelist = base.series_apply_whitelist
134144

135-
def _iterate_slices(self):
145+
def _iterate_slices(self) -> Iterable[Tuple[Hashable, Series]]:
136146
yield self._selection_name, self._selected_obj
137147

138148
@property
@@ -898,22 +908,20 @@ def aggregate(self, func=None, *args, **kwargs):
898908

899909
agg = aggregate
900910

901-
def _iterate_slices(self):
902-
if self.axis == 0:
903-
# kludge
904-
if self._selection is None:
905-
slice_axis = self.obj.columns
906-
else:
907-
slice_axis = self._selection_list
908-
slicer = lambda x: self.obj[x]
911+
def _iterate_slices(self) -> Iterable[Tuple[Hashable, Series]]:
912+
obj = self._selected_obj
913+
if self.axis == 1:
914+
obj = obj.T
915+
916+
if isinstance(obj, Series) and obj.name not in self.exclusions:
917+
# Occurs when doing DataFrameGroupBy(...)["X"]
918+
yield obj.name, obj
909919
else:
910-
slice_axis = self.obj.index
911-
slicer = self.obj.xs
920+
for label, values in obj.items():
921+
if label in self.exclusions:
922+
continue
912923

913-
for val in slice_axis:
914-
if val in self.exclusions:
915-
continue
916-
yield val, slicer(val)
924+
yield label, values
917925

918926
def _cython_agg_general(self, how, alt=None, numeric_only=True, min_count=-1):
919927
new_items, new_blocks = self._cython_agg_blocks(

pandas/core/groupby/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class providing the base-class of operations.
1414
import inspect
1515
import re
1616
import types
17-
from typing import FrozenSet, List, Optional, Tuple, Type, Union
17+
from typing import FrozenSet, Hashable, Iterable, List, Optional, Tuple, Type, Union
1818

1919
import numpy as np
2020

@@ -751,7 +751,7 @@ def _python_apply_general(self, f):
751751
keys, values, not_indexed_same=mutated or self.mutated
752752
)
753753

754-
def _iterate_slices(self):
754+
def _iterate_slices(self) -> Iterable[Tuple[Hashable, Series]]:
755755
raise AbstractMethodError(self)
756756

757757
def transform(self, func, *args, **kwargs):

0 commit comments

Comments
 (0)