Skip to content

Commit

Permalink
Assorted _libs cleanups (pandas-dev#19887)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Feb 28, 2018
1 parent 7561eb8 commit 61211a8
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 264 deletions.
3 changes: 0 additions & 3 deletions pandas/_libs/lib.pxd

This file was deleted.

187 changes: 13 additions & 174 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ from cpython cimport (Py_INCREF, PyTuple_SET_ITEM,
PyBytes_Check,
PyUnicode_Check,
PyTuple_New,
Py_EQ,
PyObject_RichCompareBool)

cimport cpython


from cpython.datetime cimport (PyDateTime_Check, PyDate_Check,
PyTime_Check, PyDelta_Check,
PyDateTime_IMPORT)
Expand Down Expand Up @@ -105,6 +103,14 @@ def item_from_zerodim(object val):
"""
If the value is a zerodim array, return the item it contains.
Parameters
----------
val : object
Returns
-------
result : object
Examples
--------
>>> item_from_zerodim(1)
Expand All @@ -117,7 +123,9 @@ def item_from_zerodim(object val):
array([1])
"""
return util.unbox_if_zerodim(val)
if cnp.PyArray_IsZeroDim(val):
return cnp.PyArray_ToScalar(cnp.PyArray_DATA(val), val)
return val


@cython.wraparound(False)
Expand Down Expand Up @@ -405,72 +413,6 @@ def maybe_booleans_to_slice(ndarray[uint8_t] mask):
return slice(start, end)


@cython.wraparound(False)
@cython.boundscheck(False)
def scalar_compare(ndarray[object] values, object val, object op):
cdef:
Py_ssize_t i, n = len(values)
ndarray[uint8_t, cast=True] result
bint isnull_val
int flag
object x

if op is operator.lt:
flag = cpython.Py_LT
elif op is operator.le:
flag = cpython.Py_LE
elif op is operator.gt:
flag = cpython.Py_GT
elif op is operator.ge:
flag = cpython.Py_GE
elif op is operator.eq:
flag = cpython.Py_EQ
elif op is operator.ne:
flag = cpython.Py_NE
else:
raise ValueError('Unrecognized operator')

result = np.empty(n, dtype=bool).view(np.uint8)
isnull_val = checknull(val)

if flag == cpython.Py_NE:
for i in range(n):
x = values[i]
if checknull(x):
result[i] = True
elif isnull_val:
result[i] = True
else:
try:
result[i] = PyObject_RichCompareBool(x, val, flag)
except (TypeError):
result[i] = True
elif flag == cpython.Py_EQ:
for i in range(n):
x = values[i]
if checknull(x):
result[i] = False
elif isnull_val:
result[i] = False
else:
try:
result[i] = PyObject_RichCompareBool(x, val, flag)
except (TypeError):
result[i] = False

else:
for i in range(n):
x = values[i]
if checknull(x):
result[i] = False
elif isnull_val:
result[i] = False
else:
result[i] = PyObject_RichCompareBool(x, val, flag)

return result.view(bool)


@cython.wraparound(False)
@cython.boundscheck(False)
cpdef bint array_equivalent_object(object[:] left, object[:] right):
Expand All @@ -486,115 +428,12 @@ cpdef bint array_equivalent_object(object[:] left, object[:] right):

# we are either not equal or both nan
# I think None == None will be true here
if not (PyObject_RichCompareBool(x, y, cpython.Py_EQ) or
if not (PyObject_RichCompareBool(x, y, Py_EQ) or
_checknull(x) and _checknull(y)):
return False
return True


@cython.wraparound(False)
@cython.boundscheck(False)
def vec_compare(ndarray[object] left, ndarray[object] right, object op):
cdef:
Py_ssize_t i, n = len(left)
ndarray[uint8_t, cast=True] result
int flag

if n != len(right):
raise ValueError('Arrays were different lengths: %d vs %d'
% (n, len(right)))

if op is operator.lt:
flag = cpython.Py_LT
elif op is operator.le:
flag = cpython.Py_LE
elif op is operator.gt:
flag = cpython.Py_GT
elif op is operator.ge:
flag = cpython.Py_GE
elif op is operator.eq:
flag = cpython.Py_EQ
elif op is operator.ne:
flag = cpython.Py_NE
else:
raise ValueError('Unrecognized operator')

result = np.empty(n, dtype=bool).view(np.uint8)

if flag == cpython.Py_NE:
for i in range(n):
x = left[i]
y = right[i]

if checknull(x) or checknull(y):
result[i] = True
else:
result[i] = PyObject_RichCompareBool(x, y, flag)
else:
for i in range(n):
x = left[i]
y = right[i]

if checknull(x) or checknull(y):
result[i] = False
else:
result[i] = PyObject_RichCompareBool(x, y, flag)

return result.view(bool)


@cython.wraparound(False)
@cython.boundscheck(False)
def scalar_binop(ndarray[object] values, object val, object op):
cdef:
Py_ssize_t i, n = len(values)
ndarray[object] result
object x

result = np.empty(n, dtype=object)
if _checknull(val):
result.fill(val)
return result

for i in range(n):
x = values[i]
if _checknull(x):
result[i] = x
else:
result[i] = op(x, val)

return maybe_convert_bool(result)


@cython.wraparound(False)
@cython.boundscheck(False)
def vec_binop(ndarray[object] left, ndarray[object] right, object op):
cdef:
Py_ssize_t i, n = len(left)
ndarray[object] result

if n != len(right):
raise ValueError('Arrays were different lengths: %d vs %d'
% (n, len(right)))

result = np.empty(n, dtype=object)

for i in range(n):
x = left[i]
y = right[i]
try:
result[i] = op(x, y)
except TypeError:
if _checknull(x):
result[i] = x
elif _checknull(y):
result[i] = y
else:
raise

return maybe_convert_bool(result)


def astype_intsafe(ndarray[object] arr, new_dtype):
cdef:
Py_ssize_t i, n = len(arr)
Expand Down
Loading

0 comments on commit 61211a8

Please sign in to comment.