diff --git a/pandas/_libs/reduction.pyx b/pandas/_libs/reduction.pyx index 43d253f632f0f..b27072aa66708 100644 --- a/pandas/_libs/reduction.pyx +++ b/pandas/_libs/reduction.pyx @@ -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: diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 761353ca5a6ca..4e593ce543ea6 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -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) @@ -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: + slicer = lambda start, edge: data.iloc[start:edge] + else: + slicer = lambda start, edge: data.iloc[:, start:edge] + length = len(data.axes[axis]) start = 0 @@ -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): @@ -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: diff --git a/pandas/tests/groupby/test_bin_groupby.py b/pandas/tests/groupby/test_bin_groupby.py index ad71f73e80e64..ff74d374e5e3f 100644 --- a/pandas/tests/groupby/test_bin_groupby.py +++ b/pandas/tests/groupby/test_bin_groupby.py @@ -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) @@ -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`"):