diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 856a1823353d5..15af453e7b5f8 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -262,15 +262,11 @@ def dispatch_to_series(left, right, func, axis=None): ------- DataFrame """ - # Note: we use iloc to access columns for compat with cases - # with non-unique columns. - import pandas.core.computation.expressions as expressions + # Get the appropriate array-op to apply to each column/block's values. + array_op = get_array_op(func) right = lib.item_from_zerodim(right) if lib.is_scalar(right) or np.ndim(right) == 0: - - # Get the appropriate array-op to apply to each block's values. - array_op = get_array_op(func) bm = left._mgr.apply(array_op, right=right) return type(left)(bm) @@ -281,7 +277,6 @@ def dispatch_to_series(left, right, func, axis=None): # fails in cases with empty columns reached via # _frame_arith_method_with_reindex - array_op = get_array_op(func) bm = left._mgr.operate_blockwise(right._mgr, array_op) return type(left)(bm) @@ -295,27 +290,24 @@ def dispatch_to_series(left, right, func, axis=None): # Note: we do not do this unconditionally as it may be lossy or # expensive for EA dtypes. right = np.asarray(right) - - def column_op(a, b): - return {i: func(a.iloc[:, i], b[i]) for i in range(len(a.columns))} - else: + right = right._values - def column_op(a, b): - return {i: func(a.iloc[:, i], b.iloc[i]) for i in range(len(a.columns))} + arrays = [array_op(l, r) for l, r in zip(left._iter_column_arrays(), right)] elif isinstance(right, ABCSeries): assert right.index.equals(left.index) # Handle other cases later + right = right._values - def column_op(a, b): - return {i: func(a.iloc[:, i], b) for i in range(len(a.columns))} + arrays = [array_op(l, right) for l in left._iter_column_arrays()] else: # Remaining cases have less-obvious dispatch rules raise NotImplementedError(right) - new_data = expressions.evaluate(column_op, left, right) - return new_data + return type(left)._from_arrays( + arrays, left.columns, left.index, verify_integrity=False + ) # -----------------------------------------------------------------------------