Skip to content

REF: simplify advance/move/set_length in libreduction #34982

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 3 commits into from
Jun 25, 2020
Merged
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
40 changes: 17 additions & 23 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ cdef class _BaseGrouper:

cdef inline object _apply_to_group(self,
object cached_typ, object cached_ityp,
Slider islider, Slider vslider,
Py_ssize_t group_size, bint initialized):
bint initialized):
"""
Call self.f on our new group, then update to the next group.
"""
Expand All @@ -222,9 +221,6 @@ cdef class _BaseGrouper:
initialized = True
_check_result_array(res, len(self.dummy_arr))

islider.advance(group_size)
vslider.advance(group_size)

return res, initialized


Expand Down Expand Up @@ -269,7 +265,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
cdef:
ndarray arr, result
ndarray[int64_t] counts
Py_ssize_t i, n, group_size
Py_ssize_t i, n, group_size, start, end
object res
bint initialized = 0
Slider vslider, islider
Expand All @@ -293,19 +289,21 @@ cdef class SeriesBinGrouper(_BaseGrouper):

result = np.empty(self.ngroups, dtype='O')

start = 0
try:
for i in range(self.ngroups):
group_size = counts[i]
end = start + group_size

islider.set_length(group_size)
vslider.set_length(group_size)
islider.move(start, end)
vslider.move(start, end)

cached_typ, cached_ityp = self._update_cached_objs(
cached_typ, cached_ityp, islider, vslider)

res, initialized = self._apply_to_group(cached_typ, cached_ityp,
islider, vslider,
group_size, initialized)
initialized)
start += group_size

result[i] = res

Expand Down Expand Up @@ -361,7 +359,7 @@ cdef class SeriesGrouper(_BaseGrouper):
# Define result to avoid UnboundLocalError
ndarray arr, result = None
ndarray[int64_t] labels, counts
Py_ssize_t i, n, group_size, lab
Py_ssize_t i, n, group_size, lab, start, end
object res
bint initialized = 0
Slider vslider, islider
Expand All @@ -377,6 +375,7 @@ cdef class SeriesGrouper(_BaseGrouper):

result = np.empty(self.ngroups, dtype='O')

start = 0
try:
for i in range(n):
group_size += 1
Expand All @@ -385,20 +384,21 @@ cdef class SeriesGrouper(_BaseGrouper):

if i == n - 1 or lab != labels[i + 1]:
if lab == -1:
islider.advance(group_size)
vslider.advance(group_size)
start += group_size
group_size = 0
continue

islider.set_length(group_size)
vslider.set_length(group_size)
end = start + group_size
islider.move(start, end)
vslider.move(start, end)

cached_typ, cached_ityp = self._update_cached_objs(
cached_typ, cached_ityp, islider, vslider)

res, initialized = self._apply_to_group(cached_typ, cached_ityp,
islider, vslider,
group_size, initialized)
initialized)

start += group_size

result[lab] = res
counts[lab] = group_size
Expand Down Expand Up @@ -458,19 +458,13 @@ cdef class Slider:
self.buf.data = self.values.data
self.buf.strides[0] = self.stride

cdef advance(self, Py_ssize_t k):
self.buf.data = <char*>self.buf.data + self.stride * k

cdef move(self, int start, int end):
"""
For slicing
"""
self.buf.data = self.values.data + self.stride * start
self.buf.shape[0] = end - start

cdef set_length(self, Py_ssize_t length):
self.buf.shape[0] = length

cdef reset(self):

self.buf.shape[0] = self.orig_len
Expand Down