Skip to content

Commit 87d1636

Browse files
committed
Do __finalize__ in _construct_result and refactor previous solution.
Signed-off-by: HE, Tao <[email protected]>
1 parent db2c641 commit 87d1636

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

doc/source/whatsnew/v0.24.2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Fixed Regressions
3131
- Fixed regression in ``IntervalDtype`` construction where passing an incorrect string with 'Interval' as a prefix could result in a ``RecursionError``. (:issue:`25338`)
3232
- Fixed regression in :class:`Categorical`, where constructing it from a categorical ``Series`` and an explicit ``categories=`` that differed from that in the ``Series`` created an invalid object which could trigger segfaults. (:issue:`25318`)
3333
- Fixed pip installing from source into an environment without NumPy (:issue:`25193`)
34-
- Fixed bug in :meth:`Series._binop` to handle binary operators that returns more than one :class:`Series` correctly (:issue:`25557`)
34+
- Fixed bug in :meth:`Series.divmod` and :meth:`Series.rdivmod` (:issue:`25557`)
3535

3636
.. _whatsnew_0242.enhancements:
3737

pandas/core/ops.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,18 +1494,20 @@ def _construct_result(left, result, index, name, dtype=None):
14941494
not be enough; we still need to override the name attribute.
14951495
"""
14961496
out = left._constructor(result, index=index, dtype=dtype)
1497-
1498-
out.name = name
1497+
out.__finalize__(left)
1498+
if name is None:
1499+
out.name = name
14991500
return out
15001501

15011502

15021503
def _construct_divmod_result(left, result, index, name, dtype=None):
15031504
"""divmod returns a tuple of like indexed series instead of a single series.
15041505
"""
1505-
constructor = left._constructor
15061506
return (
1507-
constructor(result[0], index=index, name=name, dtype=dtype),
1508-
constructor(result[1], index=index, name=name, dtype=dtype),
1507+
_construct_result(left, result[0], index=index, name=name,
1508+
dtype=dtype),
1509+
_construct_result(left, result[1], index=index, name=name,
1510+
dtype=dtype),
15091511
)
15101512

15111513

pandas/core/series.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,14 +2503,6 @@ def _binop(self, other, func, level=None, fill_value=None):
25032503
Series
25042504
"""
25052505

2506-
def mk_ret(result, new_index, name):
2507-
ret = self._constructor(result, index=new_index, name=name)
2508-
ret.__finalize__(self)
2509-
if name is None:
2510-
# When name is None, __finalize__ overwrites current name
2511-
ret.name = None
2512-
return ret
2513-
25142506
if not isinstance(other, Series):
25152507
raise AssertionError('Other operand must be Series')
25162508

@@ -2527,14 +2519,13 @@ def mk_ret(result, new_index, name):
25272519

25282520
with np.errstate(all='ignore'):
25292521
result = func(this_vals, other_vals)
2522+
25302523
name = ops.get_op_result_name(self, other)
2531-
if isinstance(result, tuple):
2532-
final_result = ()
2533-
for r in result:
2534-
final_result = (*final_result, mk_ret(r, new_index, name))
2524+
if func.__name__ in ['divmod', 'rdivmod']:
2525+
ret = ops._construct_divmod_result(self, result, new_index, name)
25352526
else:
2536-
final_result = mk_ret(result, new_index, name)
2537-
return final_result
2527+
ret = ops._construct_result(self, result, new_index, name)
2528+
return ret
25382529

25392530
def combine(self, other, func, fill_value=None):
25402531
"""

pandas/tests/series/test_operators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ def test_op_duplicate_index(self):
742742
assert_series_equal(result, expected)
743743

744744
def test_divmod(self):
745+
# GH25557
745746
a = Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
746747
b = Series([2, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
747748

0 commit comments

Comments
 (0)