From d1fecdb710e505448961737b717526f9023dd684 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Oct 2018 09:24:05 -0700 Subject: [PATCH 1/7] parametrize --- pandas/tests/arithmetic/test_datetime64.py | 59 +++++++++++----------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 36bb0aca066fb..71eba1e6901a1 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -323,32 +323,35 @@ def test_dti_cmp_null_scalar_inequality(self, tz_naive_fixture, other): with pytest.raises(TypeError): dti >= other - def test_dti_cmp_nat(self): + @pytest.mark.parametrize('dtype', [None, object]) + def test_dti_cmp_nat(self, dtype): left = pd.DatetimeIndex([pd.Timestamp('2011-01-01'), pd.NaT, pd.Timestamp('2011-01-03')]) right = pd.DatetimeIndex([pd.NaT, pd.NaT, pd.Timestamp('2011-01-03')]) - for lhs, rhs in [(left, right), - (left.astype(object), right.astype(object))]: - result = rhs == lhs - expected = np.array([False, False, True]) - tm.assert_numpy_array_equal(result, expected) + lhs, rhs = left, right + if dtype is object: + lhs, rhs = left.astype(object), right.astype(object) + + result = rhs == lhs + expected = np.array([False, False, True]) + tm.assert_numpy_array_equal(result, expected) - result = lhs != rhs - expected = np.array([True, True, False]) - tm.assert_numpy_array_equal(result, expected) + result = lhs != rhs + expected = np.array([True, True, False]) + tm.assert_numpy_array_equal(result, expected) - expected = np.array([False, False, False]) - tm.assert_numpy_array_equal(lhs == pd.NaT, expected) - tm.assert_numpy_array_equal(pd.NaT == rhs, expected) + expected = np.array([False, False, False]) + tm.assert_numpy_array_equal(lhs == pd.NaT, expected) + tm.assert_numpy_array_equal(pd.NaT == rhs, expected) - expected = np.array([True, True, True]) - tm.assert_numpy_array_equal(lhs != pd.NaT, expected) - tm.assert_numpy_array_equal(pd.NaT != lhs, expected) + expected = np.array([True, True, True]) + tm.assert_numpy_array_equal(lhs != pd.NaT, expected) + tm.assert_numpy_array_equal(pd.NaT != lhs, expected) - expected = np.array([False, False, False]) - tm.assert_numpy_array_equal(lhs < pd.NaT, expected) - tm.assert_numpy_array_equal(pd.NaT > lhs, expected) + expected = np.array([False, False, False]) + tm.assert_numpy_array_equal(lhs < pd.NaT, expected) + tm.assert_numpy_array_equal(pd.NaT > lhs, expected) def test_dti_cmp_nat_behaves_like_float_cmp_nan(self): fidx1 = pd.Index([1.0, np.nan, 3.0, np.nan, 5.0, 7.0]) @@ -901,13 +904,15 @@ def test_dt64_series_add_intlike(self, tz, op): other = Series([20, 30, 40], dtype='uint8') - pytest.raises(TypeError, getattr(ser, op), 1) - - pytest.raises(TypeError, getattr(ser, op), other) - - pytest.raises(TypeError, getattr(ser, op), other.values) - - pytest.raises(TypeError, getattr(ser, op), pd.Index(other)) + method = getattr(ser, op) + with pytest.raises(TypeError): + method(1) + with pytest.raises(TypeError): + method(other) + with pytest.raises(TypeError): + method(other.values) + with pytest.raises(TypeError): + method(pd.Index(other)) # ------------------------------------------------------------- # Timezone-Centric Tests @@ -994,10 +999,6 @@ def test_dti_add_timestamp_raises(self, box): msg = "cannot add" with tm.assert_raises_regex(TypeError, msg): idx + Timestamp('2011-01-01') - - def test_dti_radd_timestamp_raises(self): - idx = DatetimeIndex(['2011-01-01', '2011-01-02']) - msg = "cannot add DatetimeIndex and Timestamp" with tm.assert_raises_regex(TypeError, msg): Timestamp('2011-01-01') + idx From d251b6e854912c2bafc269bf462bb2ad57cb6380 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Oct 2018 09:34:48 -0700 Subject: [PATCH 2/7] De-duplicate/parametrize --- pandas/tests/arithmetic/test_period.py | 156 +++++++++---------------- 1 file changed, 58 insertions(+), 98 deletions(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index fe98b74499983..d7c2d643b5584 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -30,61 +30,80 @@ def test_pi_cmp_period(self): tm.assert_numpy_array_equal(result, exp) @pytest.mark.parametrize('freq', ['M', '2M', '3M']) - def test_pi_cmp_pi(self, freq): + def test_parr_cmp_period_scalar(self, freq, box): + # GH#13200 base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], freq=freq) + base = tm.box_expected(base, box) per = Period('2011-02', freq=freq) exp = np.array([False, True, False, False]) - tm.assert_numpy_array_equal(base == per, exp) - tm.assert_numpy_array_equal(per == base, exp) + tm.assert_equal(base == per, exp) + tm.assert_equal(per == base, exp) exp = np.array([True, False, True, True]) - tm.assert_numpy_array_equal(base != per, exp) - tm.assert_numpy_array_equal(per != base, exp) + tm.assert_equal(base != per, exp) + tm.assert_equal(per != base, exp) exp = np.array([False, False, True, True]) - tm.assert_numpy_array_equal(base > per, exp) - tm.assert_numpy_array_equal(per < base, exp) + tm.assert_equal(base > per, exp) + tm.assert_equal(per < base, exp) exp = np.array([True, False, False, False]) - tm.assert_numpy_array_equal(base < per, exp) - tm.assert_numpy_array_equal(per > base, exp) + tm.assert_equal(base < per, exp) + tm.assert_equal(per > base, exp) exp = np.array([False, True, True, True]) - tm.assert_numpy_array_equal(base >= per, exp) - tm.assert_numpy_array_equal(per <= base, exp) + tm.assert_equal(base >= per, exp) + tm.assert_equal(per <= base, exp) exp = np.array([True, True, False, False]) - tm.assert_numpy_array_equal(base <= per, exp) - tm.assert_numpy_array_equal(per >= base, exp) + tm.assert_equal(base <= per, exp) + tm.assert_equal(per >= base, exp) + + @pytest.mark.parametrize('freq', ['M', '2M', '3M']) + def test_parr_cmp_pi(self, freq, box): + # GH#13200 + xbox = np.ndarray if box is pd.Index else box + + base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], + freq=freq) + base = tm.box_expected(base, box) + # TODO: could also box idx? idx = PeriodIndex(['2011-02', '2011-01', '2011-03', '2011-05'], freq=freq) exp = np.array([False, False, True, False]) - tm.assert_numpy_array_equal(base == idx, exp) + exp = tm.box_expected(exp, xbox) + tm.assert_equal(base == idx, exp) exp = np.array([True, True, False, True]) - tm.assert_numpy_array_equal(base != idx, exp) + exp = tm.box_expected(exp, xbox) + tm.assert_equal(base != idx, exp) exp = np.array([False, True, False, False]) - tm.assert_numpy_array_equal(base > idx, exp) + exp = tm.box_expected(exp, xbox) + tm.assert_equal(base > idx, exp) exp = np.array([True, False, False, True]) - tm.assert_numpy_array_equal(base < idx, exp) + exp = tm.box_expected(exp, xbox) + tm.assert_equal(base < idx, exp) exp = np.array([False, True, True, False]) - tm.assert_numpy_array_equal(base >= idx, exp) + exp = tm.box_expected(exp, xbox) + tm.assert_equal(base >= idx, exp) exp = np.array([True, False, True, True]) - tm.assert_numpy_array_equal(base <= idx, exp) + exp = tm.box_expected(exp, xbox) + tm.assert_equal(base <= idx, exp) @pytest.mark.parametrize('freq', ['M', '2M', '3M']) - def test_pi_cmp_pi_mismatched_freq_raises(self, freq): + def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box): # different base freq base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], freq=freq) + base = tm.box_expected(base, box) msg = "Input has different freq=A-DEC from PeriodIndex" with tm.assert_raises_regex(period.IncompatibleFrequency, msg): @@ -197,44 +216,6 @@ def test_comp_nat(self, dtype): class TestPeriodSeriesComparisons(object): - @pytest.mark.parametrize('freq', ['M', '2M', '3M']) - def test_cmp_series_period_scalar(self, freq): - # GH 13200 - base = Series([Period(x, freq=freq) for x in - ['2011-01', '2011-02', '2011-03', '2011-04']]) - p = Period('2011-02', freq=freq) - - exp = Series([False, True, False, False]) - tm.assert_series_equal(base == p, exp) - tm.assert_series_equal(p == base, exp) - - exp = Series([True, False, True, True]) - tm.assert_series_equal(base != p, exp) - tm.assert_series_equal(p != base, exp) - - exp = Series([False, False, True, True]) - tm.assert_series_equal(base > p, exp) - tm.assert_series_equal(p < base, exp) - - exp = Series([True, False, False, False]) - tm.assert_series_equal(base < p, exp) - tm.assert_series_equal(p > base, exp) - - exp = Series([False, True, True, True]) - tm.assert_series_equal(base >= p, exp) - tm.assert_series_equal(p <= base, exp) - - exp = Series([True, True, False, False]) - tm.assert_series_equal(base <= p, exp) - tm.assert_series_equal(p >= base, exp) - - # different base freq - msg = "Input has different freq=A-DEC from Period" - with tm.assert_raises_regex(IncompatibleFrequency, msg): - base <= Period('2011', freq='A') - - with tm.assert_raises_regex(IncompatibleFrequency, msg): - Period('2011', freq='A') >= base @pytest.mark.parametrize('freq', ['M', '2M', '3M']) def test_cmp_series_period_series(self, freq): @@ -242,27 +223,6 @@ def test_cmp_series_period_series(self, freq): base = Series([Period(x, freq=freq) for x in ['2011-01', '2011-02', '2011-03', '2011-04']]) - ser = Series([Period(x, freq=freq) for x in - ['2011-02', '2011-01', '2011-03', '2011-05']]) - - exp = Series([False, False, True, False]) - tm.assert_series_equal(base == ser, exp) - - exp = Series([True, True, False, True]) - tm.assert_series_equal(base != ser, exp) - - exp = Series([False, True, False, False]) - tm.assert_series_equal(base > ser, exp) - - exp = Series([True, False, False, True]) - tm.assert_series_equal(base < ser, exp) - - exp = Series([False, True, True, False]) - tm.assert_series_equal(base >= ser, exp) - - exp = Series([True, False, True, True]) - tm.assert_series_equal(base <= ser, exp) - ser2 = Series([Period(x, freq='A') for x in ['2011', '2011', '2011', '2011']]) @@ -405,9 +365,10 @@ def test_parr_sub_pi_mismatched_freq(self, box_df_broadcast_failure): @pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])]) @pytest.mark.parametrize('op', [operator.add, ops.radd, operator.sub, ops.rsub]) - def test_pi_add_sub_float(self, op, other): + def test_pi_add_sub_float(self, op, other, box): dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') pi = dti.to_period('D') + pi = tm.box_expected(pi, box) with pytest.raises(TypeError): op(pi, other) @@ -874,35 +835,34 @@ def test_pi_ops(self): tm.assert_index_equal(result, exp) @pytest.mark.parametrize('ng', ["str", 1.5]) - def test_pi_ops_errors(self, ng): + def test_pi_ops_errors(self, ng, box): idx = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], freq='M', name='idx') - ser = pd.Series(idx) + obj = tm.box_expected(idx, box) msg = r"unsupported operand type\(s\)" - for obj in [idx, ser]: - with tm.assert_raises_regex(TypeError, msg): - obj + ng + with tm.assert_raises_regex(TypeError, msg): + obj + ng - with pytest.raises(TypeError): - # error message differs between PY2 and 3 - ng + obj + with pytest.raises(TypeError): + # error message differs between PY2 and 3 + ng + obj - with tm.assert_raises_regex(TypeError, msg): - obj - ng + with tm.assert_raises_regex(TypeError, msg): + obj - ng - with pytest.raises(TypeError): - np.add(obj, ng) + with pytest.raises(TypeError): + np.add(obj, ng) - with pytest.raises(TypeError): - np.add(ng, obj) + with pytest.raises(TypeError): + np.add(ng, obj) - with pytest.raises(TypeError): - np.subtract(obj, ng) + with pytest.raises(TypeError): + np.subtract(obj, ng) - with pytest.raises(TypeError): - np.subtract(ng, obj) + with pytest.raises(TypeError): + np.subtract(ng, obj) def test_pi_ops_nat(self): idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'], From 33ff3f756bbcd90c2ce775b12129c7492dd7770b Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Oct 2018 09:40:33 -0700 Subject: [PATCH 3/7] parametrize/de-duplicate --- pandas/tests/arithmetic/test_period.py | 6 +- pandas/tests/arithmetic/test_timedelta64.py | 69 ++++++++------------- 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index d7c2d643b5584..06f42c4ff5176 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -803,11 +803,7 @@ class TestPeriodIndexSeriesMethods(object): def _check(self, values, func, expected): idx = pd.PeriodIndex(values) result = func(idx) - if isinstance(expected, pd.Index): - tm.assert_index_equal(result, expected) - else: - # comp op results in bool - tm.assert_numpy_array_equal(result, expected) + tm.assert_equal(result, expected) ser = pd.Series(values) result = func(ser) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index fa1a2d9df9a58..3af9e7c34a5c4 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -45,32 +45,35 @@ def test_tdi_cmp_str_invalid(self): with pytest.raises(TypeError): left != right - def test_comp_nat(self): + @pytest.mark.parametrize('dtype', [None, object]) + def test_comp_nat(self, dtype): left = pd.TimedeltaIndex([pd.Timedelta('1 days'), pd.NaT, pd.Timedelta('3 days')]) right = pd.TimedeltaIndex([pd.NaT, pd.NaT, pd.Timedelta('3 days')]) - for lhs, rhs in [(left, right), - (left.astype(object), right.astype(object))]: - result = rhs == lhs - expected = np.array([False, False, True]) - tm.assert_numpy_array_equal(result, expected) + lhs, rhs = left, right + if dtpye is object: + lhs, rhs = left.astype(object), right.astype(object) - result = rhs != lhs - expected = np.array([True, True, False]) - tm.assert_numpy_array_equal(result, expected) + result = rhs == lhs + expected = np.array([False, False, True]) + tm.assert_numpy_array_equal(result, expected) - expected = np.array([False, False, False]) - tm.assert_numpy_array_equal(lhs == pd.NaT, expected) - tm.assert_numpy_array_equal(pd.NaT == rhs, expected) + result = rhs != lhs + expected = np.array([True, True, False]) + tm.assert_numpy_array_equal(result, expected) + + expected = np.array([False, False, False]) + tm.assert_numpy_array_equal(lhs == pd.NaT, expected) + tm.assert_numpy_array_equal(pd.NaT == rhs, expected) - expected = np.array([True, True, True]) - tm.assert_numpy_array_equal(lhs != pd.NaT, expected) - tm.assert_numpy_array_equal(pd.NaT != lhs, expected) + expected = np.array([True, True, True]) + tm.assert_numpy_array_equal(lhs != pd.NaT, expected) + tm.assert_numpy_array_equal(pd.NaT != lhs, expected) - expected = np.array([False, False, False]) - tm.assert_numpy_array_equal(lhs < pd.NaT, expected) - tm.assert_numpy_array_equal(pd.NaT > lhs, expected) + expected = np.array([False, False, False]) + tm.assert_numpy_array_equal(lhs < pd.NaT, expected) + tm.assert_numpy_array_equal(pd.NaT > lhs, expected) def test_comparisons_nat(self): tdidx1 = pd.TimedeltaIndex(['1 day', pd.NaT, '1 day 00:00:01', pd.NaT, @@ -425,14 +428,6 @@ def test_td64arr_add_timestamp(self, box): result = idx + Timestamp('2011-01-01') tm.assert_equal(result, expected) - def test_td64_radd_timestamp(self, box): - idx = TimedeltaIndex(['1 day', '2 day']) - expected = DatetimeIndex(['2011-01-02', '2011-01-03']) - - idx = tm.box_expected(idx, box) - expected = tm.box_expected(expected, box) - - # TODO: parametrize over scalar datetime types? result = Timestamp('2011-01-01') + idx tm.assert_equal(result, expected) @@ -500,26 +495,16 @@ def test_tdi_add_dt64_array(self, box_df_broadcast_failure): def test_td64arr_add_int_series_invalid(self, box, tdser): tdser = tm.box_expected(tdser, box) err = TypeError if box is not pd.Index else NullFrequencyError - with pytest.raises(err): - tdser + Series([2, 3, 4]) + int_ser = Series([2, 3, 4]) - def test_td64arr_radd_int_series_invalid(self, box, tdser): - tdser = tm.box_expected(tdser, box) - err = TypeError if box is not pd.Index else NullFrequencyError with pytest.raises(err): - Series([2, 3, 4]) + tdser - - def test_td64arr_sub_int_series_invalid(self, box, tdser): - tdser = tm.box_expected(tdser, box) - err = TypeError if box is not pd.Index else NullFrequencyError + tdser + int_ser with pytest.raises(err): - tdser - Series([2, 3, 4]) - - def test_td64arr_rsub_int_series_invalid(self, box, tdser): - tdser = tm.box_expected(tdser, box) - err = TypeError if box is not pd.Index else NullFrequencyError + int_ser + tdser + with pytest.raises(err): + tdser - int_ser with pytest.raises(err): - Series([2, 3, 4]) - tdser + int_ser - tdser def test_td64arr_add_intlike(self, box_df_broadcast_failure): # GH#19123 From 80b07db69d1d6a8119a14771b67ad3f1c718de17 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Oct 2018 16:17:35 -0700 Subject: [PATCH 4/7] xfail where appropriate --- pandas/tests/arithmetic/test_period.py | 18 ++++++++++++++---- pandas/tests/arithmetic/test_timedelta64.py | 2 +- pandas/util/testing.py | 2 ++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 06f42c4ff5176..c14dd2cdeded8 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -32,38 +32,47 @@ def test_pi_cmp_period(self): @pytest.mark.parametrize('freq', ['M', '2M', '3M']) def test_parr_cmp_period_scalar(self, freq, box): # GH#13200 + xbox = np.ndarray if box is pd.Index else box + base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], freq=freq) base = tm.box_expected(base, box) per = Period('2011-02', freq=freq) exp = np.array([False, True, False, False]) + exp = tm.box_expected(exp, xbox) tm.assert_equal(base == per, exp) tm.assert_equal(per == base, exp) exp = np.array([True, False, True, True]) + exp = tm.box_expected(exp, xbox) tm.assert_equal(base != per, exp) tm.assert_equal(per != base, exp) exp = np.array([False, False, True, True]) + exp = tm.box_expected(exp, xbox) tm.assert_equal(base > per, exp) tm.assert_equal(per < base, exp) exp = np.array([True, False, False, False]) + exp = tm.box_expected(exp, xbox) tm.assert_equal(base < per, exp) tm.assert_equal(per > base, exp) exp = np.array([False, True, True, True]) + exp = tm.box_expected(exp, xbox) tm.assert_equal(base >= per, exp) tm.assert_equal(per <= base, exp) exp = np.array([True, True, False, False]) + exp = tm.box_expected(exp, xbox) tm.assert_equal(base <= per, exp) tm.assert_equal(per >= base, exp) @pytest.mark.parametrize('freq', ['M', '2M', '3M']) - def test_parr_cmp_pi(self, freq, box): + def test_parr_cmp_pi(self, freq, box_df_fail): # GH#13200 + box = box_df_fail xbox = np.ndarray if box is pd.Index else box base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], @@ -105,7 +114,7 @@ def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box): freq=freq) base = tm.box_expected(base, box) - msg = "Input has different freq=A-DEC from PeriodIndex" + msg = "Input has different freq=A-DEC from " with tm.assert_raises_regex(period.IncompatibleFrequency, msg): base <= Period('2011', freq='A') @@ -117,7 +126,7 @@ def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box): base <= idx # Different frequency - msg = "Input has different freq=4M from PeriodIndex" + msg = "Input has different freq=4M from " with tm.assert_raises_regex(period.IncompatibleFrequency, msg): base <= Period('2011', freq='4M') @@ -365,7 +374,8 @@ def test_parr_sub_pi_mismatched_freq(self, box_df_broadcast_failure): @pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])]) @pytest.mark.parametrize('op', [operator.add, ops.radd, operator.sub, ops.rsub]) - def test_pi_add_sub_float(self, op, other, box): + def test_pi_add_sub_float(self, op, other, box_df_broadcast_failure): + box = box_df_broadcast_failure dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') pi = dti.to_period('D') pi = tm.box_expected(pi, box) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 3af9e7c34a5c4..2d118d8528393 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -52,7 +52,7 @@ def test_comp_nat(self, dtype): right = pd.TimedeltaIndex([pd.NaT, pd.NaT, pd.Timedelta('3 days')]) lhs, rhs = left, right - if dtpye is object: + if dtype is object: lhs, rhs = left.astype(object), right.astype(object) result = rhs == lhs diff --git a/pandas/util/testing.py b/pandas/util/testing.py index b5ec0912c5c26..4327ff42b6044 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -1563,6 +1563,8 @@ def box_expected(expected, box_cls): expected = pd.Series(expected) elif box_cls is pd.DataFrame: expected = pd.Series(expected).to_frame() + elif box_cls is np.ndarray: + expected = np.array(expected) else: raise NotImplementedError(box_cls) return expected From c6077746977c39373bdcdffd52f5d1f9133a5da6 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Oct 2018 17:10:09 -0700 Subject: [PATCH 5/7] de-duplicate and xfail --- pandas/tests/arithmetic/test_period.py | 27 +++++++++----------------- 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index c14dd2cdeded8..716bb4eced3ce 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -108,8 +108,11 @@ def test_parr_cmp_pi(self, freq, box_df_fail): tm.assert_equal(base <= idx, exp) @pytest.mark.parametrize('freq', ['M', '2M', '3M']) - def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box): + def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box_df_fail): + # GH#13200 # different base freq + box = box_df_fail + base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], freq=freq) base = tm.box_expected(base, box) @@ -121,6 +124,7 @@ def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box): with tm.assert_raises_regex(period.IncompatibleFrequency, msg): Period('2011', freq='A') >= base + # TODO: Could parametrize over boxes for idx? idx = PeriodIndex(['2011', '2012', '2013', '2014'], freq='A') with tm.assert_raises_regex(period.IncompatibleFrequency, msg): base <= idx @@ -225,21 +229,6 @@ def test_comp_nat(self, dtype): class TestPeriodSeriesComparisons(object): - - @pytest.mark.parametrize('freq', ['M', '2M', '3M']) - def test_cmp_series_period_series(self, freq): - # GH#13200 - base = Series([Period(x, freq=freq) for x in - ['2011-01', '2011-02', '2011-03', '2011-04']]) - - ser2 = Series([Period(x, freq='A') for x in - ['2011', '2011', '2011', '2011']]) - - # different base freq - msg = "Input has different freq=A-DEC from Period" - with tm.assert_raises_regex(IncompatibleFrequency, msg): - base <= ser2 - def test_cmp_series_period_series_mixed_freq(self): # GH#13200 base = Series([Period('2011', freq='A'), @@ -374,8 +363,10 @@ def test_parr_sub_pi_mismatched_freq(self, box_df_broadcast_failure): @pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])]) @pytest.mark.parametrize('op', [operator.add, ops.radd, operator.sub, ops.rsub]) - def test_pi_add_sub_float(self, op, other, box_df_broadcast_failure): - box = box_df_broadcast_failure + def test_pi_add_sub_float(self, op, other, box): + if box is pd.DataFrame and isinstance(other, np.ndarray): + pytest.xfail(reason="Tries to broadcast incorrectly") + dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') pi = dti.to_period('D') pi = tm.box_expected(pi, box) From f2daf23ff80ede7ffe15696654a6216c1fd459b8 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Oct 2018 17:16:39 -0700 Subject: [PATCH 6/7] de-duplicate --- pandas/tests/arithmetic/test_period.py | 30 ++++++-------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 716bb4eced3ce..91db9b176536f 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -640,7 +640,7 @@ def test_pi_sub_isub_timedeltalike_daily(self, three_days): rng -= other tm.assert_index_equal(rng, expected) - def test_pi_add_iadd_timedeltalike_freq_mismatch_daily(self, not_daily): + def test_pi_add_sub_timedeltalike_freq_mismatch_daily(self, not_daily): other = not_daily rng = pd.period_range('2014-05-01', '2014-05-15', freq='D') msg = 'Input has different freq(=.+)? from PeriodIndex\\(freq=D\\)' @@ -648,13 +648,10 @@ def test_pi_add_iadd_timedeltalike_freq_mismatch_daily(self, not_daily): rng + other with tm.assert_raises_regex(period.IncompatibleFrequency, msg): rng += other - - def test_pi_sub_timedeltalike_freq_mismatch_daily(self, not_daily): - other = not_daily - rng = pd.period_range('2014-05-01', '2014-05-15', freq='D') - msg = 'Input has different freq(=.+)? from PeriodIndex\\(freq=D\\)' with tm.assert_raises_regex(period.IncompatibleFrequency, msg): rng - other + with tm.assert_raises_regex(period.IncompatibleFrequency, msg): + rng -= other def test_pi_add_iadd_timedeltalike_hourly(self, two_hours): other = two_hours @@ -701,8 +698,8 @@ def test_add_iadd_timedeltalike_annual(self): rng += pd.offsets.YearEnd(5) tm.assert_index_equal(rng, expected) - def test_pi_add_iadd_timedeltalike_freq_mismatch_annual(self, - mismatched_freq): + def test_pi_add_sub_timedeltalike_freq_mismatch_annual(self, + mismatched_freq): other = mismatched_freq rng = pd.period_range('2014', '2024', freq='A') msg = ('Input has different freq(=.+)? ' @@ -711,13 +708,6 @@ def test_pi_add_iadd_timedeltalike_freq_mismatch_annual(self, rng + other with tm.assert_raises_regex(period.IncompatibleFrequency, msg): rng += other - - def test_pi_sub_isub_timedeltalike_freq_mismatch_annual(self, - mismatched_freq): - other = mismatched_freq - rng = pd.period_range('2014', '2024', freq='A') - msg = ('Input has different freq(=.+)? ' - 'from PeriodIndex\\(freq=A-DEC\\)') with tm.assert_raises_regex(period.IncompatibleFrequency, msg): rng - other with tm.assert_raises_regex(period.IncompatibleFrequency, msg): @@ -733,8 +723,8 @@ def test_pi_add_iadd_timedeltalike_M(self): rng += pd.offsets.MonthEnd(5) tm.assert_index_equal(rng, expected) - def test_pi_add_iadd_timedeltalike_freq_mismatch_monthly(self, - mismatched_freq): + def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self, + mismatched_freq): other = mismatched_freq rng = pd.period_range('2014-01', '2016-12', freq='M') msg = 'Input has different freq(=.+)? from PeriodIndex\\(freq=M\\)' @@ -742,12 +732,6 @@ def test_pi_add_iadd_timedeltalike_freq_mismatch_monthly(self, rng + other with tm.assert_raises_regex(period.IncompatibleFrequency, msg): rng += other - - def test_pi_sub_isub_timedeltalike_freq_mismatch_monthly(self, - mismatched_freq): - other = mismatched_freq - rng = pd.period_range('2014-01', '2016-12', freq='M') - msg = 'Input has different freq(=.+)? from PeriodIndex\\(freq=M\\)' with tm.assert_raises_regex(period.IncompatibleFrequency, msg): rng - other with tm.assert_raises_regex(period.IncompatibleFrequency, msg): From c04397bccbb5d60c47fa73a2fbf7029c90f71927 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 19 Oct 2018 19:52:53 -0700 Subject: [PATCH 7/7] flake8 cleanup unused import --- pandas/tests/arithmetic/test_period.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 91db9b176536f..4ccebd4305b90 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -11,7 +11,6 @@ import pandas.util.testing as tm from pandas.errors import PerformanceWarning -from pandas._libs.tslibs.period import IncompatibleFrequency import pandas.core.indexes.period as period from pandas.core import ops