Skip to content

Commit cb8a67e

Browse files
jbrockmendeljreback
authored andcommitted
Implement Series.__rdivmod__, un-xfail tests (#23271)
1 parent 1735f42 commit cb8a67e

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

pandas/core/ops.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def _get_op_name(op, special):
518518
'df_examples': None},
519519
'divmod': {'op': 'divmod',
520520
'desc': 'Integer division and modulo',
521-
'reverse': None,
521+
'reverse': 'rdivmod',
522522
'df_examples': None},
523523

524524
# Comparison Operators
@@ -1039,6 +1039,7 @@ def _create_methods(cls, arith_method, comp_method, bool_method, special):
10391039
if have_divmod:
10401040
# divmod doesn't have an op that is supported by numexpr
10411041
new_methods['divmod'] = arith_method(cls, divmod, special)
1042+
new_methods['rdivmod'] = arith_method(cls, rdivmod, special)
10421043

10431044
new_methods.update(dict(
10441045
eq=comp_method(cls, operator.eq, special),
@@ -1224,7 +1225,7 @@ def dispatch_to_extension_op(op, left, right):
12241225
res_values = op(new_left, new_right)
12251226
res_name = get_op_result_name(left, right)
12261227

1227-
if op.__name__ == 'divmod':
1228+
if op.__name__ in ['divmod', 'rdivmod']:
12281229
return _construct_divmod_result(
12291230
left, res_values, left.index, res_name)
12301231

@@ -1241,7 +1242,7 @@ def _arith_method_SERIES(cls, op, special):
12411242
eval_kwargs = _gen_eval_kwargs(op_name)
12421243
fill_zeros = _gen_fill_zeros(op_name)
12431244
construct_result = (_construct_divmod_result
1244-
if op is divmod else _construct_result)
1245+
if op in [divmod, rdivmod] else _construct_result)
12451246

12461247
def na_op(x, y):
12471248
import pandas.core.computation.expressions as expressions
@@ -1871,8 +1872,8 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
18711872
if fill_value is not None:
18721873
self = self.fillna(fill_value)
18731874

1874-
pass_op = op if lib.is_scalar(other) else na_op
1875-
return self._combine_const(other, pass_op, try_cast=True)
1875+
assert np.ndim(other) == 0
1876+
return self._combine_const(other, op, try_cast=True)
18761877

18771878
f.__name__ = op_name
18781879

pandas/tests/arithmetic/test_numeric.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -516,33 +516,38 @@ def test_modulo(self, numeric_idx, box):
516516
result = idx % 2
517517
tm.assert_equal(result, expected)
518518

519-
def test_divmod(self, numeric_idx):
519+
def test_divmod_scalar(self, numeric_idx):
520520
idx = numeric_idx
521+
521522
result = divmod(idx, 2)
522523
with np.errstate(all='ignore'):
523524
div, mod = divmod(idx.values, 2)
524-
expected = Index(div), Index(mod)
525+
526+
expected = Index(div), Index(mod)
525527
for r, e in zip(result, expected):
526528
tm.assert_index_equal(r, e)
527529

530+
def test_divmod_ndarray(self, numeric_idx):
531+
idx = numeric_idx
528532
other = np.ones(idx.values.shape, dtype=idx.values.dtype) * 2
533+
529534
result = divmod(idx, other)
530535
with np.errstate(all='ignore'):
531536
div, mod = divmod(idx.values, other)
532-
expected = Index(div), Index(mod)
537+
538+
expected = Index(div), Index(mod)
533539
for r, e in zip(result, expected):
534540
tm.assert_index_equal(r, e)
535541

536-
@pytest.mark.xfail(reason='GH#19252 Series has no __rdivmod__',
537-
strict=True)
538542
def test_divmod_series(self, numeric_idx):
539543
idx = numeric_idx
540544
other = np.ones(idx.values.shape, dtype=idx.values.dtype) * 2
545+
541546
result = divmod(idx, Series(other))
542547
with np.errstate(all='ignore'):
543548
div, mod = divmod(idx.values, other)
544-
expected = Series(div), Series(mod)
545549

550+
expected = Series(div), Series(mod)
546551
for r, e in zip(result, expected):
547552
tm.assert_series_equal(r, e)
548553

pandas/tests/arithmetic/test_timedelta64.py

-6
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,6 @@ def test_td64arr_add_intlike(self, box_df_broadcast_failure):
553553

554554
@pytest.mark.parametrize('scalar', [1, 1.5, np.array(2)])
555555
def test_td64arr_add_sub_numeric_scalar_invalid(self, box, scalar, tdser):
556-
557-
if box is pd.DataFrame and isinstance(scalar, np.ndarray):
558-
# raises ValueError
559-
pytest.xfail(reason="reversed ops return incorrect answers "
560-
"instead of raising.")
561-
562556
tdser = tm.box_expected(tdser, box)
563557
err = TypeError
564558
if box is pd.Index and not isinstance(scalar, float):

0 commit comments

Comments
 (0)