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

stronger typing in libreduction #29502

Merged
merged 5 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
30 changes: 12 additions & 18 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ cdef class Reducer:
"""
cdef:
Py_ssize_t increment, chunksize, nresults
object arr, dummy, f, labels, typ, ityp, index
object dummy, f, labels, typ, ityp, index
ndarray arr

def __init__(self, object arr, object f, axis=1, dummy=None, labels=None):
n, k = arr.shape
def __init__(self, ndarray arr, object f, axis=1, dummy=None, labels=None):
n, k = (<object>arr).shape

if axis == 0:
if not arr.flags.f_contiguous:
Expand Down Expand Up @@ -103,7 +104,7 @@ cdef class Reducer:
ndarray arr, result, chunk
Py_ssize_t i, incr
flatiter it
bint has_labels, has_ndarray_labels
bint has_labels
object res, name, labels, index
object cached_typ = None

Expand All @@ -113,17 +114,13 @@ cdef class Reducer:
chunk.data = arr.data
labels = self.labels
has_labels = labels is not None
has_ndarray_labels = util.is_array(labels)
has_index = self.index is not None
incr = self.increment

try:
for i in range(self.nresults):

if has_ndarray_labels:
name = labels[i]
elif has_labels:
# labels is an ExtensionArray
if has_labels:
name = labels[i]
else:
name = None
Expand Down Expand Up @@ -188,11 +185,10 @@ cdef class SeriesBinGrouper:
Py_ssize_t nresults, ngroups

cdef public:
object arr, index, dummy_arr, dummy_index
ndarray arr, index, dummy_arr, dummy_index
object values, f, bins, typ, ityp, name

def __init__(self, object series, object f, object bins, object dummy):
n = len(series)

assert dummy is not None # always obj[:0]

Expand Down Expand Up @@ -314,12 +310,11 @@ cdef class SeriesGrouper:
Py_ssize_t nresults, ngroups

cdef public:
object arr, index, dummy_arr, dummy_index
ndarray arr, index, dummy_arr, dummy_index
object f, labels, values, typ, ityp, name

def __init__(self, object series, object f, object labels,
Py_ssize_t ngroups, object dummy):
n = len(series)

# in practice we always pass either obj[:0] or the
# safer obj._get_values(slice(None, 0))
Expand Down Expand Up @@ -460,14 +455,13 @@ cdef class Slider:
Py_ssize_t stride, orig_len, orig_stride
char *orig_data

def __init__(self, object values, object buf):
assert (values.ndim == 1)
def __init__(self, ndarray values, ndarray buf):
assert values.ndim == 1
assert values.dtype == buf.dtype

if util.is_array(values) and not values.flags.contiguous:
# e.g. Categorical has no `flags` attribute
if not values.flags.contiguous:
values = values.copy()

assert (values.dtype == buf.dtype)
self.values = values
self.buf = buf
self.stride = values.strides[0]
Expand Down