Skip to content

Commit 37d04a3

Browse files
sighingnowjreback
authored andcommitted
BUG: Fix _binop for operators for serials which has more than one returns (divmod/rdivmod). (#25588)
1 parent 27aa9d8 commit 37d04a3

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

doc/source/whatsnew/v0.25.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Performance Improvements
182182
Bug Fixes
183183
~~~~~~~~~
184184

185+
185186
Categorical
186187
^^^^^^^^^^^
187188

@@ -217,6 +218,7 @@ Numeric
217218
- Bug in :meth:`to_numeric` in which large negative numbers were being improperly handled (:issue:`24910`)
218219
- Bug in :meth:`to_numeric` in which numbers were being coerced to float, even though ``errors`` was not ``coerce`` (:issue:`24910`)
219220
- Bug in error messages in :meth:`DataFrame.corr` and :meth:`Series.corr`. Added the possibility of using a callable. (:issue:`25729`)
221+
- Bug in :meth:`Series.divmod` and :meth:`Series.rdivmod` which would raise an (incorrect) ``ValueError`` rather than return a pair of :class:`Series` objects as result (:issue:`25557`)
220222
-
221223
-
222224
-

pandas/core/ops.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1660,18 +1660,19 @@ def _construct_result(left, result, index, name, dtype=None):
16601660
not be enough; we still need to override the name attribute.
16611661
"""
16621662
out = left._constructor(result, index=index, dtype=dtype)
1663-
1663+
out = out.__finalize__(left)
16641664
out.name = name
16651665
return out
16661666

16671667

16681668
def _construct_divmod_result(left, result, index, name, dtype=None):
16691669
"""divmod returns a tuple of like indexed series instead of a single series.
16701670
"""
1671-
constructor = left._constructor
16721671
return (
1673-
constructor(result[0], index=index, name=name, dtype=dtype),
1674-
constructor(result[1], index=index, name=name, dtype=dtype),
1672+
_construct_result(left, result[0], index=index, name=name,
1673+
dtype=dtype),
1674+
_construct_result(left, result[1], index=index, name=name,
1675+
dtype=dtype),
16751676
)
16761677

16771678

pandas/core/series.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,7 @@ def _binop(self, other, func, level=None, fill_value=None):
25272527
-------
25282528
Series
25292529
"""
2530+
25302531
if not isinstance(other, Series):
25312532
raise AssertionError('Other operand must be Series')
25322533

@@ -2543,13 +2544,13 @@ def _binop(self, other, func, level=None, fill_value=None):
25432544

25442545
with np.errstate(all='ignore'):
25452546
result = func(this_vals, other_vals)
2547+
25462548
name = ops.get_op_result_name(self, other)
2547-
result = self._constructor(result, index=new_index, name=name)
2548-
result = result.__finalize__(self)
2549-
if name is None:
2550-
# When name is None, __finalize__ overwrites current name
2551-
result.name = None
2552-
return result
2549+
if func.__name__ in ['divmod', 'rdivmod']:
2550+
ret = ops._construct_divmod_result(self, result, new_index, name)
2551+
else:
2552+
ret = ops._construct_result(self, result, new_index, name)
2553+
return ret
25532554

25542555
def combine(self, other, func, fill_value=None):
25552556
"""

pandas/tests/series/test_operators.py

+15
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,21 @@ def test_op_duplicate_index(self):
741741
expected = pd.Series([11, 12, np.nan], index=[1, 1, 2])
742742
assert_series_equal(result, expected)
743743

744+
def test_divmod(self):
745+
# GH25557
746+
a = Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
747+
b = Series([2, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
748+
749+
result = a.divmod(b)
750+
expected = divmod(a, b)
751+
assert_series_equal(result[0], expected[0])
752+
assert_series_equal(result[1], expected[1])
753+
754+
result = a.rdivmod(b)
755+
expected = divmod(b, a)
756+
assert_series_equal(result[0], expected[0])
757+
assert_series_equal(result[1], expected[1])
758+
744759

745760
class TestSeriesUnaryOps(object):
746761
# __neg__, __pos__, __inv__

0 commit comments

Comments
 (0)