Skip to content

Commit 8cfcf65

Browse files
authored
REF: use public indexers in groupby.ops (#31814)
1 parent b30d7d4 commit 8cfcf65

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

pandas/_libs/reduction.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ cdef class SeriesGrouper(_BaseGrouper):
309309
def __init__(self, object series, object f, object labels,
310310
Py_ssize_t ngroups, object dummy):
311311

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

316315
if len(series) == 0:

pandas/core/groupby/ops.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ def _aggregate_series_fast(self, obj: Series, func):
658658
group_index, _, ngroups = self.group_info
659659

660660
# avoids object / Series creation overhead
661-
dummy = obj._get_values(slice(None, 0))
661+
dummy = obj.iloc[:0]
662662
indexer = get_group_index_sorter(group_index, ngroups)
663663
obj = obj.take(indexer)
664664
group_index = algorithms.take_nd(group_index, indexer, allow_fill=False)
@@ -780,7 +780,11 @@ def get_iterator(self, data: FrameOrSeries, axis: int = 0):
780780
Generator yielding sequence of (name, subsetted object)
781781
for each group
782782
"""
783-
slicer = lambda start, edge: data._slice(slice(start, edge), axis=axis)
783+
if axis == 0:
784+
slicer = lambda start, edge: data.iloc[start:edge]
785+
else:
786+
slicer = lambda start, edge: data.iloc[:, start:edge]
787+
784788
length = len(data.axes[axis])
785789

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

920924
class SeriesSplitter(DataSplitter):
921925
def _chop(self, sdata: Series, slice_obj: slice) -> Series:
922-
return sdata._get_values(slice_obj)
926+
return sdata.iloc[slice_obj]
923927

924928

925929
class FrameSplitter(DataSplitter):
@@ -934,7 +938,7 @@ def _chop(self, sdata: DataFrame, slice_obj: slice) -> DataFrame:
934938
if self.axis == 0:
935939
return sdata.iloc[slice_obj]
936940
else:
937-
return sdata._slice(slice_obj, axis=1)
941+
return sdata.iloc[:, slice_obj]
938942

939943

940944
def get_splitter(data: FrameOrSeries, *args, **kwargs) -> DataSplitter:

pandas/tests/groupby/test_bin_groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
def test_series_grouper():
1313
obj = Series(np.random.randn(10))
14-
dummy = obj[:0]
14+
dummy = obj.iloc[:0]
1515

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

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

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

0 commit comments

Comments
 (0)