|
11 | 11 | from functools import partial
|
12 | 12 | from textwrap import dedent
|
13 | 13 | 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 | +) |
15 | 25 | import warnings
|
16 | 26 |
|
17 | 27 | import numpy as np
|
@@ -132,7 +142,7 @@ def pinner(cls):
|
132 | 142 | class SeriesGroupBy(GroupBy):
|
133 | 143 | _apply_whitelist = base.series_apply_whitelist
|
134 | 144 |
|
135 |
| - def _iterate_slices(self): |
| 145 | + def _iterate_slices(self) -> Iterable[Tuple[Hashable, Series]]: |
136 | 146 | yield self._selection_name, self._selected_obj
|
137 | 147 |
|
138 | 148 | @property
|
@@ -898,22 +908,20 @@ def aggregate(self, func=None, *args, **kwargs):
|
898 | 908 |
|
899 | 909 | agg = aggregate
|
900 | 910 |
|
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 |
909 | 919 | 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 |
912 | 923 |
|
913 |
| - for val in slice_axis: |
914 |
| - if val in self.exclusions: |
915 |
| - continue |
916 |
| - yield val, slicer(val) |
| 924 | + yield label, values |
917 | 925 |
|
918 | 926 | def _cython_agg_general(self, how, alt=None, numeric_only=True, min_count=-1):
|
919 | 927 | new_items, new_blocks = self._cython_agg_blocks(
|
|
0 commit comments