Skip to content

Commit f07a790

Browse files
jbrockmendeljreback
authored andcommitted
Make more of numpy_helper unnecessary (#22344)
1 parent 7b80d4d commit f07a790

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

pandas/_libs/index.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,14 @@ cdef class IndexEngine:
319319
# form the set of the results (like ismember)
320320
members = np.empty(n, dtype=np.uint8)
321321
for i in range(n):
322-
val = util.get_value_1d(values, i)
322+
val = values[i]
323323
if val in stargets:
324324
if val not in d:
325325
d[val] = []
326326
d[val].append(i)
327327

328328
for i in range(n_t):
329-
330-
val = util.get_value_1d(targets, i)
329+
val = targets[i]
331330

332331
# found
333332
if val in d:

pandas/_libs/lib.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ def infer_dtype(object value, bint skipna=False):
11531153

11541154
# try to use a valid value
11551155
for i in range(n):
1156-
val = util.get_value_1d(values, i)
1156+
val = values[i]
11571157

11581158
# do not use is_nul_datetimelike to keep
11591159
# np.datetime64('nat') and np.timedelta64('nat')
@@ -1240,7 +1240,7 @@ def infer_dtype(object value, bint skipna=False):
12401240
return 'interval'
12411241

12421242
for i in range(n):
1243-
val = util.get_value_1d(values, i)
1243+
val = values[i]
12441244
if (util.is_integer_object(val) and
12451245
not util.is_timedelta64_object(val) and
12461246
not util.is_datetime64_object(val)):
@@ -2255,7 +2255,7 @@ def fast_multiget(dict mapping, ndarray keys, default=np.nan):
22552255
keys = getattr(keys, 'values', keys)
22562256

22572257
for i in range(n):
2258-
val = util.get_value_1d(keys, i)
2258+
val = keys[i]
22592259
if val in mapping:
22602260
output[i] = mapping[val]
22612261
else:

pandas/_libs/src/numpy_helper.h

-16
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,4 @@ PANDAS_INLINE PyObject* get_value_1d(PyArrayObject* ap, Py_ssize_t i) {
2828
return PyArray_Scalar(item, PyArray_DESCR(ap), (PyObject*)ap);
2929
}
3030

31-
// returns ASCII or UTF8 (py3) view on python str
32-
// python object owns memory, should not be freed
33-
PANDAS_INLINE const char* get_c_string(PyObject* obj) {
34-
#if PY_VERSION_HEX >= 0x03000000
35-
return PyUnicode_AsUTF8(obj);
36-
#else
37-
return PyString_AsString(obj);
38-
#endif
39-
}
40-
41-
void set_array_not_contiguous(PyArrayObject* ao) {
42-
// Numpy>=1.8-compliant equivalent to:
43-
// ao->flags &= ~(NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS);
44-
PyArray_CLEARFLAGS(ao, (NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS));
45-
}
46-
4731
#endif // PANDAS__LIBS_SRC_NUMPY_HELPER_H_

pandas/_libs/util.pxd

+31-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,34 @@ from cython cimport Py_ssize_t
55
cimport numpy as cnp
66
from numpy cimport ndarray
77

8+
cdef extern from "numpy/ndarraytypes.h":
9+
void PyArray_CLEARFLAGS(ndarray arr, int flags) nogil
10+
11+
12+
cdef extern from "numpy/arrayobject.h":
13+
enum:
14+
NPY_ARRAY_C_CONTIGUOUS
15+
NPY_ARRAY_F_CONTIGUOUS
16+
17+
18+
cdef extern from *:
19+
"""
20+
// returns ASCII or UTF8 (py3) view on python str
21+
// python object owns memory, should not be freed
22+
static const char* get_c_string(PyObject* obj) {
23+
#if PY_VERSION_HEX >= 0x03000000
24+
return PyUnicode_AsUTF8(obj);
25+
#else
26+
return PyString_AsString(obj);
27+
#endif
28+
}
29+
"""
30+
const char *get_c_string(object) except NULL
831

9-
cdef extern from "src/numpy_helper.h":
10-
void set_array_not_contiguous(ndarray ao)
1132

33+
cdef extern from "src/numpy_helper.h":
1234
int assign_value_1d(ndarray, Py_ssize_t, object) except -1
1335
object get_value_1d(ndarray, Py_ssize_t)
14-
const char *get_c_string(object) except NULL
1536

1637

1738
cdef extern from "src/headers/stdint.h":
@@ -44,6 +65,13 @@ ctypedef fused numeric:
4465
cnp.float64_t
4566

4667

68+
cdef inline void set_array_not_contiguous(ndarray ao) nogil:
69+
# Numpy>=1.8-compliant equivalent to:
70+
# ao->flags &= ~(NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS);
71+
PyArray_CLEARFLAGS(ao,
72+
(NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS))
73+
74+
4775
cdef inline object get_value_at(ndarray arr, object loc):
4876
cdef:
4977
Py_ssize_t i, sz

0 commit comments

Comments
 (0)