From 9f8fa5b6b7e63b5ca272c15dc6297367b2e99ecd Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Jan 2018 17:06:07 -0800 Subject: [PATCH 1/3] Separate test_numeric_compat into method-specific tests --- pandas/tests/indexes/test_numeric.py | 82 ++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 17 deletions(-) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 1ce8ade50c071..0b774ea944b22 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -29,20 +29,21 @@ def full_like(array, value): class Numeric(Base): def test_numeric_compat(self): + pass # override Base method + def test_mul_int(self): idx = self.create_index() - didx = idx * idx - result = idx * 1 tm.assert_index_equal(result, idx) + def test_rmul_int(self): + idx = self.create_index() + result = 1 * idx tm.assert_index_equal(result, idx) - # in general not true for RangeIndex - if not isinstance(idx, RangeIndex): - result = idx * idx - tm.assert_index_equal(result, idx ** 2) + def test_div_int(self): + idx = self.create_index() # truediv under PY3 result = idx / 1 @@ -57,9 +58,16 @@ def test_numeric_compat(self): expected = Index(idx.values / 2) tm.assert_index_equal(result, expected) + def test_floordiv_int(self): + idx = self.create_index() + result = idx // 1 tm.assert_index_equal(result, idx) + def test_mul_int_array(self): + idx = self.create_index() + didx = idx * idx + result = idx * np.array(5, dtype='int64') tm.assert_index_equal(result, idx * 5) @@ -67,19 +75,45 @@ def test_numeric_compat(self): result = idx * np.arange(5, dtype=arr_dtype) tm.assert_index_equal(result, didx) + def test_mul_int_series(self): + idx = self.create_index() + didx = idx * idx + + arr_dtype = 'uint64' if isinstance(idx, UInt64Index) else 'int64' result = idx * Series(np.arange(5, dtype=arr_dtype)) - tm.assert_series_equal(result, Series(didx)) + tm.assert_index_equal(result, didx) + def test_mul_float_series(self): + idx = self.create_index() rng5 = np.arange(5, dtype='float64') + result = idx * Series(rng5 + 0.1) - expected = Series(rng5 * (rng5 + 0.1)) - tm.assert_series_equal(result, expected) + expected = Float64Index(rng5 * (rng5 + 0.1)) + tm.assert_index_equal(result, expected) - # invalid - pytest.raises(TypeError, - lambda: idx * date_range('20130101', periods=5)) - pytest.raises(ValueError, lambda: idx * idx[0:3]) - pytest.raises(ValueError, lambda: idx * np.array([1, 2])) + def test_mul_index(self): + idx = self.create_index() + + # in general not true for RangeIndex + if not isinstance(idx, RangeIndex): + result = idx * idx + tm.assert_index_equal(result, idx ** 2) + + def test_mul_datelike_raises(self): + idx = self.create_index() + with pytest.raises(TypeError): + idx * date_range('20130101', periods=5) + + def test_mul_size_mismatch_raises(self): + idx = self.create_index() + + with pytest.raises(ValueError): + idx * idx[0:3] + with pytest.raises(ValueError): + idx * np.array([1, 2]) + + def test_divmod(self): + idx = self.create_index() result = divmod(idx, 2) with np.errstate(all='ignore'): @@ -95,15 +129,29 @@ def test_numeric_compat(self): for r, e in zip(result, expected): tm.assert_index_equal(r, e) + result = divmod(idx, Series(full_like(idx.values, 2))) + with np.errstate(all='ignore'): + div, mod = divmod(idx.values, full_like(idx.values, 2)) + expected = Index(div), Index(mod) + for r, e in zip(result, expected): + tm.assert_index_equal(r, e) + + def test_pow_float(self): # test power calculations both ways, GH 14973 - expected = pd.Float64Index(2.0**idx.values) - result = 2.0**idx - tm.assert_index_equal(result, expected) + idx = self.create_index() expected = pd.Float64Index(idx.values**2.0) result = idx**2.0 tm.assert_index_equal(result, expected) + def test_rpow_float(self): + # test power calculations both ways, GH 14973 + idx = self.create_index() + + expected = pd.Float64Index(2.0**idx.values) + result = 2.0**idx + tm.assert_index_equal(result, expected) + @pytest.mark.xfail(reason='GH#19252 Series has no __rdivmod__') def test_divmod_series(self): idx = self.create_index() From 5883636be945f2ca3b5050ca0d6d201b6e8e3bb7 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Jan 2018 21:32:18 -0800 Subject: [PATCH 2/3] update fixed index-->series --- pandas/tests/indexes/test_numeric.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 0b774ea944b22..15a9c36af6b4b 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -88,8 +88,8 @@ def test_mul_float_series(self): rng5 = np.arange(5, dtype='float64') result = idx * Series(rng5 + 0.1) - expected = Float64Index(rng5 * (rng5 + 0.1)) - tm.assert_index_equal(result, expected) + expected = Series(rng5 * (rng5 + 0.1)) + tm.assert_series_equal(result, expected) def test_mul_index(self): idx = self.create_index() From 3df8a1f97b052f2268d74e68959c04fa8e769724 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Jan 2018 22:23:43 -0800 Subject: [PATCH 3/3] update assert_index-->assert_series --- pandas/tests/indexes/test_numeric.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 15a9c36af6b4b..3de1c4c982654 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -81,7 +81,7 @@ def test_mul_int_series(self): arr_dtype = 'uint64' if isinstance(idx, UInt64Index) else 'int64' result = idx * Series(np.arange(5, dtype=arr_dtype)) - tm.assert_index_equal(result, didx) + tm.assert_series_equal(result, Series(didx)) def test_mul_float_series(self): idx = self.create_index() @@ -129,13 +129,6 @@ def test_divmod(self): for r, e in zip(result, expected): tm.assert_index_equal(r, e) - result = divmod(idx, Series(full_like(idx.values, 2))) - with np.errstate(all='ignore'): - div, mod = divmod(idx.values, full_like(idx.values, 2)) - expected = Index(div), Index(mod) - for r, e in zip(result, expected): - tm.assert_index_equal(r, e) - def test_pow_float(self): # test power calculations both ways, GH 14973 idx = self.create_index()