Skip to content

Commit 787ea54

Browse files
jbrockmendeljreback
authored andcommitted
REF: de-duplicate _apply_to_group (#29628)
1 parent 7038f85 commit 787ea54

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

pandas/_libs/reduction.pyx

+29-24
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,27 @@ cdef class _BaseGrouper:
189189

190190
return cached_typ, cached_ityp
191191

192+
cdef inline object _apply_to_group(self,
193+
object cached_typ, object cached_ityp,
194+
Slider islider, Slider vslider,
195+
Py_ssize_t group_size, bint initialized):
196+
"""
197+
Call self.f on our new group, then update to the next group.
198+
"""
199+
cached_ityp._engine.clear_mapping()
200+
res = self.f(cached_typ)
201+
res = _extract_result(res)
202+
if not initialized:
203+
# On the first pass, we check the output shape to see
204+
# if this looks like a reduction.
205+
initialized = 1
206+
_check_result_array(res, len(self.dummy_arr))
207+
208+
islider.advance(group_size)
209+
vslider.advance(group_size)
210+
211+
return res, initialized
212+
192213

193214
cdef class SeriesBinGrouper(_BaseGrouper):
194215
"""
@@ -217,7 +238,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
217238
self.typ = series._constructor
218239
self.ityp = series.index._constructor
219240
self.index = series.index.values
220-
self.name = getattr(series, 'name', None)
241+
self.name = series.name
221242

222243
self.dummy_arr, self.dummy_index = self._check_dummy(dummy)
223244

@@ -265,20 +286,12 @@ cdef class SeriesBinGrouper(_BaseGrouper):
265286
cached_typ, cached_ityp = self._update_cached_objs(
266287
cached_typ, cached_ityp, islider, vslider)
267288

268-
cached_ityp._engine.clear_mapping()
269-
res = self.f(cached_typ)
270-
res = _extract_result(res)
271-
if not initialized:
272-
# On the first pass, we check the output shape to see
273-
# if this looks like a reduction.
274-
initialized = 1
275-
_check_result_array(res, len(self.dummy_arr))
289+
res, initialized = self._apply_to_group(cached_typ, cached_ityp,
290+
islider, vslider,
291+
group_size, initialized)
276292

277293
result[i] = res
278294

279-
islider.advance(group_size)
280-
vslider.advance(group_size)
281-
282295
finally:
283296
# so we don't free the wrong memory
284297
islider.reset()
@@ -322,7 +335,7 @@ cdef class SeriesGrouper(_BaseGrouper):
322335
self.typ = series._constructor
323336
self.ityp = series.index._constructor
324337
self.index = series.index.values
325-
self.name = getattr(series, 'name', None)
338+
self.name = series.name
326339

327340
self.dummy_arr, self.dummy_index = self._check_dummy(dummy)
328341
self.ngroups = ngroups
@@ -367,20 +380,12 @@ cdef class SeriesGrouper(_BaseGrouper):
367380
cached_typ, cached_ityp = self._update_cached_objs(
368381
cached_typ, cached_ityp, islider, vslider)
369382

370-
cached_ityp._engine.clear_mapping()
371-
res = self.f(cached_typ)
372-
res = _extract_result(res)
373-
if not initialized:
374-
# On the first pass, we check the output shape to see
375-
# if this looks like a reduction.
376-
initialized = 1
377-
_check_result_array(res, len(self.dummy_arr))
383+
res, initialized = self._apply_to_group(cached_typ, cached_ityp,
384+
islider, vslider,
385+
group_size, initialized)
378386

379387
result[lab] = res
380388
counts[lab] = group_size
381-
islider.advance(group_size)
382-
vslider.advance(group_size)
383-
384389
group_size = 0
385390

386391
finally:

0 commit comments

Comments
 (0)