From 2745b299e4edd39cf04fa287b7a4c88de61feba3 Mon Sep 17 00:00:00 2001 From: Nick Becker Date: Wed, 22 Aug 2018 13:58:14 -0400 Subject: [PATCH] Feature/reflected ops noncommutative testing (#1) * np array solution * cleanup * np solution for division * full reflected ops tests --- pygdf/series.py | 24 +++++++++++++++++++++--- pygdf/tests/test_binops.py | 16 ++++------------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/pygdf/series.py b/pygdf/series.py index 65c76f8605b..fb7cf81621f 100644 --- a/pygdf/series.py +++ b/pygdf/series.py @@ -288,7 +288,13 @@ def __sub__(self, other): return self._binaryop(other, 'sub') def __rsub__(self, other): - return self.__sub__(other) + if isinstance(other, (int, float, + np.int32, np.int64, + np.float32, np.float64)): + empty = np.empty(len(self)) + empty.fill(other) + other = Series(empty) + return other.__sub__(self) def __mul__(self, other): return self._binaryop(other, 'mul') @@ -300,13 +306,25 @@ def __floordiv__(self, other): return self._binaryop(other, 'floordiv') def __rfloordiv__(self, other): - return self.__floordiv__(other) + if isinstance(other, (int, float, + np.int32, np.int64, + np.float32, np.float64)): + empty = np.empty(len(self)) + empty.fill(other) + other = Series(empty) + return other.__floordiv__(self) def __truediv__(self, other): return self._binaryop(other, 'truediv') def __rtruediv__(self, other): - return self.__truediv__(other) + if isinstance(other, (int, float, + np.int32, np.int64, + np.float32, np.float64)): + empty = np.empty(len(self)) + empty.fill(other) + other = Series(empty) + return other.__truediv__(self) __div__ = __truediv__ diff --git a/pygdf/tests/test_binops.py b/pygdf/tests/test_binops.py index 3e332cc78b1..f5b2c6a4c6c 100644 --- a/pygdf/tests/test_binops.py +++ b/pygdf/tests/test_binops.py @@ -140,20 +140,17 @@ def test_series_cmpop_mixed_dtype(cmpop, lhs_dtype, rhs_dtype): cmpop(lhs, rhs)) -_commut_ops = [ +reflected_ops = [ lambda x: 1 + x, lambda x: 1 * x, -] - -_noncommut_ops = [ lambda x: 1 - x, lambda x: 1 / x, lambda x: 1 // x, ] -@pytest.mark.parametrize('func, dtype', list(product(_commut_ops, _dtypes))) -def test_commutative_reflected_op_scalar(func, dtype): +@pytest.mark.parametrize('func, dtype', list(product(reflected_ops, _dtypes))) +def test_reflected_ops_scalar(func, dtype): import pandas as pd # create random series @@ -168,9 +165,4 @@ def test_commutative_reflected_op_scalar(func, dtype): ps_result = func(random_series) # verify - np.testing.assert_array_equal(ps_result, gs_result) - - -@pytest.mark.parametrize('func, dtype', list(product(_noncommut_ops, _dtypes))) -def test_noncommutative_reflected_ops(func, dtype): - pass + np.testing.assert_allclose(ps_result, gs_result) \ No newline at end of file