@@ -504,37 +504,31 @@ def _combine_series_frame(left, right, func, axis: int, str_rep: str):
504
504
505
505
Returns
506
506
-------
507
- result : DataFrame
507
+ result : DataFrame or Dict[int, Series[]]
508
508
"""
509
509
# 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 )
523
510
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 :
528
518
rvalues = rvalues .reshape (1 , - 1 )
529
- rvalues = np .broadcast_to (rvalues , left .shape )
530
519
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 )
534
521
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 :
535
529
new_data = dispatch_to_series (left , right , func , axis = "columns" )
536
530
537
- return left . _construct_result ( new_data )
531
+ return new_data
538
532
539
533
540
534
def _align_method_FRAME (
@@ -713,7 +707,6 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
713
707
pass_op = pass_op if not is_logical else op
714
708
715
709
new_data = self ._combine_frame (other , pass_op , fill_value )
716
- return self ._construct_result (new_data )
717
710
718
711
elif isinstance (other , ABCSeries ):
719
712
# 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):
725
718
raise NotImplementedError (f"fill_value { fill_value } not supported." )
726
719
727
720
axis = self ._get_axis_number (axis ) if axis is not None else 1
728
- return _combine_series_frame (
721
+ new_data = _combine_series_frame (
729
722
self , other , pass_op , axis = axis , str_rep = str_rep
730
723
)
731
724
else :
@@ -734,7 +727,8 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
734
727
self = self .fillna (fill_value )
735
728
736
729
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 )
738
732
739
733
f .__name__ = op_name
740
734
@@ -758,15 +752,17 @@ def f(self, other, axis=default_axis, level=None):
758
752
if isinstance (other , ABCDataFrame ):
759
753
# Another DataFrame
760
754
new_data = dispatch_to_series (self , other , op , str_rep )
761
- return self ._construct_result (new_data )
762
755
763
756
elif isinstance (other , ABCSeries ):
764
757
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
+ )
766
761
else :
767
762
# in this case we always have `np.ndim(other) == 0`
768
763
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 )
770
766
771
767
f .__name__ = op_name
772
768
@@ -784,21 +780,9 @@ def f(self, other):
784
780
self , other , axis = None , level = None , flex = False
785
781
)
786
782
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 )
802
786
return self ._construct_result (new_data )
803
787
804
788
f .__name__ = op_name
0 commit comments