Skip to content

Commit 667bb37

Browse files
jbrockmendeljreback
authored andcommitted
REF: do alignment before _combine_series_frame (#31266)
1 parent 8d1e084 commit 667bb37

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

pandas/core/ops/__init__.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -584,33 +584,23 @@ def flex_wrapper(self, other, level=None, fill_value=None, axis=0):
584584
# DataFrame
585585

586586

587-
def _combine_series_frame(self, other, func, fill_value=None, axis=None, level=None):
587+
def _combine_series_frame(left, right, func, axis: int):
588588
"""
589589
Apply binary operator `func` to self, other using alignment and fill
590-
conventions determined by the fill_value, axis, and level kwargs.
590+
conventions determined by the axis argument.
591591
592592
Parameters
593593
----------
594-
self : DataFrame
595-
other : Series
594+
left : DataFrame
595+
right : Series
596596
func : binary operator
597-
fill_value : object, default None
598-
axis : {0, 1, 'columns', 'index', None}, default None
599-
level : int or None, default None
597+
axis : {0, 1}
600598
601599
Returns
602600
-------
603601
result : DataFrame
604602
"""
605-
if fill_value is not None:
606-
raise NotImplementedError(f"fill_value {fill_value} not supported.")
607-
608-
if axis is None:
609-
# default axis is columns
610-
axis = 1
611-
612-
axis = self._get_axis_number(axis)
613-
left, right = self.align(other, join="outer", axis=axis, level=level, copy=False)
603+
# We assume that self.align(other, ...) has already been called
614604
if axis == 0:
615605
new_data = left._combine_match_index(right, func)
616606
else:
@@ -707,9 +697,15 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
707697
# so do not want the masked op.
708698
pass_op = op if axis in [0, "columns", None] else na_op
709699
pass_op = pass_op if not is_logical else op
710-
return _combine_series_frame(
711-
self, other, pass_op, fill_value=fill_value, axis=axis, level=level
700+
701+
if fill_value is not None:
702+
raise NotImplementedError(f"fill_value {fill_value} not supported.")
703+
704+
axis = self._get_axis_number(axis) if axis is not None else 1
705+
self, other = self.align(
706+
other, join="outer", axis=axis, level=level, copy=False
712707
)
708+
return _combine_series_frame(self, other, pass_op, axis=axis)
713709
else:
714710
# in this case we always have `np.ndim(other) == 0`
715711
if fill_value is not None:
@@ -745,9 +741,11 @@ def f(self, other, axis=default_axis, level=None):
745741
return self._construct_result(new_data)
746742

747743
elif isinstance(other, ABCSeries):
748-
return _combine_series_frame(
749-
self, other, op, fill_value=None, axis=axis, level=level
744+
axis = self._get_axis_number(axis) if axis is not None else 1
745+
self, other = self.align(
746+
other, join="outer", axis=axis, level=level, copy=False
750747
)
748+
return _combine_series_frame(self, other, op, axis=axis)
751749
else:
752750
# in this case we always have `np.ndim(other) == 0`
753751
new_data = dispatch_to_series(self, other, op)
@@ -774,18 +772,21 @@ def f(self, other):
774772
"Can only compare identically-labeled DataFrame objects"
775773
)
776774
new_data = dispatch_to_series(self, other, op, str_rep)
777-
return self._construct_result(new_data)
778775

779776
elif isinstance(other, ABCSeries):
780-
return _combine_series_frame(
781-
self, other, op, fill_value=None, axis=None, level=None
777+
# axis=1 is default for DataFrame-with-Series op
778+
self, other = self.align(
779+
other, join="outer", axis=1, level=None, copy=False
782780
)
781+
new_data = dispatch_to_series(self, other, op, axis="columns")
782+
783783
else:
784784

785785
# straight boolean comparisons we want to allow all columns
786786
# (regardless of dtype to pass thru) See #4537 for discussion.
787787
new_data = dispatch_to_series(self, other, op)
788-
return self._construct_result(new_data)
788+
789+
return self._construct_result(new_data)
789790

790791
f.__name__ = op_name
791792

0 commit comments

Comments
 (0)