|
1 |
| -from numpy cimport ndarray |
| 1 | +from numpy cimport ndarray, NPY_C_CONTIGUOUS, NPY_F_CONTIGUOUS |
2 | 2 | cimport numpy as cnp
|
| 3 | +cnp.import_array() |
| 4 | + |
3 | 5 | cimport cpython
|
| 6 | +from cpython cimport PyTypeObject |
| 7 | + |
| 8 | +cdef extern from "Python.h": |
| 9 | + # Note: importing extern-style allows us to declare these as nogil |
| 10 | + # functions, whereas `from cpython cimport` does not. |
| 11 | + bint PyUnicode_Check(object obj) nogil |
| 12 | + bint PyString_Check(object obj) nogil |
| 13 | + bint PyBool_Check(object obj) nogil |
| 14 | + bint PyFloat_Check(object obj) nogil |
| 15 | + bint PyComplex_Check(object obj) nogil |
| 16 | + bint PyObject_TypeCheck(object obj, PyTypeObject* type) nogil |
| 17 | + |
| 18 | + |
| 19 | +cdef extern from "numpy/arrayobject.h": |
| 20 | + PyTypeObject PyFloatingArrType_Type |
| 21 | + |
| 22 | +cdef extern from "numpy/ndarrayobject.h": |
| 23 | + PyTypeObject PyTimedeltaArrType_Type |
| 24 | + PyTypeObject PyDatetimeArrType_Type |
| 25 | + PyTypeObject PyComplexFloatingArrType_Type |
| 26 | + PyTypeObject PyBoolArrType_Type |
| 27 | + |
| 28 | + bint PyArray_IsIntegerScalar(obj) nogil |
| 29 | + bint PyArray_Check(obj) nogil |
| 30 | + |
| 31 | +# -------------------------------------------------------------------- |
| 32 | +# Type Checking |
| 33 | + |
| 34 | +cdef inline bint is_string_object(object obj) nogil: |
| 35 | + return PyString_Check(obj) or PyUnicode_Check(obj) |
| 36 | + |
| 37 | + |
| 38 | +cdef inline bint is_integer_object(object obj) nogil: |
| 39 | + return not PyBool_Check(obj) and PyArray_IsIntegerScalar(obj) |
| 40 | + |
| 41 | + |
| 42 | +cdef inline bint is_float_object(object obj) nogil: |
| 43 | + return (PyFloat_Check(obj) or |
| 44 | + (PyObject_TypeCheck(obj, &PyFloatingArrType_Type))) |
| 45 | + |
4 | 46 |
|
| 47 | +cdef inline bint is_complex_object(object obj) nogil: |
| 48 | + return (PyComplex_Check(obj) or |
| 49 | + PyObject_TypeCheck(obj, &PyComplexFloatingArrType_Type)) |
| 50 | + |
| 51 | + |
| 52 | +cdef inline bint is_bool_object(object obj) nogil: |
| 53 | + return (PyBool_Check(obj) or |
| 54 | + PyObject_TypeCheck(obj, &PyBoolArrType_Type)) |
| 55 | + |
| 56 | + |
| 57 | +cdef inline bint is_timedelta64_object(object obj) nogil: |
| 58 | + return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type) |
| 59 | + |
| 60 | + |
| 61 | +cdef inline bint is_datetime64_object(object obj) nogil: |
| 62 | + return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type) |
| 63 | + |
| 64 | +# -------------------------------------------------------------------- |
5 | 65 |
|
6 | 66 | cdef extern from "numpy_helper.h":
|
7 | 67 | void set_array_not_contiguous(ndarray ao)
|
8 | 68 |
|
9 |
| - int is_integer_object(object) |
10 |
| - int is_float_object(object) |
11 |
| - int is_complex_object(object) |
12 |
| - int is_bool_object(object) |
13 |
| - int is_string_object(object) |
14 |
| - int is_datetime64_object(object) |
15 |
| - int is_timedelta64_object(object) |
16 | 69 | int assign_value_1d(ndarray, Py_ssize_t, object) except -1
|
17 | 70 | cnp.int64_t get_nat()
|
18 | 71 | object get_value_1d(ndarray, Py_ssize_t)
|
19 | 72 | char *get_c_string(object) except NULL
|
20 | 73 | object char_to_string(char*)
|
21 |
| - object unbox_if_zerodim(object arr) |
22 | 74 |
|
23 | 75 | ctypedef fused numeric:
|
24 | 76 | cnp.int8_t
|
@@ -112,3 +164,22 @@ cdef inline bint _checknan(object val):
|
112 | 164 |
|
113 | 165 | cdef inline bint is_period_object(object val):
|
114 | 166 | return getattr(val, '_typ', '_typ') == 'period'
|
| 167 | + |
| 168 | + |
| 169 | +cdef inline object unbox_if_zerodim(object arr): |
| 170 | + """ |
| 171 | + If arr is zerodim array, return a proper array scalar (e.g. np.int64). |
| 172 | + Otherwise, return arr as is. |
| 173 | +
|
| 174 | + Parameters |
| 175 | + ---------- |
| 176 | + arr : object |
| 177 | +
|
| 178 | + Returns |
| 179 | + ------- |
| 180 | + result : object |
| 181 | + """ |
| 182 | + if cnp.PyArray_IsZeroDim(arr): |
| 183 | + return cnp.PyArray_ToScalar(cnp.PyArray_DATA(arr), arr) |
| 184 | + else: |
| 185 | + return arr |
0 commit comments