Skip to content

Commit 582d4f4

Browse files
committed
Fix ValueError (pandas-dev#60908)
1 parent 5736b96 commit 582d4f4

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

Diff for: pandas/core/series.py

+28-24
Original file line numberDiff line numberDiff line change
@@ -5874,31 +5874,35 @@ def _arith_method(self, other, op):
58745874
self, other = self._align_for_op(other)
58755875
return base.IndexOpsMixin._arith_method(self, other, op)
58765876

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):
58825878
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)
59025906

59035907
def _binop(self, other: Series, func, level=None, fill_value=None) -> Series:
59045908
"""

0 commit comments

Comments
 (0)