Skip to content

Commit 5398eca

Browse files
authored
CLN: de-duplicate ops code (#34079)
1 parent 9eeb171 commit 5398eca

File tree

1 file changed

+28
-44
lines changed

1 file changed

+28
-44
lines changed

pandas/core/ops/__init__.py

+28-44
Original file line numberDiff line numberDiff line change
@@ -504,37 +504,31 @@ def _combine_series_frame(left, right, func, axis: int, str_rep: str):
504504
505505
Returns
506506
-------
507-
result : DataFrame
507+
result : DataFrame or Dict[int, Series[]]
508508
"""
509509
# We assume that self.align(other, ...) has already been called
510-
if axis == 0:
511-
values = right._values
512-
if isinstance(values, np.ndarray):
513-
# TODO(EA2D): no need to special-case with 2D EAs
514-
# We can operate block-wise
515-
values = values.reshape(-1, 1)
516-
values = np.broadcast_to(values, left.shape)
517-
518-
array_op = get_array_op(func, str_rep=str_rep)
519-
bm = left._mgr.apply(array_op, right=values.T, align_keys=["right"])
520-
return type(left)(bm)
521-
522-
new_data = dispatch_to_series(left, right, func)
523510

524-
else:
525-
rvalues = right._values
526-
if isinstance(rvalues, np.ndarray):
527-
# We can operate block-wise
511+
rvalues = right._values
512+
if isinstance(rvalues, np.ndarray):
513+
# TODO(EA2D): no need to special-case with 2D EAs
514+
# We can operate block-wise
515+
if axis == 0:
516+
rvalues = rvalues.reshape(-1, 1)
517+
else:
528518
rvalues = rvalues.reshape(1, -1)
529-
rvalues = np.broadcast_to(rvalues, left.shape)
530519

531-
array_op = get_array_op(func, str_rep=str_rep)
532-
bm = left._mgr.apply(array_op, right=rvalues.T, align_keys=["right"])
533-
return type(left)(bm)
520+
rvalues = np.broadcast_to(rvalues, left.shape)
534521

522+
array_op = get_array_op(func, str_rep=str_rep)
523+
bm = left._mgr.apply(array_op, right=rvalues.T, align_keys=["right"])
524+
return type(left)(bm)
525+
526+
if axis == 0:
527+
new_data = dispatch_to_series(left, right, func)
528+
else:
535529
new_data = dispatch_to_series(left, right, func, axis="columns")
536530

537-
return left._construct_result(new_data)
531+
return new_data
538532

539533

540534
def _align_method_FRAME(
@@ -713,7 +707,6 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
713707
pass_op = pass_op if not is_logical else op
714708

715709
new_data = self._combine_frame(other, pass_op, fill_value)
716-
return self._construct_result(new_data)
717710

718711
elif isinstance(other, ABCSeries):
719712
# For these values of `axis`, we end up dispatching to Series op,
@@ -725,7 +718,7 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
725718
raise NotImplementedError(f"fill_value {fill_value} not supported.")
726719

727720
axis = self._get_axis_number(axis) if axis is not None else 1
728-
return _combine_series_frame(
721+
new_data = _combine_series_frame(
729722
self, other, pass_op, axis=axis, str_rep=str_rep
730723
)
731724
else:
@@ -734,7 +727,8 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
734727
self = self.fillna(fill_value)
735728

736729
new_data = dispatch_to_series(self, other, op, str_rep)
737-
return self._construct_result(new_data)
730+
731+
return self._construct_result(new_data)
738732

739733
f.__name__ = op_name
740734

@@ -758,15 +752,17 @@ def f(self, other, axis=default_axis, level=None):
758752
if isinstance(other, ABCDataFrame):
759753
# Another DataFrame
760754
new_data = dispatch_to_series(self, other, op, str_rep)
761-
return self._construct_result(new_data)
762755

763756
elif isinstance(other, ABCSeries):
764757
axis = self._get_axis_number(axis) if axis is not None else 1
765-
return _combine_series_frame(self, other, op, axis=axis, str_rep=str_rep)
758+
new_data = _combine_series_frame(
759+
self, other, op, axis=axis, str_rep=str_rep
760+
)
766761
else:
767762
# in this case we always have `np.ndim(other) == 0`
768763
new_data = dispatch_to_series(self, other, op, str_rep)
769-
return self._construct_result(new_data)
764+
765+
return self._construct_result(new_data)
770766

771767
f.__name__ = op_name
772768

@@ -784,21 +780,9 @@ def f(self, other):
784780
self, other, axis=None, level=None, flex=False
785781
)
786782

787-
if isinstance(other, ABCDataFrame):
788-
# Another DataFrame
789-
new_data = dispatch_to_series(self, other, op, str_rep)
790-
791-
elif isinstance(other, ABCSeries):
792-
new_data = dispatch_to_series(
793-
self, other, op, str_rep=str_rep, axis="columns"
794-
)
795-
796-
else:
797-
798-
# straight boolean comparisons we want to allow all columns
799-
# (regardless of dtype to pass thru) See #4537 for discussion.
800-
new_data = dispatch_to_series(self, other, op, str_rep)
801-
783+
axis = "columns" # only relevant for Series other case
784+
# See GH#4537 for discussion of scalar op behavior
785+
new_data = dispatch_to_series(self, other, op, str_rep, axis=axis)
802786
return self._construct_result(new_data)
803787

804788
f.__name__ = op_name

0 commit comments

Comments
 (0)