def na_arithmetic_op(left, right, op, str_rep: str): """ Return the result of evaluating op on the passed in values. If native types are not compatible, try coersion to object dtype. Parameters ---------- left : np.ndarray right : np.ndarray or scalar str_rep : str or None Returns ------- array-like Raises ------ TypeError : invalid operation """ import pandas.core.computation.expressions as expressions try: result = expressions.evaluate(op, str_rep, left, right) except TypeError: result = masked_arith_op(left, right, op) return missing.dispatch_fill_zeros(op, left, right, result)
def na_op(x, y): """ Return the result of evaluating op on the passed in values. If native types are not compatible, try coersion to object dtype. Parameters ---------- x : array-like y : array-like or scalar Returns ------- array-like Raises ------ TypeError : invalid operation """ import pandas.core.computation.expressions as expressions try: result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs) except TypeError: result = masked_arith_op(x, y, op) return missing.dispatch_fill_zeros(op, x, y, result)
def na_op(x, y): import pandas.core.computation.expressions as expressions try: result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs) except TypeError: result = masked_arith_op(x, y, op) return missing.dispatch_fill_zeros(op, x, y, result)
def _na_arithmetic_op(left: np.ndarray, right, op, is_cmp: bool = False): """ Return the result of evaluating op on the passed in values. If native types are not compatible, try coercion to object dtype. Parameters ---------- left : np.ndarray right : np.ndarray or scalar Excludes DataFrame, Series, Index, ExtensionArray. is_cmp : bool, default False If this a comparison operation. Returns ------- array-like Raises ------ TypeError : invalid operation """ if isinstance(right, str): # can never use numexpr func = op else: func = partial(expressions.evaluate, op) try: result = func(left, right) except TypeError: if not is_cmp and (is_object_dtype(left.dtype) or is_object_dtype(right)): # For object dtype, fallback to a masked operation (only operating # on the non-missing values) # Don't do this for comparisons, as that will handle complex numbers # incorrectly, see GH#32047 result = _masked_arith_op(left, right, op) else: raise if is_cmp and (is_scalar(result) or result is NotImplemented): # numpy returned a scalar instead of operating element-wise # e.g. numeric array vs str # TODO: can remove this after dropping some future numpy version? return invalid_comparison(left, right, op) return missing.dispatch_fill_zeros(op, left, right, result)
def na_arithmetic_op(left, right, op, str_rep: Optional[str], is_cmp: bool = False): """ Return the result of evaluating op on the passed in values. If native types are not compatible, try coersion to object dtype. Parameters ---------- left : np.ndarray right : np.ndarray or scalar str_rep : str or None is_cmp : bool, default False If this a comparison operation. Returns ------- array-like Raises ------ TypeError : invalid operation """ import pandas.core.computation.expressions as expressions try: result = expressions.evaluate(op, str_rep, left, right) except TypeError: if is_cmp: # numexpr failed on comparison op, e.g. ndarray[float] > datetime # In this case we do not fall back to the masked op, as that # will handle complex numbers incorrectly, see GH#32047 raise result = masked_arith_op(left, right, op) if is_cmp and (is_scalar(result) or result is NotImplemented): # numpy returned a scalar instead of operating element-wise # e.g. numeric array vs str return invalid_comparison(left, right, op) return missing.dispatch_fill_zeros(op, left, right, result)
def _na_arithmetic_op(left, right, op, is_cmp: bool = False): """ Return the result of evaluating op on the passed in values. If native types are not compatible, try coercion to object dtype. Parameters ---------- left : np.ndarray right : np.ndarray or scalar is_cmp : bool, default False If this a comparison operation. Returns ------- array-like Raises ------ TypeError : invalid operation """ try: result = expressions.evaluate(op, left, right) except TypeError: if is_object_dtype(left) or is_object_dtype(right) and not is_cmp: # For object dtype, fallback to a masked operation (only operating # on the non-missing values) # Don't do this for comparisons, as that will handle complex numbers # incorrectly, see GH#32047 result = _masked_arith_op(left, right, op) else: raise if is_cmp and (is_scalar(result) or result is NotImplemented): # numpy returned a scalar instead of operating element-wise # e.g. numeric array vs str return invalid_comparison(left, right, op) return missing.dispatch_fill_zeros(op, left, right, result)