Skip to content

Commit 61211a8

Browse files
jbrockmendeljreback
authored andcommitted
Assorted _libs cleanups (pandas-dev#19887)
1 parent 7561eb8 commit 61211a8

File tree

10 files changed

+330
-264
lines changed

10 files changed

+330
-264
lines changed

pandas/_libs/lib.pxd

-3
This file was deleted.

pandas/_libs/lib.pyx

+13-174
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ from cpython cimport (Py_INCREF, PyTuple_SET_ITEM,
2121
PyBytes_Check,
2222
PyUnicode_Check,
2323
PyTuple_New,
24+
Py_EQ,
2425
PyObject_RichCompareBool)
2526

26-
cimport cpython
27-
28-
2927
from cpython.datetime cimport (PyDateTime_Check, PyDate_Check,
3028
PyTime_Check, PyDelta_Check,
3129
PyDateTime_IMPORT)
@@ -105,6 +103,14 @@ def item_from_zerodim(object val):
105103
"""
106104
If the value is a zerodim array, return the item it contains.
107105
106+
Parameters
107+
----------
108+
val : object
109+
110+
Returns
111+
-------
112+
result : object
113+
108114
Examples
109115
--------
110116
>>> item_from_zerodim(1)
@@ -117,7 +123,9 @@ def item_from_zerodim(object val):
117123
array([1])
118124
119125
"""
120-
return util.unbox_if_zerodim(val)
126+
if cnp.PyArray_IsZeroDim(val):
127+
return cnp.PyArray_ToScalar(cnp.PyArray_DATA(val), val)
128+
return val
121129

122130

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

407415

408-
@cython.wraparound(False)
409-
@cython.boundscheck(False)
410-
def scalar_compare(ndarray[object] values, object val, object op):
411-
cdef:
412-
Py_ssize_t i, n = len(values)
413-
ndarray[uint8_t, cast=True] result
414-
bint isnull_val
415-
int flag
416-
object x
417-
418-
if op is operator.lt:
419-
flag = cpython.Py_LT
420-
elif op is operator.le:
421-
flag = cpython.Py_LE
422-
elif op is operator.gt:
423-
flag = cpython.Py_GT
424-
elif op is operator.ge:
425-
flag = cpython.Py_GE
426-
elif op is operator.eq:
427-
flag = cpython.Py_EQ
428-
elif op is operator.ne:
429-
flag = cpython.Py_NE
430-
else:
431-
raise ValueError('Unrecognized operator')
432-
433-
result = np.empty(n, dtype=bool).view(np.uint8)
434-
isnull_val = checknull(val)
435-
436-
if flag == cpython.Py_NE:
437-
for i in range(n):
438-
x = values[i]
439-
if checknull(x):
440-
result[i] = True
441-
elif isnull_val:
442-
result[i] = True
443-
else:
444-
try:
445-
result[i] = PyObject_RichCompareBool(x, val, flag)
446-
except (TypeError):
447-
result[i] = True
448-
elif flag == cpython.Py_EQ:
449-
for i in range(n):
450-
x = values[i]
451-
if checknull(x):
452-
result[i] = False
453-
elif isnull_val:
454-
result[i] = False
455-
else:
456-
try:
457-
result[i] = PyObject_RichCompareBool(x, val, flag)
458-
except (TypeError):
459-
result[i] = False
460-
461-
else:
462-
for i in range(n):
463-
x = values[i]
464-
if checknull(x):
465-
result[i] = False
466-
elif isnull_val:
467-
result[i] = False
468-
else:
469-
result[i] = PyObject_RichCompareBool(x, val, flag)
470-
471-
return result.view(bool)
472-
473-
474416
@cython.wraparound(False)
475417
@cython.boundscheck(False)
476418
cpdef bint array_equivalent_object(object[:] left, object[:] right):
@@ -486,115 +428,12 @@ cpdef bint array_equivalent_object(object[:] left, object[:] right):
486428

487429
# we are either not equal or both nan
488430
# I think None == None will be true here
489-
if not (PyObject_RichCompareBool(x, y, cpython.Py_EQ) or
431+
if not (PyObject_RichCompareBool(x, y, Py_EQ) or
490432
_checknull(x) and _checknull(y)):
491433
return False
492434
return True
493435

494436

495-
@cython.wraparound(False)
496-
@cython.boundscheck(False)
497-
def vec_compare(ndarray[object] left, ndarray[object] right, object op):
498-
cdef:
499-
Py_ssize_t i, n = len(left)
500-
ndarray[uint8_t, cast=True] result
501-
int flag
502-
503-
if n != len(right):
504-
raise ValueError('Arrays were different lengths: %d vs %d'
505-
% (n, len(right)))
506-
507-
if op is operator.lt:
508-
flag = cpython.Py_LT
509-
elif op is operator.le:
510-
flag = cpython.Py_LE
511-
elif op is operator.gt:
512-
flag = cpython.Py_GT
513-
elif op is operator.ge:
514-
flag = cpython.Py_GE
515-
elif op is operator.eq:
516-
flag = cpython.Py_EQ
517-
elif op is operator.ne:
518-
flag = cpython.Py_NE
519-
else:
520-
raise ValueError('Unrecognized operator')
521-
522-
result = np.empty(n, dtype=bool).view(np.uint8)
523-
524-
if flag == cpython.Py_NE:
525-
for i in range(n):
526-
x = left[i]
527-
y = right[i]
528-
529-
if checknull(x) or checknull(y):
530-
result[i] = True
531-
else:
532-
result[i] = PyObject_RichCompareBool(x, y, flag)
533-
else:
534-
for i in range(n):
535-
x = left[i]
536-
y = right[i]
537-
538-
if checknull(x) or checknull(y):
539-
result[i] = False
540-
else:
541-
result[i] = PyObject_RichCompareBool(x, y, flag)
542-
543-
return result.view(bool)
544-
545-
546-
@cython.wraparound(False)
547-
@cython.boundscheck(False)
548-
def scalar_binop(ndarray[object] values, object val, object op):
549-
cdef:
550-
Py_ssize_t i, n = len(values)
551-
ndarray[object] result
552-
object x
553-
554-
result = np.empty(n, dtype=object)
555-
if _checknull(val):
556-
result.fill(val)
557-
return result
558-
559-
for i in range(n):
560-
x = values[i]
561-
if _checknull(x):
562-
result[i] = x
563-
else:
564-
result[i] = op(x, val)
565-
566-
return maybe_convert_bool(result)
567-
568-
569-
@cython.wraparound(False)
570-
@cython.boundscheck(False)
571-
def vec_binop(ndarray[object] left, ndarray[object] right, object op):
572-
cdef:
573-
Py_ssize_t i, n = len(left)
574-
ndarray[object] result
575-
576-
if n != len(right):
577-
raise ValueError('Arrays were different lengths: %d vs %d'
578-
% (n, len(right)))
579-
580-
result = np.empty(n, dtype=object)
581-
582-
for i in range(n):
583-
x = left[i]
584-
y = right[i]
585-
try:
586-
result[i] = op(x, y)
587-
except TypeError:
588-
if _checknull(x):
589-
result[i] = x
590-
elif _checknull(y):
591-
result[i] = y
592-
else:
593-
raise
594-
595-
return maybe_convert_bool(result)
596-
597-
598437
def astype_intsafe(ndarray[object] arr, new_dtype):
599438
cdef:
600439
Py_ssize_t i, n = len(arr)

0 commit comments

Comments
 (0)