MCPcopy
hub / github.com/pandas-dev/pandas / arithmetic_op

Function arithmetic_op

pandas/core/ops/array_ops.py:242–291  ·  view source on GitHub ↗

Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ... Note: the caller is responsible for ensuring that numpy warnings are suppressed (with np.errstate(all="ignore")) if needed. Parameters ---------- left : np.ndarray or ExtensionArray right : objec

(left: ArrayLike, right: Any, op)

Source from the content-addressed store, hash-verified

240
241
242def arithmetic_op(left: ArrayLike, right: Any, op):
243 """
244 Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ...
245
246 Note: the caller is responsible for ensuring that numpy warnings are
247 suppressed (with np.errstate(all="ignore")) if needed.
248
249 Parameters
250 ----------
251 left : np.ndarray or ExtensionArray
252 right : object
253 Cannot be a DataFrame or Index. Series is *not* excluded.
254 op : {operator.add, operator.sub, ...}
255 Or one of the reversed variants from roperator.
256
257 Returns
258 -------
259 ndarray or ExtensionArray
260 Or a 2-tuple of these in the case of divmod or rdivmod.
261 """
262 # NB: We assume that extract_array and ensure_wrapped_if_datetimelike
263 # have already been called on `left` and `right`,
264 # and `maybe_prepare_scalar_for_op` has already been called on `right`
265 # We need to special-case datetime64/timedelta64 dtypes (e.g. because numpy
266 # casts integer dtypes to timedelta64 when operating with timedelta64 - GH#22390)
267 if isinstance(right, list):
268 # GH#62423
269 right = sanitize_array(right, None)
270 right = ensure_wrapped_if_datetimelike(right)
271
272 if (
273 should_extension_dispatch(left, right)
274 or isinstance(right, (Timedelta, BaseOffset, Timestamp))
275 or right is NaT
276 ):
277 # Timedelta/Timestamp and other custom scalars are included in the check
278 # because numexpr will fail on it, see GH#31457
279 res_values = op(left, right)
280 else:
281 # TODO we should handle EAs consistently and move this check before the if/else
282 # (https://github.com/pandas-dev/pandas/issues/41165)
283 # error: Argument 2 to "_bool_arith_check" has incompatible type
284 # "Union[ExtensionArray, ndarray[Any, Any]]"; expected "ndarray[Any, Any]"
285 _bool_arith_check(op, left, right) # type: ignore[arg-type]
286
287 # error: Argument 1 to "_na_arithmetic_op" has incompatible type
288 # "Union[ExtensionArray, ndarray[Any, Any]]"; expected "ndarray[Any, Any]"
289 res_values = _na_arithmetic_op(left, right, op) # type: ignore[arg-type]
290
291 return res_values
292
293
294def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike:

Callers

nothing calls this directly

Calls 6

sanitize_arrayFunction · 0.90
opFunction · 0.85
_bool_arith_checkFunction · 0.85
_na_arithmetic_opFunction · 0.85

Tested by

no test coverage detected