From 1a2b52407b81a999beaf7f7276129a8eb9c5030e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 7 Sep 2018 19:48:04 -0700 Subject: [PATCH] TST: Continue collecting arithmetic tests (#22559) --- pandas/tests/arithmetic/test_numeric.py | 69 +++++++++++++++++++ pandas/tests/arithmetic/test_object.py | 33 +++++++++ .../test_timedelta64.py} | 0 pandas/tests/frame/test_arithmetic.py | 16 ----- pandas/tests/indexes/test_numeric.py | 34 --------- .../indexes/timedeltas/test_arithmetic.py | 32 --------- pandas/tests/series/test_arithmetic.py | 8 --- 7 files changed, 102 insertions(+), 90 deletions(-) rename pandas/tests/{test_arithmetic.py => arithmetic/test_timedelta64.py} (100%) diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 9ede1a62aaf2e..d3957330f11e4 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -42,6 +42,30 @@ def test_operator_series_comparison_zerorank(self): expected = 0.0 > pd.Series([1, 2, 3]) tm.assert_series_equal(result, expected) + def test_df_numeric_cmp_dt64_raises(self): + # GH#8932, GH#22163 + ts = pd.Timestamp.now() + df = pd.DataFrame({'x': range(5)}) + with pytest.raises(TypeError): + df > ts + with pytest.raises(TypeError): + df < ts + with pytest.raises(TypeError): + ts < df + with pytest.raises(TypeError): + ts > df + + assert not (df == ts).any().any() + assert (df != ts).all().all() + + def test_compare_invalid(self): + # GH#8058 + # ops testing + a = pd.Series(np.random.randn(5), name=0) + b = pd.Series(np.random.randn(5)) + b.name = pd.Timestamp('2000-01-01') + tm.assert_series_equal(a / b, 1 / (b / a)) + # ------------------------------------------------------------------ # Numeric dtypes Arithmetic with Timedelta Scalar @@ -754,6 +778,51 @@ def check(series, other): check(tser, 5) +class TestUFuncCompat(object): + @pytest.mark.parametrize('holder', [pd.Int64Index, pd.UInt64Index, + pd.Float64Index, pd.Series]) + def test_ufunc_coercions(self, holder): + idx = holder([1, 2, 3, 4, 5], name='x') + box = pd.Series if holder is pd.Series else pd.Index + + result = np.sqrt(idx) + assert result.dtype == 'f8' and isinstance(result, box) + exp = pd.Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name='x') + exp = tm.box_expected(exp, box) + tm.assert_equal(result, exp) + + result = np.divide(idx, 2.) + assert result.dtype == 'f8' and isinstance(result, box) + exp = pd.Float64Index([0.5, 1., 1.5, 2., 2.5], name='x') + exp = tm.box_expected(exp, box) + tm.assert_equal(result, exp) + + # _evaluate_numeric_binop + result = idx + 2. + assert result.dtype == 'f8' and isinstance(result, box) + exp = pd.Float64Index([3., 4., 5., 6., 7.], name='x') + exp = tm.box_expected(exp, box) + tm.assert_equal(result, exp) + + result = idx - 2. + assert result.dtype == 'f8' and isinstance(result, box) + exp = pd.Float64Index([-1., 0., 1., 2., 3.], name='x') + exp = tm.box_expected(exp, box) + tm.assert_equal(result, exp) + + result = idx * 1. + assert result.dtype == 'f8' and isinstance(result, box) + exp = pd.Float64Index([1., 2., 3., 4., 5.], name='x') + exp = tm.box_expected(exp, box) + tm.assert_equal(result, exp) + + result = idx / 2. + assert result.dtype == 'f8' and isinstance(result, box) + exp = pd.Float64Index([0.5, 1., 1.5, 2., 2.5], name='x') + exp = tm.box_expected(exp, box) + tm.assert_equal(result, exp) + + class TestObjectDtypeEquivalence(object): # Tests that arithmetic operations match operations executed elementwise diff --git a/pandas/tests/arithmetic/test_object.py b/pandas/tests/arithmetic/test_object.py index 2c1cc83c09f88..64d7cbc47fddd 100644 --- a/pandas/tests/arithmetic/test_object.py +++ b/pandas/tests/arithmetic/test_object.py @@ -180,3 +180,36 @@ def test_series_with_dtype_radd_timedelta(self, dtype): result = ser + pd.Timedelta('3 days') tm.assert_series_equal(result, expected) + + # TODO: cleanup & parametrize over box + def test_mixed_timezone_series_ops_object(self): + # GH#13043 + ser = pd.Series([pd.Timestamp('2015-01-01', tz='US/Eastern'), + pd.Timestamp('2015-01-01', tz='Asia/Tokyo')], + name='xxx') + assert ser.dtype == object + + exp = pd.Series([pd.Timestamp('2015-01-02', tz='US/Eastern'), + pd.Timestamp('2015-01-02', tz='Asia/Tokyo')], + name='xxx') + tm.assert_series_equal(ser + pd.Timedelta('1 days'), exp) + tm.assert_series_equal(pd.Timedelta('1 days') + ser, exp) + + # object series & object series + ser2 = pd.Series([pd.Timestamp('2015-01-03', tz='US/Eastern'), + pd.Timestamp('2015-01-05', tz='Asia/Tokyo')], + name='xxx') + assert ser2.dtype == object + exp = pd.Series([pd.Timedelta('2 days'), pd.Timedelta('4 days')], + name='xxx') + tm.assert_series_equal(ser2 - ser, exp) + tm.assert_series_equal(ser - ser2, -exp) + + ser = pd.Series([pd.Timedelta('01:00:00'), pd.Timedelta('02:00:00')], + name='xxx', dtype=object) + assert ser.dtype == object + + exp = pd.Series([pd.Timedelta('01:30:00'), pd.Timedelta('02:30:00')], + name='xxx') + tm.assert_series_equal(ser + pd.Timedelta('00:30:00'), exp) + tm.assert_series_equal(pd.Timedelta('00:30:00') + ser, exp) diff --git a/pandas/tests/test_arithmetic.py b/pandas/tests/arithmetic/test_timedelta64.py similarity index 100% rename from pandas/tests/test_arithmetic.py rename to pandas/tests/arithmetic/test_timedelta64.py diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index f142f770a0c54..a6f4e0e38ec5d 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -48,22 +48,6 @@ def test_mixed_comparison(self): result = df != other assert result.all().all() - def test_df_numeric_cmp_dt64_raises(self): - # GH#8932, GH#22163 - ts = pd.Timestamp.now() - df = pd.DataFrame({'x': range(5)}) - with pytest.raises(TypeError): - df > ts - with pytest.raises(TypeError): - df < ts - with pytest.raises(TypeError): - ts < df - with pytest.raises(TypeError): - ts > df - - assert not (df == ts).any().any() - assert (df != ts).all().all() - def test_df_boolean_comparison_error(self): # GH#4576 # boolean comparisons with a tuple/list give unexpected results diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index c8aa7f8fd50fd..1cb2cd46a65db 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -565,40 +565,6 @@ def test_slice_keep_name(self): idx = self._holder([1, 2], name='asdf') assert idx.name == idx[1:].name - def test_ufunc_coercions(self): - idx = self._holder([1, 2, 3, 4, 5], name='x') - - result = np.sqrt(idx) - assert isinstance(result, Float64Index) - exp = Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name='x') - tm.assert_index_equal(result, exp) - - result = np.divide(idx, 2.) - assert isinstance(result, Float64Index) - exp = Float64Index([0.5, 1., 1.5, 2., 2.5], name='x') - tm.assert_index_equal(result, exp) - - # _evaluate_numeric_binop - result = idx + 2. - assert isinstance(result, Float64Index) - exp = Float64Index([3., 4., 5., 6., 7.], name='x') - tm.assert_index_equal(result, exp) - - result = idx - 2. - assert isinstance(result, Float64Index) - exp = Float64Index([-1., 0., 1., 2., 3.], name='x') - tm.assert_index_equal(result, exp) - - result = idx * 1. - assert isinstance(result, Float64Index) - exp = Float64Index([1., 2., 3., 4., 5.], name='x') - tm.assert_index_equal(result, exp) - - result = idx / 2. - assert isinstance(result, Float64Index) - exp = Float64Index([0.5, 1., 1.5, 2., 2.5], name='x') - tm.assert_index_equal(result, exp) - class TestInt64Index(NumericInt): _dtype = 'int64' diff --git a/pandas/tests/indexes/timedeltas/test_arithmetic.py b/pandas/tests/indexes/timedeltas/test_arithmetic.py index f3bc523ca525e..e425937fedf4b 100644 --- a/pandas/tests/indexes/timedeltas/test_arithmetic.py +++ b/pandas/tests/indexes/timedeltas/test_arithmetic.py @@ -430,38 +430,6 @@ def test_ops_ndarray(self): if LooseVersion(np.__version__) >= LooseVersion('1.8'): tm.assert_numpy_array_equal(other - td, expected) - def test_ops_series_object(self): - # GH 13043 - s = pd.Series([pd.Timestamp('2015-01-01', tz='US/Eastern'), - pd.Timestamp('2015-01-01', tz='Asia/Tokyo')], - name='xxx') - assert s.dtype == object - - exp = pd.Series([pd.Timestamp('2015-01-02', tz='US/Eastern'), - pd.Timestamp('2015-01-02', tz='Asia/Tokyo')], - name='xxx') - tm.assert_series_equal(s + pd.Timedelta('1 days'), exp) - tm.assert_series_equal(pd.Timedelta('1 days') + s, exp) - - # object series & object series - s2 = pd.Series([pd.Timestamp('2015-01-03', tz='US/Eastern'), - pd.Timestamp('2015-01-05', tz='Asia/Tokyo')], - name='xxx') - assert s2.dtype == object - exp = pd.Series([pd.Timedelta('2 days'), pd.Timedelta('4 days')], - name='xxx') - tm.assert_series_equal(s2 - s, exp) - tm.assert_series_equal(s - s2, -exp) - - s = pd.Series([pd.Timedelta('01:00:00'), pd.Timedelta('02:00:00')], - name='xxx', dtype=object) - assert s.dtype == object - - exp = pd.Series([pd.Timedelta('01:30:00'), pd.Timedelta('02:30:00')], - name='xxx') - tm.assert_series_equal(s + pd.Timedelta('00:30:00'), exp) - tm.assert_series_equal(pd.Timedelta('00:30:00') + s, exp) - def test_timedelta_ops_with_missing_values(self): # setup s1 = pd.to_timedelta(Series(['00:00:01'])) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 41064b84abc36..37ba1c91368b3 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- import operator -import numpy as np import pytest from pandas import Series @@ -14,13 +13,6 @@ # Comparisons class TestSeriesComparison(object): - def test_compare_invalid(self): - # GH#8058 - # ops testing - a = pd.Series(np.random.randn(5), name=0) - b = pd.Series(np.random.randn(5)) - b.name = pd.Timestamp('2000-01-01') - tm.assert_series_equal(a / b, 1 / (b / a)) @pytest.mark.parametrize('opname', ['eq', 'ne', 'gt', 'lt', 'ge', 'le']) def test_ser_flex_cmp_return_dtypes(self, opname):