Skip to content

Commit 69f1bdc

Browse files
jbrockmendelharisbal
authored and
harisbal
committed
implement bits of numpy_helper in cython where possible (pandas-dev#19450)
1 parent 74cf2dd commit 69f1bdc

File tree

3 files changed

+81
-51
lines changed

3 files changed

+81
-51
lines changed

pandas/_libs/src/numpy_helper.h

-40
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,6 @@ The full license is in the LICENSE file, distributed with this software.
1818

1919
PANDAS_INLINE npy_int64 get_nat(void) { return NPY_MIN_INT64; }
2020

21-
PANDAS_INLINE int is_integer_object(PyObject* obj) {
22-
return (!PyBool_Check(obj)) && PyArray_IsIntegerScalar(obj);
23-
}
24-
25-
PANDAS_INLINE int is_float_object(PyObject* obj) {
26-
return (PyFloat_Check(obj) || PyArray_IsScalar(obj, Floating));
27-
}
28-
PANDAS_INLINE int is_complex_object(PyObject* obj) {
29-
return (PyComplex_Check(obj) || PyArray_IsScalar(obj, ComplexFloating));
30-
}
31-
32-
PANDAS_INLINE int is_bool_object(PyObject* obj) {
33-
return (PyBool_Check(obj) || PyArray_IsScalar(obj, Bool));
34-
}
35-
36-
PANDAS_INLINE int is_string_object(PyObject* obj) {
37-
return (PyString_Check(obj) || PyUnicode_Check(obj));
38-
}
39-
40-
PANDAS_INLINE int is_datetime64_object(PyObject* obj) {
41-
return PyArray_IsScalar(obj, Datetime);
42-
}
43-
44-
PANDAS_INLINE int is_timedelta64_object(PyObject* obj) {
45-
return PyArray_IsScalar(obj, Timedelta);
46-
}
47-
4821
PANDAS_INLINE int assign_value_1d(PyArrayObject* ap, Py_ssize_t _i,
4922
PyObject* v) {
5023
npy_intp i = (npy_intp)_i;
@@ -80,17 +53,4 @@ void set_array_not_contiguous(PyArrayObject* ao) {
8053
ao->flags &= ~(NPY_C_CONTIGUOUS | NPY_F_CONTIGUOUS);
8154
}
8255

83-
// If arr is zerodim array, return a proper array scalar (e.g. np.int64).
84-
// Otherwise, return arr as is.
85-
PANDAS_INLINE PyObject* unbox_if_zerodim(PyObject* arr) {
86-
if (PyArray_IsZeroDim(arr)) {
87-
PyObject* ret;
88-
ret = PyArray_ToScalar(PyArray_DATA(arr), arr);
89-
return ret;
90-
} else {
91-
Py_INCREF(arr);
92-
return arr;
93-
}
94-
}
95-
9656
#endif // PANDAS__LIBS_SRC_NUMPY_HELPER_H_

pandas/_libs/src/util.pxd

+80-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,76 @@
1-
from numpy cimport ndarray
1+
from numpy cimport ndarray, NPY_C_CONTIGUOUS, NPY_F_CONTIGUOUS
22
cimport numpy as cnp
3+
cnp.import_array()
4+
35
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+
446

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+
# --------------------------------------------------------------------
565

666
cdef extern from "numpy_helper.h":
767
void set_array_not_contiguous(ndarray ao)
868

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)
1669
int assign_value_1d(ndarray, Py_ssize_t, object) except -1
1770
cnp.int64_t get_nat()
1871
object get_value_1d(ndarray, Py_ssize_t)
1972
char *get_c_string(object) except NULL
2073
object char_to_string(char*)
21-
object unbox_if_zerodim(object arr)
2274

2375
ctypedef fused numeric:
2476
cnp.int8_t
@@ -112,3 +164,22 @@ cdef inline bint _checknan(object val):
112164

113165
cdef inline bint is_period_object(object val):
114166
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

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,7 @@ def pxd(name):
687687
ext.sources[0] = root + suffix
688688

689689
ujson_ext = Extension('pandas._libs.json',
690-
depends=['pandas/_libs/src/ujson/lib/ultrajson.h',
691-
'pandas/_libs/src/numpy_helper.h'],
690+
depends=['pandas/_libs/src/ujson/lib/ultrajson.h'],
692691
sources=(['pandas/_libs/src/ujson/python/ujson.c',
693692
'pandas/_libs/src/ujson/python/objToJSON.c',
694693
'pandas/_libs/src/ujson/python/JSONtoObj.c',

0 commit comments

Comments
 (0)