@@ -65,23 +65,23 @@ def set_numexpr_threads(n=None) -> None:
65
65
ne .set_num_threads (n )
66
66
67
67
68
- def _evaluate_standard (op , op_str , a , b ):
68
+ def _evaluate_standard (op , op_str , left_op , right_op ):
69
69
"""
70
70
Standard evaluation.
71
71
"""
72
72
if _TEST_MODE :
73
73
_store_test_result (False )
74
- return op (a , b )
74
+ return op (left_op , right_op )
75
75
76
76
77
- def _can_use_numexpr (op , op_str , a , b , dtype_check ) -> bool :
77
+ def _can_use_numexpr (op , op_str , left_op , right_op , dtype_check ) -> bool :
78
78
"""return a boolean if we WILL be using numexpr"""
79
79
if op_str is not None :
80
80
# required min elements (otherwise we are adding overhead)
81
- if a .size > _MIN_ELEMENTS :
81
+ if left_op .size > _MIN_ELEMENTS :
82
82
# check for dtype compatibility
83
83
dtypes : set [str ] = set ()
84
- for o in [a , b ]:
84
+ for o in [left_op , right_op ]:
85
85
# ndarray and Series Case
86
86
if hasattr (o , "dtype" ):
87
87
dtypes |= {o .dtype .name }
@@ -93,43 +93,43 @@ def _can_use_numexpr(op, op_str, a, b, dtype_check) -> bool:
93
93
return False
94
94
95
95
96
- def _evaluate_numexpr (op , op_str , a , b ):
96
+ def _evaluate_numexpr (op , op_str , left_op , right_op ):
97
97
result = None
98
98
99
- if _can_use_numexpr (op , op_str , a , b , "evaluate" ):
99
+ if _can_use_numexpr (op , op_str , left_op , right_op , "evaluate" ):
100
100
is_reversed = op .__name__ .strip ("_" ).startswith ("r" )
101
101
if is_reversed :
102
102
# we were originally called by a reversed op method
103
- a , b = b , a
103
+ left_op , right_op = right_op , left_op
104
104
105
- a_value = a
106
- b_value = b
105
+ left_value = left_op
106
+ right_value = right_op
107
107
108
108
try :
109
109
result = ne .evaluate (
110
- f"a_value { op_str } b_value " ,
111
- local_dict = {"a_value " : a_value , "b_value " : b_value },
110
+ f"left_value { op_str } right_value " ,
111
+ local_dict = {"left_value " : left_value , "right_value " : right_value },
112
112
casting = "safe" ,
113
113
)
114
114
except TypeError :
115
115
# numexpr raises eg for array ** array with integers
116
116
# (https://github.com/pydata/numexpr/issues/379)
117
117
pass
118
118
except NotImplementedError :
119
- if _bool_arith_fallback (op_str , a , b ):
119
+ if _bool_arith_fallback (op_str , left_op , right_op ):
120
120
pass
121
121
else :
122
122
raise
123
123
124
124
if is_reversed :
125
125
# reverse order to original for fallback
126
- a , b = b , a
126
+ left_op , right_op = right_op , left_op
127
127
128
128
if _TEST_MODE :
129
129
_store_test_result (result is not None )
130
130
131
131
if result is None :
132
- result = _evaluate_standard (op , op_str , a , b )
132
+ result = _evaluate_standard (op , op_str , left_op , right_op )
133
133
134
134
return result
135
135
@@ -170,24 +170,28 @@ def _evaluate_numexpr(op, op_str, a, b):
170
170
}
171
171
172
172
173
- def _where_standard (cond , a , b ):
173
+ def _where_standard (cond , left_op , right_op ):
174
174
# Caller is responsible for extracting ndarray if necessary
175
- return np .where (cond , a , b )
175
+ return np .where (cond , left_op , right_op )
176
176
177
177
178
- def _where_numexpr (cond , a , b ):
178
+ def _where_numexpr (cond , left_op , right_op ):
179
179
# Caller is responsible for extracting ndarray if necessary
180
180
result = None
181
181
182
- if _can_use_numexpr (None , "where" , a , b , "where" ):
182
+ if _can_use_numexpr (None , "where" , left_op , right_op , "where" ):
183
183
result = ne .evaluate (
184
- "where(cond_value, a_value, b_value)" ,
185
- local_dict = {"cond_value" : cond , "a_value" : a , "b_value" : b },
184
+ "where(cond_value, left_value, right_value)" ,
185
+ local_dict = {
186
+ "cond_value" : cond ,
187
+ "left_value" : left_op ,
188
+ "right_value" : right_op
189
+ },
186
190
casting = "safe" ,
187
191
)
188
192
189
193
if result is None :
190
- result = _where_standard (cond , a , b )
194
+ result = _where_standard (cond , left_op , right_op )
191
195
192
196
return result
193
197
@@ -206,13 +210,13 @@ def _has_bool_dtype(x):
206
210
_BOOL_OP_UNSUPPORTED = {"+" : "|" , "*" : "&" , "-" : "^" }
207
211
208
212
209
- def _bool_arith_fallback (op_str , a , b ) -> bool :
213
+ def _bool_arith_fallback (op_str , left_op , right_op ) -> bool :
210
214
"""
211
215
Check if we should fallback to the python `_evaluate_standard` in case
212
216
of an unsupported operation by numexpr, which is the case for some
213
217
boolean ops.
214
218
"""
215
- if _has_bool_dtype (a ) and _has_bool_dtype (b ):
219
+ if _has_bool_dtype (left_op ) and _has_bool_dtype (right_op ):
216
220
if op_str in _BOOL_OP_UNSUPPORTED :
217
221
warnings .warn (
218
222
f"evaluating in Python space because the { op_str !r} "
@@ -224,40 +228,42 @@ def _bool_arith_fallback(op_str, a, b) -> bool:
224
228
return False
225
229
226
230
227
- def evaluate (op , a , b , use_numexpr : bool = True ):
231
+ def evaluate (op , left_op , right_op , use_numexpr : bool = True ):
228
232
"""
229
- Evaluate and return the expression of the op on a and b .
233
+ Evaluate and return the expression of the op on left_op and right_op .
230
234
231
235
Parameters
232
236
----------
233
237
op : the actual operand
234
- a : left operand
235
- b : right operand
238
+ left_op : left operand
239
+ right_op : right operand
236
240
use_numexpr : bool, default True
237
241
Whether to try to use numexpr.
238
242
"""
239
243
op_str = _op_str_mapping [op ]
240
244
if op_str is not None :
241
245
if use_numexpr :
242
246
# error: "None" not callable
243
- return _evaluate (op , op_str , a , b ) # type: ignore[misc]
244
- return _evaluate_standard (op , op_str , a , b )
247
+ return _evaluate (op , op_str , left_op , right_op ) # type: ignore[misc]
248
+ return _evaluate_standard (op , op_str , left_op , right_op )
245
249
246
250
247
- def where (cond , a , b , use_numexpr : bool = True ):
251
+ def where (cond , left_op , right_op , use_numexpr : bool = True ):
248
252
"""
249
- Evaluate the where condition cond on a and b .
253
+ Evaluate the where condition cond on left_op and right_op .
250
254
251
255
Parameters
252
256
----------
253
257
cond : np.ndarray[bool]
254
- a : return if cond is True
255
- b : return if cond is False
258
+ left_op : return if cond is True
259
+ right_op : return if cond is False
256
260
use_numexpr : bool, default True
257
261
Whether to try to use numexpr.
258
262
"""
259
263
assert _where is not None
260
- return _where (cond , a , b ) if use_numexpr else _where_standard (cond , a , b )
264
+ return (_where (cond , left_op , right_op )
265
+ if use_numexpr
266
+ else _where_standard (cond , left_op , right_op ))
261
267
262
268
263
269
def set_test_mode (v : bool = True ) -> None :
0 commit comments