From 6853fd39e8338e23b0ac9f860a48f20502cf7eb0 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sun, 21 Oct 2018 15:35:58 -0700 Subject: [PATCH] Implement Series.__rdivmod__, un-xfail tests --- pandas/core/ops.py | 11 ++++++----- pandas/tests/arithmetic/test_numeric.py | 17 +++++++++++------ pandas/tests/arithmetic/test_timedelta64.py | 6 ------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index daa87ad173772..c14e9edcc75d7 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -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 @@ -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), @@ -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) @@ -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 @@ -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 diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index e9316221b125b..25845dd8b3151 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -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) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index fa1a2d9df9a58..14a55785c243b 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -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):