Skip to content

CLN: clean libreduction #37036

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 6 commits into from
Oct 10, 2020
Merged
Changes from 5 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
62 changes: 22 additions & 40 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from copy import copy

from cython import Py_ssize_t

from libc.stdlib cimport free, malloc

import numpy as np
Expand All @@ -11,14 +9,14 @@ from numpy cimport int64_t, ndarray

cnp.import_array()

from pandas._libs cimport util
from pandas._libs.util cimport is_array, set_array_not_contiguous

from pandas._libs.lib import is_scalar, maybe_convert_objects


cpdef check_result_array(object obj, Py_ssize_t cnt):

if (util.is_array(obj) or
if (is_array(obj) or
(isinstance(obj, list) and len(obj) == cnt) or
getattr(obj, 'shape', None) == (cnt,)):
raise ValueError('Must produce aggregated value')
Expand All @@ -33,7 +31,7 @@ cdef class _BaseGrouper:
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:
if is_array(values) and not values.flags.contiguous:
# e.g. Categorical has no `flags` attribute
values = values.copy()
index = dummy.index.values
Expand Down Expand Up @@ -106,7 +104,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
self.f = f

values = series.values
if util.is_array(values) and not values.flags.c_contiguous:
if is_array(values) and not values.flags.c_contiguous:
# e.g. Categorical has no `flags` attribute
values = values.copy('C')
self.arr = values
Expand Down Expand Up @@ -204,7 +202,7 @@ cdef class SeriesGrouper(_BaseGrouper):
self.f = f

values = series.values
if util.is_array(values) and not values.flags.c_contiguous:
if is_array(values) and not values.flags.c_contiguous:
# e.g. Categorical has no `flags` attribute
values = values.copy('C')
self.arr = values
Expand Down Expand Up @@ -288,9 +286,9 @@ cpdef inline extract_result(object res, bint squeeze=True):
res = res._values
if squeeze and res.ndim == 1 and len(res) == 1:
res = res[0]
if hasattr(res, 'values') and util.is_array(res.values):
if hasattr(res, 'values') and is_array(res.values):
res = res.values
if util.is_array(res):
if is_array(res):
if res.ndim == 0:
res = res.item()
elif squeeze and res.ndim == 1 and len(res) == 1:
Expand All @@ -304,7 +302,7 @@ cdef class Slider:
"""
cdef:
ndarray values, buf
Py_ssize_t stride, orig_len, orig_stride
Py_ssize_t stride
char *orig_data

def __init__(self, ndarray values, ndarray buf):
Expand All @@ -316,11 +314,9 @@ cdef class Slider:

self.values = values
self.buf = buf
self.stride = values.strides[0]

self.stride = values.strides[0]
self.orig_data = self.buf.data
self.orig_len = self.buf.shape[0]
self.orig_stride = self.buf.strides[0]

self.buf.data = self.values.data
self.buf.strides[0] = self.stride
Expand All @@ -333,10 +329,8 @@ cdef class Slider:
self.buf.shape[0] = end - start

cdef reset(self):

self.buf.shape[0] = self.orig_len
self.buf.data = self.orig_data
self.buf.strides[0] = self.orig_stride
self.buf.shape[0] = 0


class InvalidApply(Exception):
Expand Down Expand Up @@ -408,29 +402,24 @@ cdef class BlockSlider:
"""
Only capable of sliding on axis=0
"""

cdef public:
object frame, dummy, index
int nblocks
Slider idx_slider
list blocks

cdef:
object frame, dummy, index, block
list blocks
ndarray values
Slider idx_slider
char **base_ptrs
int nblocks
Py_ssize_t i

def __init__(self, object frame):
cdef:
Py_ssize_t i
object b

self.frame = frame
self.dummy = frame[:0]
self.index = self.dummy.index

self.blocks = [b.values for b in self.dummy._mgr.blocks]
self.blocks = [block.values for block in self.dummy._mgr.blocks]

for x in self.blocks:
util.set_array_not_contiguous(x)
for values in self.blocks:
set_array_not_contiguous(values)

self.nblocks = len(self.blocks)
# See the comment in indexes/base.py about _index_data.
Expand All @@ -439,18 +428,15 @@ cdef class BlockSlider:
self.idx_slider = Slider(
self.frame.index._index_data, self.dummy.index._index_data)

self.base_ptrs = <char**>malloc(sizeof(char*) * len(self.blocks))
self.base_ptrs = <char**>malloc(sizeof(char*) * self.nblocks)
for i, block in enumerate(self.blocks):
self.base_ptrs[i] = (<ndarray>block).data

def __dealloc__(self):
free(self.base_ptrs)

cdef move(self, int start, int end):
cdef:
ndarray arr
Py_ssize_t i

cdef ndarray arr
# move blocks
for i in range(self.nblocks):
arr = self.blocks[i]
Expand All @@ -467,11 +453,7 @@ cdef class BlockSlider:
self.index._cache.clear() # e.g. inferred_freq must go

cdef reset(self):
cdef:
ndarray arr
Py_ssize_t i

# reset blocks
cdef ndarray arr
for i in range(self.nblocks):
arr = self.blocks[i]

Expand Down