Skip to content
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

REF: de-duplicate _check_dummy #29520

Merged
merged 2 commits into from
Nov 11, 2019
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
55 changes: 21 additions & 34 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,26 @@ cdef class Reducer:
return result


cdef class SeriesBinGrouper:
cdef class _BaseGrouper:
cdef _check_dummy(self, dummy):
# both values and index must be an ndarray!

values = dummy.values
# GH 23683: datetimetz types are equivalent to datetime types here
if (dummy.dtype != self.arr.dtype
and values.dtype != self.arr.dtype):
raise ValueError('Dummy array must be same dtype')
if util.is_array(values) and not values.flags.contiguous:
# e.g. Categorical has no `flags` attribute
values = values.copy()
index = dummy.index.values
if not index.flags.contiguous:
index = index.copy()

return values, index


cdef class SeriesBinGrouper(_BaseGrouper):
"""
Performs grouping operation according to bin edges, rather than labels
"""
Expand Down Expand Up @@ -217,21 +236,6 @@ cdef class SeriesBinGrouper:
else:
self.ngroups = len(bins) + 1

cdef _check_dummy(self, dummy):
# both values and index must be an ndarray!

values = dummy.values
if values.dtype != self.arr.dtype:
raise ValueError('Dummy array must be same dtype')
if util.is_array(values) and not values.flags.contiguous:
# e.g. Categorical has no `flags` attribute
values = values.copy()
index = dummy.index.values
if not index.flags.contiguous:
index = index.copy()

return values, index

def get_result(self):
cdef:
ndarray arr, result
Expand Down Expand Up @@ -305,7 +309,7 @@ cdef class SeriesBinGrouper:
return result, counts


cdef class SeriesGrouper:
cdef class SeriesGrouper(_BaseGrouper):
"""
Performs generic grouping operation while avoiding ndarray construction
overhead
Expand Down Expand Up @@ -341,23 +345,6 @@ cdef class SeriesGrouper:
self.dummy_arr, self.dummy_index = self._check_dummy(dummy)
self.ngroups = ngroups

cdef _check_dummy(self, dummy):
# both values and index must be an ndarray!

values = dummy.values
# GH 23683: datetimetz types are equivalent to datetime types here
if (dummy.dtype != self.arr.dtype
and values.dtype != self.arr.dtype):
raise ValueError('Dummy array must be same dtype')
if util.is_array(values) and not values.flags.contiguous:
# e.g. Categorical has no `flags` attribute
values = values.copy()
index = dummy.index.values
if not index.flags.contiguous:
index = index.copy()

return values, index

def get_result(self):
cdef:
# Define result to avoid UnboundLocalError
Expand Down