Skip to content

CLN: Consistent and Annotated Return Type of _iterate_slices #28958

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 5 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 24 additions & 16 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
from functools import partial
from textwrap import dedent
import typing
from typing import Any, Callable, FrozenSet, Sequence, Type, Union
from typing import (
Any,
Callable,
FrozenSet,
Hashable,
Iterator,
Sequence,
Tuple,
Type,
Union,
)
import warnings

import numpy as np
Expand Down Expand Up @@ -132,7 +142,7 @@ def pinner(cls):
class SeriesGroupBy(GroupBy):
_apply_whitelist = base.series_apply_whitelist

def _iterate_slices(self):
def _iterate_slices(self) -> Iterator[Tuple[Hashable, Series]]:
yield self._selection_name, self._selected_obj

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

agg = aggregate

def _iterate_slices(self):
if self.axis == 0:
# kludge
if self._selection is None:
slice_axis = self.obj.columns
else:
slice_axis = self._selection_list
slicer = lambda x: self.obj[x]
def _iterate_slices(self) -> Iterator[Tuple[Hashable, Series]]:
obj = self._selected_obj.copy()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why copy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mistake on my end. Didn’t want to mutate passed frame in the axis=1 case but I can get rid of this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off topic: can we make the axis=1 case unnecessary by just transposing when we see it? Not sure exactly how that would work, but it would let us get rid of some lambda-wrapping, which would be nice.

if self.axis == 1:
obj = obj.T

if isinstance(obj, Series) and obj.name not in self.exclusions:
# Occurs when doing DataFrameGroupBy(...)["X"]
yield obj.name, obj
else:
slice_axis = self.obj.index
slicer = self.obj.xs
for label, values in obj.items():
if label in self.exclusions:
continue

for val in slice_axis:
if val in self.exclusions:
continue
yield val, slicer(val)
yield label, values

def _cython_agg_general(self, how, alt=None, numeric_only=True, min_count=-1):
new_items, new_blocks = self._cython_agg_blocks(
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class providing the base-class of operations.
import inspect
import re
import types
from typing import FrozenSet, List, Optional, Tuple, Type, Union
from typing import FrozenSet, Hashable, Iterator, List, Optional, Tuple, Type, Union

import numpy as np

Expand Down Expand Up @@ -758,7 +758,7 @@ def _python_apply_general(self, f):
keys, values, not_indexed_same=mutated or self.mutated
)

def _iterate_slices(self):
def _iterate_slices(self) -> Iterator[Tuple[Hashable, Series]]:
raise AbstractMethodError(self)

def transform(self, func, *args, **kwargs):
Expand Down