@@ -5874,31 +5874,35 @@ def _arith_method(self, other, op):
5874
5874
self , other = self ._align_for_op (other )
5875
5875
return base .IndexOpsMixin ._arith_method (self , other , op )
5876
5876
5877
- def _align_for_op (self , right , align_asobject : bool = False ):
5878
- """align lhs and rhs Series"""
5879
- # TODO: Different from DataFrame._align_for_op, list, tuple and ndarray
5880
- # are not coerced here
5881
- # because Series has inconsistencies described in GH#13637
5877
+ def _align_for_op (self , right , align_asobject = False , fill_value = np .nan ):
5882
5878
left = self
5883
-
5884
- if isinstance (right , Series ):
5885
- # avoid repeated alignment
5886
- if not left .index .equals (right .index ):
5887
- if align_asobject :
5888
- if left .dtype not in (object , np .bool_ ) or right .dtype not in (
5889
- object ,
5890
- np .bool_ ,
5891
- ):
5892
- pass
5893
- # GH#52538 no longer cast in these cases
5894
- else :
5895
- # to keep original value's dtype for bool ops
5896
- left = left .astype (object )
5897
- right = right .astype (object )
5898
-
5899
- left , right = left .align (right )
5900
-
5901
- return left , right
5879
+ if not isinstance (right , Series ):
5880
+ return left , right
5881
+
5882
+ if not (hasattr (left .index , "levels" ) or hasattr (right .index , "levels" )):
5883
+ if left .empty or right .empty :
5884
+ return left .iloc [0 :0 ], right .iloc [0 :0 ]
5885
+ return left .align (right , join = 'outer' , fill_value = fill_value )
5886
+
5887
+ if hasattr (left .index , "levels" ) and not hasattr (right .index , "levels" ):
5888
+ if left .empty or right .empty :
5889
+ return left .iloc [0 :0 ], right .iloc [0 :0 ]
5890
+
5891
+ first_level = left .index .get_level_values (0 )
5892
+ right_aligned = right .reindex (first_level , fill_value = fill_value )
5893
+ return left , right_aligned
5894
+
5895
+ if hasattr (right .index , "levels" ) and not hasattr (left .index , "levels" ):
5896
+ if left .empty or right .empty :
5897
+ return left .iloc [0 :0 ], right .iloc [0 :0 ]
5898
+
5899
+ first_level = right .index .get_level_values (0 )
5900
+ left_aligned = left .reindex (first_level , fill_value = fill_value )
5901
+ return left_aligned , right
5902
+
5903
+ if left .empty or right .empty :
5904
+ return left .iloc [0 :0 ], right .iloc [0 :0 ]
5905
+ return left .align (right , join = 'outer' , fill_value = fill_value )
5902
5906
5903
5907
def _binop (self , other : Series , func , level = None , fill_value = None ) -> Series :
5904
5908
"""
0 commit comments