diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0f6fa43e020891..67360122ed0214 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5279,24 +5279,17 @@ def _arith_op(left, right): new_data = dispatch_fill_zeros(func, this.values, other.values, res_values) return this._construct_result(new_data) - def _combine_match_index(self, other, func, level=None): - left, right = self.align(other, join="outer", axis=0, level=level, copy=False) - # at this point we have `left.index.equals(right.index)` + def _combine_match_index(self, other, func): + # at this point we have `self.index.equals(other.index)` - if left._is_mixed_type or right._is_mixed_type: + if self._is_mixed_type or other._is_mixed_type: # operate column-wise; avoid costly object-casting in `.values` - new_data = ops.dispatch_to_series(left, right, func) + new_data = ops.dispatch_to_series(self, other, func) else: # fastpath --> operate directly on values with np.errstate(all="ignore"): - new_data = func(left.values.T, right.values).T - return left._construct_result(new_data) - - def _combine_match_columns(self, other: Series, func, level=None): - left, right = self.align(other, join="outer", axis=1, level=level, copy=False) - # at this point we have `left.columns.equals(right.index)` - new_data = ops.dispatch_to_series(left, right, func, axis="columns") - return left._construct_result(new_data) + new_data = func(self.values.T, other.values).T + return new_data def _construct_result(self, result) -> "DataFrame": """ diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 79272c56432817..c979b473ad09ac 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -384,7 +384,7 @@ def column_op(a, b): return {i: func(a.iloc[:, i], b.iloc[:, i]) for i in range(len(a.columns))} elif isinstance(right, ABCSeries) and axis == "columns": - # We only get here if called via left._combine_match_columns, + # We only get here if called via _combine_frame_series, # in which case we specifically want to operate row-by-row assert right.index.equals(left.columns) @@ -613,15 +613,18 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None, level=N "fill_value {fill} not supported.".format(fill=fill_value) ) - if axis is not None: - axis = self._get_axis_number(axis) - if axis == 0: - return self._combine_match_index(other, func, level=level) - else: - return self._combine_match_columns(other, func, level=level) + if axis is None: + # default axis is columns + axis = 1 + + axis = self._get_axis_number(axis) + left, right = self.align(other, join="outer", axis=axis, level=level, copy=False) + if axis == 0: + new_data = left._combine_match_index(right, func) + else: + new_data = dispatch_to_series(left, right, func, axis="columns") - # default axis is columns - return self._combine_match_columns(other, func, level=level) + return left._construct_result(new_data) def _align_method_FRAME(left, right, axis):