Skip to content

REF: simplify libreduction #41262

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 2 commits into from
May 2, 2021
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
44 changes: 24 additions & 20 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ cdef class _BaseGrouper:

return values, index

cdef _init_dummy_series_and_index(self, Slider islider, Slider vslider):
"""
Create Series and Index objects that we will alter in-place while iterating.
"""
cached_index = self.ityp(islider.buf, dtype=self.idtype)
cached_series = self.typ(
vslider.buf, dtype=vslider.buf.dtype, index=cached_index, name=self.name
)
return cached_index, cached_series

cdef inline _update_cached_objs(self, object cached_typ, object cached_ityp,
Slider islider, Slider vslider):
if cached_typ is None:
cached_ityp = self.ityp(islider.buf, dtype=self.idtype)
cached_typ = self.typ(
vslider.buf, dtype=vslider.buf.dtype, index=cached_ityp, name=self.name
)
else:
# See the comment in indexes/base.py about _index_data.
# We need this for EA-backed indexes that have a reference
# to a 1-d ndarray like datetime / timedelta / period.
object.__setattr__(cached_ityp, '_index_data', islider.buf)
cached_ityp._engine.clear_mapping()
cached_ityp._cache.clear() # e.g. inferred_freq must go
cached_typ._mgr.set_values(vslider.buf)
object.__setattr__(cached_typ, '_index', cached_ityp)
object.__setattr__(cached_typ, 'name', self.name)
return cached_typ, cached_ityp
# See the comment in indexes/base.py about _index_data.
# We need this for EA-backed indexes that have a reference
# to a 1-d ndarray like datetime / timedelta / period.
cached_ityp._engine.clear_mapping()
cached_ityp._cache.clear() # e.g. inferred_freq must go
cached_typ._mgr.set_values(vslider.buf)

cdef inline object _apply_to_group(self,
object cached_typ, object cached_ityp,
Expand All @@ -81,8 +81,8 @@ cdef class _BaseGrouper:
cdef:
object res

cached_ityp._engine.clear_mapping()
cached_ityp._cache.clear() # e.g. inferred_freq must go
# NB: we assume that _update_cached_objs has already cleared cleared
# the cache and engine mapping
res = self.f(cached_typ)
res = extract_result(res)
if not initialized:
Expand Down Expand Up @@ -160,6 +160,8 @@ cdef class SeriesBinGrouper(_BaseGrouper):

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

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

start = 0
try:
for i in range(self.ngroups):
Expand All @@ -169,7 +171,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
islider.move(start, end)
vslider.move(start, end)

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

res, initialized = self._apply_to_group(cached_typ, cached_ityp,
Expand Down Expand Up @@ -246,6 +248,8 @@ cdef class SeriesGrouper(_BaseGrouper):

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

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

start = 0
try:
for i in range(n):
Expand All @@ -263,7 +267,7 @@ cdef class SeriesGrouper(_BaseGrouper):
islider.move(start, end)
vslider.move(start, end)

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

res, initialized = self._apply_to_group(cached_typ, cached_ityp,
Expand Down