Skip to content

Commit fc6247d

Browse files
jbrockmendeljreback
authored andcommitted
REF: Consolidate alignment calls in DataFrame ops (#28638)
1 parent 0f06fd0 commit fc6247d

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

pandas/core/frame.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -5279,24 +5279,17 @@ def _arith_op(left, right):
52795279
new_data = dispatch_fill_zeros(func, this.values, other.values, res_values)
52805280
return this._construct_result(new_data)
52815281

5282-
def _combine_match_index(self, other, func, level=None):
5283-
left, right = self.align(other, join="outer", axis=0, level=level, copy=False)
5284-
# at this point we have `left.index.equals(right.index)`
5282+
def _combine_match_index(self, other, func):
5283+
# at this point we have `self.index.equals(other.index)`
52855284

5286-
if left._is_mixed_type or right._is_mixed_type:
5285+
if self._is_mixed_type or other._is_mixed_type:
52875286
# operate column-wise; avoid costly object-casting in `.values`
5288-
new_data = ops.dispatch_to_series(left, right, func)
5287+
new_data = ops.dispatch_to_series(self, other, func)
52895288
else:
52905289
# fastpath --> operate directly on values
52915290
with np.errstate(all="ignore"):
5292-
new_data = func(left.values.T, right.values).T
5293-
return left._construct_result(new_data)
5294-
5295-
def _combine_match_columns(self, other: Series, func, level=None):
5296-
left, right = self.align(other, join="outer", axis=1, level=level, copy=False)
5297-
# at this point we have `left.columns.equals(right.index)`
5298-
new_data = ops.dispatch_to_series(left, right, func, axis="columns")
5299-
return left._construct_result(new_data)
5291+
new_data = func(self.values.T, other.values).T
5292+
return new_data
53005293

53015294
def _construct_result(self, result) -> "DataFrame":
53025295
"""

pandas/core/ops/__init__.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def column_op(a, b):
384384
return {i: func(a.iloc[:, i], b.iloc[:, i]) for i in range(len(a.columns))}
385385

386386
elif isinstance(right, ABCSeries) and axis == "columns":
387-
# We only get here if called via left._combine_match_columns,
387+
# We only get here if called via _combine_frame_series,
388388
# in which case we specifically want to operate row-by-row
389389
assert right.index.equals(left.columns)
390390

@@ -613,15 +613,18 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None, level=N
613613
"fill_value {fill} not supported.".format(fill=fill_value)
614614
)
615615

616-
if axis is not None:
617-
axis = self._get_axis_number(axis)
618-
if axis == 0:
619-
return self._combine_match_index(other, func, level=level)
620-
else:
621-
return self._combine_match_columns(other, func, level=level)
616+
if axis is None:
617+
# default axis is columns
618+
axis = 1
619+
620+
axis = self._get_axis_number(axis)
621+
left, right = self.align(other, join="outer", axis=axis, level=level, copy=False)
622+
if axis == 0:
623+
new_data = left._combine_match_index(right, func)
624+
else:
625+
new_data = dispatch_to_series(left, right, func, axis="columns")
622626

623-
# default axis is columns
624-
return self._combine_match_columns(other, func, level=level)
627+
return left._construct_result(new_data)
625628

626629

627630
def _align_method_FRAME(left, right, axis):

0 commit comments

Comments
 (0)