Skip to content

Commit 8588651

Browse files
REF/PERF: move np.errstate out of core array_ops up to higher level (#40396)
1 parent 0ff5b40 commit 8588651

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

pandas/core/arrays/numpy_.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ def _cmp_method(self, other, op):
376376
other = other._ndarray
377377

378378
pd_op = ops.get_array_op(op)
379-
result = pd_op(self._ndarray, other)
379+
with np.errstate(all="ignore"):
380+
result = pd_op(self._ndarray, other)
380381

381382
if op is divmod or op is ops.rdivmod:
382383
a, b = result

pandas/core/computation/expressions.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def _evaluate_standard(op, op_str, a, b):
7171
"""
7272
if _TEST_MODE:
7373
_store_test_result(False)
74-
with np.errstate(all="ignore"):
75-
return op(a, b)
74+
return op(a, b)
7675

7776

7877
def _can_use_numexpr(op, op_str, a, b, dtype_check):

pandas/core/frame.py

+20-16
Original file line numberDiff line numberDiff line change
@@ -6643,7 +6643,8 @@ def _dispatch_frame_op(self, right, func: Callable, axis: Optional[int] = None):
66436643
right = lib.item_from_zerodim(right)
66446644
if not is_list_like(right):
66456645
# i.e. scalar, faster than checking np.ndim(right) == 0
6646-
bm = self._mgr.apply(array_op, right=right)
6646+
with np.errstate(all="ignore"):
6647+
bm = self._mgr.apply(array_op, right=right)
66476648
return type(self)(bm)
66486649

66496650
elif isinstance(right, DataFrame):
@@ -6654,16 +6655,17 @@ def _dispatch_frame_op(self, right, func: Callable, axis: Optional[int] = None):
66546655
# _frame_arith_method_with_reindex
66556656

66566657
# TODO operate_blockwise expects a manager of the same type
6657-
bm = self._mgr.operate_blockwise(
6658-
# error: Argument 1 to "operate_blockwise" of "ArrayManager" has
6659-
# incompatible type "Union[ArrayManager, BlockManager]"; expected
6660-
# "ArrayManager"
6661-
# error: Argument 1 to "operate_blockwise" of "BlockManager" has
6662-
# incompatible type "Union[ArrayManager, BlockManager]"; expected
6663-
# "BlockManager"
6664-
right._mgr, # type: ignore[arg-type]
6665-
array_op,
6666-
)
6658+
with np.errstate(all="ignore"):
6659+
bm = self._mgr.operate_blockwise(
6660+
# error: Argument 1 to "operate_blockwise" of "ArrayManager" has
6661+
# incompatible type "Union[ArrayManager, BlockManager]"; expected
6662+
# "ArrayManager"
6663+
# error: Argument 1 to "operate_blockwise" of "BlockManager" has
6664+
# incompatible type "Union[ArrayManager, BlockManager]"; expected
6665+
# "BlockManager"
6666+
right._mgr, # type: ignore[arg-type]
6667+
array_op,
6668+
)
66676669
return type(self)(bm)
66686670

66696671
elif isinstance(right, Series) and axis == 1:
@@ -6674,16 +6676,18 @@ def _dispatch_frame_op(self, right, func: Callable, axis: Optional[int] = None):
66746676
# maybe_align_as_frame ensures we do not have an ndarray here
66756677
assert not isinstance(right, np.ndarray)
66766678

6677-
arrays = [
6678-
array_op(_left, _right)
6679-
for _left, _right in zip(self._iter_column_arrays(), right)
6680-
]
6679+
with np.errstate(all="ignore"):
6680+
arrays = [
6681+
array_op(_left, _right)
6682+
for _left, _right in zip(self._iter_column_arrays(), right)
6683+
]
66816684

66826685
elif isinstance(right, Series):
66836686
assert right.index.equals(self.index) # Handle other cases later
66846687
right = right._values
66856688

6686-
arrays = [array_op(left, right) for left in self._iter_column_arrays()]
6689+
with np.errstate(all="ignore"):
6690+
arrays = [array_op(left, right) for left in self._iter_column_arrays()]
66876691

66886692
else:
66896693
# Remaining cases have less-obvious dispatch rules

pandas/core/ops/array_ops.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ def _masked_arith_op(x: np.ndarray, y, op):
106106

107107
# See GH#5284, GH#5035, GH#19448 for historical reference
108108
if mask.any():
109-
with np.errstate(all="ignore"):
110-
result[mask] = op(xrav[mask], yrav[mask])
109+
result[mask] = op(xrav[mask], yrav[mask])
111110

112111
else:
113112
if not is_scalar(y):
@@ -126,8 +125,7 @@ def _masked_arith_op(x: np.ndarray, y, op):
126125
mask = np.where(y == 1, False, mask)
127126

128127
if mask.any():
129-
with np.errstate(all="ignore"):
130-
result[mask] = op(xrav[mask], y)
128+
result[mask] = op(xrav[mask], y)
131129

132130
result = maybe_upcast_putmask(result, ~mask)
133131
result = result.reshape(x.shape) # 2D compat
@@ -179,6 +177,9 @@ def arithmetic_op(left: ArrayLike, right: Any, op):
179177
"""
180178
Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ...
181179
180+
Note: the caller is responsible for ensuring that numpy warnings are
181+
suppressed (with np.errstate(all="ignore")) if needed.
182+
182183
Parameters
183184
----------
184185
left : np.ndarray or ExtensionArray
@@ -206,8 +207,7 @@ def arithmetic_op(left: ArrayLike, right: Any, op):
206207
res_values = op(lvalues, rvalues)
207208

208209
else:
209-
with np.errstate(all="ignore"):
210-
res_values = _na_arithmetic_op(lvalues, rvalues, op)
210+
res_values = _na_arithmetic_op(lvalues, rvalues, op)
211211

212212
return res_values
213213

@@ -216,6 +216,9 @@ def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike:
216216
"""
217217
Evaluate a comparison operation `=`, `!=`, `>=`, `>`, `<=`, or `<`.
218218
219+
Note: the caller is responsible for ensuring that numpy warnings are
220+
suppressed (with np.errstate(all="ignore")) if needed.
221+
219222
Parameters
220223
----------
221224
left : np.ndarray or ExtensionArray
@@ -267,8 +270,7 @@ def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike:
267270
with warnings.catch_warnings():
268271
# suppress warnings from numpy about element-wise comparison
269272
warnings.simplefilter("ignore", DeprecationWarning)
270-
with np.errstate(all="ignore"):
271-
res_values = _na_arithmetic_op(lvalues, rvalues, op, is_cmp=True)
273+
res_values = _na_arithmetic_op(lvalues, rvalues, op, is_cmp=True)
272274

273275
return res_values
274276

pandas/core/series.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5087,7 +5087,8 @@ def _cmp_method(self, other, op):
50875087
lvalues = self._values
50885088
rvalues = extract_array(other, extract_numpy=True)
50895089

5090-
res_values = ops.comparison_op(lvalues, rvalues, op)
5090+
with np.errstate(all="ignore"):
5091+
res_values = ops.comparison_op(lvalues, rvalues, op)
50915092

50925093
return self._construct_result(res_values, name=res_name)
50935094

@@ -5107,7 +5108,8 @@ def _arith_method(self, other, op):
51075108

51085109
lvalues = self._values
51095110
rvalues = extract_array(other, extract_numpy=True)
5110-
result = ops.arithmetic_op(lvalues, rvalues, op)
5111+
with np.errstate(all="ignore"):
5112+
result = ops.arithmetic_op(lvalues, rvalues, op)
51115113

51125114
return self._construct_result(result, name=res_name)
51135115

0 commit comments

Comments
 (0)