Skip to content

Implement Series.__rdivmod__, un-xfail tests #23271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def _get_op_name(op, special):
'df_examples': None},
'divmod': {'op': 'divmod',
'desc': 'Integer division and modulo',
'reverse': None,
'reverse': 'rdivmod',
'df_examples': None},

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

new_methods.update(dict(
eq=comp_method(cls, operator.eq, special),
Expand Down Expand Up @@ -1224,7 +1225,7 @@ def dispatch_to_extension_op(op, left, right):
res_values = op(new_left, new_right)
res_name = get_op_result_name(left, right)

if op.__name__ == 'divmod':
if op.__name__ in ['divmod', 'rdivmod']:
return _construct_divmod_result(
left, res_values, left.index, res_name)

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

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

pass_op = op if lib.is_scalar(other) else na_op
return self._combine_const(other, pass_op, try_cast=True)
assert np.ndim(other) == 0
return self._combine_const(other, op, try_cast=True)

f.__name__ = op_name

Expand Down
17 changes: 11 additions & 6 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,33 +516,38 @@ def test_modulo(self, numeric_idx, box):
result = idx % 2
tm.assert_equal(result, expected)

def test_divmod(self, numeric_idx):
def test_divmod_scalar(self, numeric_idx):
idx = numeric_idx

result = divmod(idx, 2)
with np.errstate(all='ignore'):
div, mod = divmod(idx.values, 2)
expected = Index(div), Index(mod)

expected = Index(div), Index(mod)
for r, e in zip(result, expected):
tm.assert_index_equal(r, e)

def test_divmod_ndarray(self, numeric_idx):
idx = numeric_idx
other = np.ones(idx.values.shape, dtype=idx.values.dtype) * 2

result = divmod(idx, other)
with np.errstate(all='ignore'):
div, mod = divmod(idx.values, other)
expected = Index(div), Index(mod)

expected = Index(div), Index(mod)
for r, e in zip(result, expected):
tm.assert_index_equal(r, e)

@pytest.mark.xfail(reason='GH#19252 Series has no __rdivmod__',
strict=True)
def test_divmod_series(self, numeric_idx):
idx = numeric_idx
other = np.ones(idx.values.shape, dtype=idx.values.dtype) * 2

result = divmod(idx, Series(other))
with np.errstate(all='ignore'):
div, mod = divmod(idx.values, other)
expected = Series(div), Series(mod)

expected = Series(div), Series(mod)
for r, e in zip(result, expected):
tm.assert_series_equal(r, e)

Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,6 @@ def test_td64arr_add_intlike(self, box_df_broadcast_failure):

@pytest.mark.parametrize('scalar', [1, 1.5, np.array(2)])
def test_td64arr_add_sub_numeric_scalar_invalid(self, box, scalar, tdser):

if box is pd.DataFrame and isinstance(scalar, np.ndarray):
# raises ValueError
pytest.xfail(reason="reversed ops return incorrect answers "
"instead of raising.")

tdser = tm.box_expected(tdser, box)
err = TypeError
if box is pd.Index and not isinstance(scalar, float):
Expand Down