diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 4965469d05276..293f067810f27 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -319,15 +319,14 @@ cdef class IndexEngine: # form the set of the results (like ismember) members = np.empty(n, dtype=np.uint8) for i in range(n): - val = util.get_value_1d(values, i) + val = values[i] if val in stargets: if val not in d: d[val] = [] d[val].append(i) for i in range(n_t): - - val = util.get_value_1d(targets, i) + val = targets[i] # found if val in d: diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index e05905ab63624..654e7eaf92ff0 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1153,7 +1153,7 @@ def infer_dtype(object value, bint skipna=False): # try to use a valid value for i in range(n): - val = util.get_value_1d(values, i) + val = values[i] # do not use is_nul_datetimelike to keep # np.datetime64('nat') and np.timedelta64('nat') @@ -1240,7 +1240,7 @@ def infer_dtype(object value, bint skipna=False): return 'interval' for i in range(n): - val = util.get_value_1d(values, i) + val = values[i] if (util.is_integer_object(val) and not util.is_timedelta64_object(val) and not util.is_datetime64_object(val)): @@ -2255,7 +2255,7 @@ def fast_multiget(dict mapping, ndarray keys, default=np.nan): keys = getattr(keys, 'values', keys) for i in range(n): - val = util.get_value_1d(keys, i) + val = keys[i] if val in mapping: output[i] = mapping[val] else: diff --git a/pandas/_libs/src/numpy_helper.h b/pandas/_libs/src/numpy_helper.h index d44334906901a..d9d0fb74da73c 100644 --- a/pandas/_libs/src/numpy_helper.h +++ b/pandas/_libs/src/numpy_helper.h @@ -28,20 +28,4 @@ PANDAS_INLINE PyObject* get_value_1d(PyArrayObject* ap, Py_ssize_t i) { return PyArray_Scalar(item, PyArray_DESCR(ap), (PyObject*)ap); } -// returns ASCII or UTF8 (py3) view on python str -// python object owns memory, should not be freed -PANDAS_INLINE const char* get_c_string(PyObject* obj) { -#if PY_VERSION_HEX >= 0x03000000 - return PyUnicode_AsUTF8(obj); -#else - return PyString_AsString(obj); -#endif -} - -void set_array_not_contiguous(PyArrayObject* ao) { - // Numpy>=1.8-compliant equivalent to: - // ao->flags &= ~(NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS); - PyArray_CLEARFLAGS(ao, (NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS)); -} - #endif // PANDAS__LIBS_SRC_NUMPY_HELPER_H_ diff --git a/pandas/_libs/util.pxd b/pandas/_libs/util.pxd index 31843a755e7b1..25d20c930cf08 100644 --- a/pandas/_libs/util.pxd +++ b/pandas/_libs/util.pxd @@ -5,13 +5,34 @@ from cython cimport Py_ssize_t cimport numpy as cnp from numpy cimport ndarray +cdef extern from "numpy/ndarraytypes.h": + void PyArray_CLEARFLAGS(ndarray arr, int flags) nogil + + +cdef extern from "numpy/arrayobject.h": + enum: + NPY_ARRAY_C_CONTIGUOUS + NPY_ARRAY_F_CONTIGUOUS + + +cdef extern from *: + """ + // returns ASCII or UTF8 (py3) view on python str + // python object owns memory, should not be freed + static const char* get_c_string(PyObject* obj) { + #if PY_VERSION_HEX >= 0x03000000 + return PyUnicode_AsUTF8(obj); + #else + return PyString_AsString(obj); + #endif + } + """ + const char *get_c_string(object) except NULL -cdef extern from "src/numpy_helper.h": - void set_array_not_contiguous(ndarray ao) +cdef extern from "src/numpy_helper.h": int assign_value_1d(ndarray, Py_ssize_t, object) except -1 object get_value_1d(ndarray, Py_ssize_t) - const char *get_c_string(object) except NULL cdef extern from "src/headers/stdint.h": @@ -44,6 +65,13 @@ ctypedef fused numeric: cnp.float64_t +cdef inline void set_array_not_contiguous(ndarray ao) nogil: + # Numpy>=1.8-compliant equivalent to: + # ao->flags &= ~(NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS); + PyArray_CLEARFLAGS(ao, + (NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS)) + + cdef inline object get_value_at(ndarray arr, object loc): cdef: Py_ssize_t i, sz