1
1
# -*- coding: utf-8 -*-
2
- # cython: profile=False
3
2
import operator
4
3
5
4
from cpython cimport (PyFloat_Check, PyBool_Check,
@@ -21,7 +20,7 @@ from missing cimport checknull
21
20
22
21
@ cython.wraparound (False )
23
22
@ cython.boundscheck (False )
24
- def scalar_compare (ndarray[ object] values , object val , object op ):
23
+ def scalar_compare (object[: ] values , object val , object op ):
25
24
"""
26
25
Compare each element of `values` array with the scalar `val`, with
27
26
the comparison operation described by `op`.
@@ -73,7 +72,7 @@ def scalar_compare(ndarray[object] values, object val, object op):
73
72
else :
74
73
try :
75
74
result[i] = PyObject_RichCompareBool(x, val, flag)
76
- except ( TypeError ) :
75
+ except TypeError :
77
76
result[i] = True
78
77
elif flag == Py_EQ:
79
78
for i in range (n):
@@ -85,7 +84,7 @@ def scalar_compare(ndarray[object] values, object val, object op):
85
84
else :
86
85
try :
87
86
result[i] = PyObject_RichCompareBool(x, val, flag)
88
- except ( TypeError ) :
87
+ except TypeError :
89
88
result[i] = False
90
89
91
90
else :
@@ -103,7 +102,7 @@ def scalar_compare(ndarray[object] values, object val, object op):
103
102
104
103
@ cython.wraparound (False )
105
104
@ cython.boundscheck (False )
106
- def vec_compare (ndarray[ object] left , ndarray[ object] right , object op ):
105
+ def vec_compare (object[: ] left , object[: ] right , object op ):
107
106
"""
108
107
Compare the elements of `left` with the elements of `right` pointwise,
109
108
with the comparison operation described by `op`.
@@ -126,8 +125,8 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op):
126
125
int flag
127
126
128
127
if n != len (right):
129
- raise ValueError (' Arrays were different lengths: %d vs %d '
130
- % (n, len (right)))
128
+ raise ValueError (' Arrays were different lengths: {n} vs {nright} '
129
+ .format( n = n, nright = len (right)))
131
130
132
131
if op is operator.lt:
133
132
flag = Py_LT
@@ -170,7 +169,7 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op):
170
169
171
170
@ cython.wraparound (False )
172
171
@ cython.boundscheck (False )
173
- def scalar_binop (ndarray[ object] values , object val , object op ):
172
+ def scalar_binop (object[: ] values , object val , object op ):
174
173
"""
175
174
Apply the given binary operator `op` between each element of the array
176
175
`values` and the scalar `val`.
@@ -187,13 +186,13 @@ def scalar_binop(ndarray[object] values, object val, object op):
187
186
"""
188
187
cdef:
189
188
Py_ssize_t i, n = len (values)
190
- ndarray[ object ] result
189
+ object [: ] result
191
190
object x
192
191
193
192
result = np.empty(n, dtype = object )
194
193
if val is None or is_nan(val):
195
- result.fill( val)
196
- return result
194
+ result[:] = val
195
+ return result.base # `.base` to access underlying np.ndarray
197
196
198
197
for i in range (n):
199
198
x = values[i]
@@ -202,12 +201,12 @@ def scalar_binop(ndarray[object] values, object val, object op):
202
201
else :
203
202
result[i] = op(x, val)
204
203
205
- return maybe_convert_bool(result)
204
+ return maybe_convert_bool(result.base )
206
205
207
206
208
207
@ cython.wraparound (False )
209
208
@ cython.boundscheck (False )
210
- def vec_binop (ndarray[ object] left , ndarray[ object] right , object op ):
209
+ def vec_binop (object[: ] left , object[: ] right , object op ):
211
210
"""
212
211
Apply the given binary operator `op` pointwise to the elements of
213
212
arrays `left` and `right`.
@@ -224,11 +223,11 @@ def vec_binop(ndarray[object] left, ndarray[object] right, object op):
224
223
"""
225
224
cdef:
226
225
Py_ssize_t i, n = len (left)
227
- ndarray[ object ] result
226
+ object [: ] result
228
227
229
228
if n != len (right):
230
- raise ValueError (' Arrays were different lengths: %d vs %d '
231
- % (n, len (right)))
229
+ raise ValueError (' Arrays were different lengths: {n} vs {nright} '
230
+ .format( n = n, nright = len (right)))
232
231
233
232
result = np.empty(n, dtype = object )
234
233
@@ -245,7 +244,7 @@ def vec_binop(ndarray[object] left, ndarray[object] right, object op):
245
244
else :
246
245
raise
247
246
248
- return maybe_convert_bool(result)
247
+ return maybe_convert_bool(result.base) # `.base` to access np.ndarray
249
248
250
249
251
250
def maybe_convert_bool (ndarray[object] arr ,
@@ -270,7 +269,7 @@ def maybe_convert_bool(ndarray[object] arr,
270
269
if false_values is not None :
271
270
false_vals = false_vals | set (false_values)
272
271
273
- for i from 0 <= i < n :
272
+ for i in range (n) :
274
273
val = arr[i]
275
274
276
275
if PyBool_Check(val):
0 commit comments