Skip to content

REF: use public indexers in groupby.ops #31814

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
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ cdef class SeriesGrouper(_BaseGrouper):
def __init__(self, object series, object f, object labels,
Py_ssize_t ngroups, object dummy):

# in practice we always pass either obj[:0] or the
# safer obj._get_values(slice(None, 0))
# in practice we always pass obj.iloc[:0] or equivalent
assert dummy is not None

if len(series) == 0:
Expand Down
12 changes: 8 additions & 4 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def _aggregate_series_fast(self, obj: Series, func):
group_index, _, ngroups = self.group_info

# avoids object / Series creation overhead
dummy = obj._get_values(slice(None, 0))
dummy = obj.iloc[:0]
indexer = get_group_index_sorter(group_index, ngroups)
obj = obj.take(indexer)
group_index = algorithms.take_nd(group_index, indexer, allow_fill=False)
Expand Down Expand Up @@ -780,7 +780,11 @@ def get_iterator(self, data: FrameOrSeries, axis: int = 0):
Generator yielding sequence of (name, subsetted object)
for each group
"""
slicer = lambda start, edge: data._slice(slice(start, edge), axis=axis)
if axis == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

a more natural idiom (used in many places is)

indexer = [None] * 2
indexer[axis] = slice(start, edge)
slicer = data.iloc[indexer]

Copy link
Member Author

Choose a reason for hiding this comment

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

i guess, but thats not really conducive to being a lambda

Copy link
Contributor

Choose a reason for hiding this comment

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

why does this need to be a lamba?

Copy link
Member Author

Choose a reason for hiding this comment

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

doesnt have to be, thats just the pattern we have now. i find iloc[:, start:end] clearer than the 3-line version

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

slicer = lambda start, edge: data.iloc[start:edge]
else:
slicer = lambda start, edge: data.iloc[:, start:edge]

length = len(data.axes[axis])

start = 0
Expand Down Expand Up @@ -919,7 +923,7 @@ def _chop(self, sdata, slice_obj: slice) -> NDFrame:

class SeriesSplitter(DataSplitter):
def _chop(self, sdata: Series, slice_obj: slice) -> Series:
return sdata._get_values(slice_obj)
return sdata.iloc[slice_obj]


class FrameSplitter(DataSplitter):
Expand All @@ -934,7 +938,7 @@ def _chop(self, sdata: DataFrame, slice_obj: slice) -> DataFrame:
if self.axis == 0:
return sdata.iloc[slice_obj]
else:
return sdata._slice(slice_obj, axis=1)
return sdata.iloc[:, slice_obj]


def get_splitter(data: FrameOrSeries, *args, **kwargs) -> DataSplitter:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/groupby/test_bin_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def test_series_grouper():
obj = Series(np.random.randn(10))
dummy = obj[:0]
dummy = obj.iloc[:0]

labels = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1, 1], dtype=np.int64)

Expand All @@ -28,7 +28,7 @@ def test_series_grouper():
def test_series_grouper_requires_nonempty_raises():
# GH#29500
obj = Series(np.random.randn(10))
dummy = obj[:0]
dummy = obj.iloc[:0]
labels = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1, 1], dtype=np.int64)

with pytest.raises(ValueError, match="SeriesGrouper requires non-empty `series`"):
Expand Down