Skip to content

REF: clearer names in libreduction #41274

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 1 commit into from
May 3, 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
38 changes: 21 additions & 17 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ cdef class _BaseGrouper:
)
return cached_index, cached_series

cdef inline _update_cached_objs(self, object cached_typ, object cached_ityp,
cdef inline _update_cached_objs(self, object cached_series, object cached_index,
Slider islider, Slider vslider):
# 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)
cached_index._engine.clear_mapping()
cached_index._cache.clear() # e.g. inferred_freq must go
cached_series._mgr.set_values(vslider.buf)

cdef inline object _apply_to_group(self,
object cached_typ, object cached_ityp,
object cached_series, object cached_index,
bint initialized):
"""
Call self.f on our new group, then update to the next group.
Expand All @@ -83,7 +83,7 @@ cdef class _BaseGrouper:

# NB: we assume that _update_cached_objs has already cleared cleared
# the cache and engine mapping
res = self.f(cached_typ)
res = self.f(cached_series)
res = extract_result(res)
if not initialized:
# On the first pass, we check the output shape to see
Expand Down Expand Up @@ -140,7 +140,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
object res
bint initialized = 0
Slider vslider, islider
object cached_typ = None, cached_ityp = None
object cached_series = None, cached_index = None

counts = np.zeros(self.ngroups, dtype=np.int64)

Expand All @@ -160,7 +160,9 @@ cdef class SeriesBinGrouper(_BaseGrouper):

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

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

start = 0
try:
Expand All @@ -172,9 +174,9 @@ cdef class SeriesBinGrouper(_BaseGrouper):
vslider.move(start, end)

self._update_cached_objs(
cached_typ, cached_ityp, islider, vslider)
cached_series, cached_index, islider, vslider)

res, initialized = self._apply_to_group(cached_typ, cached_ityp,
res, initialized = self._apply_to_group(cached_series, cached_index,
initialized)
start += group_size

Expand Down Expand Up @@ -236,7 +238,7 @@ cdef class SeriesGrouper(_BaseGrouper):
object res
bint initialized = 0
Slider vslider, islider
object cached_typ = None, cached_ityp = None
object cached_series = None, cached_index = None

labels = self.labels
counts = np.zeros(self.ngroups, dtype=np.int64)
Expand All @@ -248,7 +250,9 @@ cdef class SeriesGrouper(_BaseGrouper):

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

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

start = 0
try:
Expand All @@ -268,9 +272,9 @@ cdef class SeriesGrouper(_BaseGrouper):
vslider.move(start, end)

self._update_cached_objs(
cached_typ, cached_ityp, islider, vslider)
cached_series, cached_index, islider, vslider)

res, initialized = self._apply_to_group(cached_typ, cached_ityp,
res, initialized = self._apply_to_group(cached_series, cached_index,
initialized)

start += group_size
Expand All @@ -293,20 +297,20 @@ cdef class SeriesGrouper(_BaseGrouper):
return result, counts


cpdef inline extract_result(object res, bint squeeze=True):
cpdef inline extract_result(object res):
""" extract the result object, it might be a 0-dim ndarray
or a len-1 0-dim, or a scalar """
if hasattr(res, "_values"):
# Preserve EA
res = res._values
if squeeze and res.ndim == 1 and len(res) == 1:
if res.ndim == 1 and len(res) == 1:
res = res[0]
if hasattr(res, 'values') and is_array(res.values):
res = res.values
if is_array(res):
if res.ndim == 0:
res = res.item()
elif squeeze and res.ndim == 1 and len(res) == 1:
elif res.ndim == 1 and len(res) == 1:
res = res[0]
return res

Expand Down