diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index f97a1651163e8..405dc0805a285 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -124,14 +124,14 @@ def test_comparison_invalid(self, box_with_array): result = x != y expected = tm.box_expected([True] * 5, xbox) tm.assert_equal(result, expected) - - with pytest.raises(TypeError): + msg = 'Invalid comparison between' + with pytest.raises(TypeError, match=msg): x >= y - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): x > y - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): x < y - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): x <= y @pytest.mark.parametrize('data', [ @@ -327,9 +327,10 @@ def test_comparison_tzawareness_compat(self, op): # raise naive_series = Series(dr) aware_series = Series(dz) - with pytest.raises(TypeError): + msg = 'Cannot compare tz-naive and tz-aware' + with pytest.raises(TypeError, match=msg): op(dz, naive_series) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(dr, aware_series) # TODO: implement _assert_tzawareness_compat for the reverse @@ -428,14 +429,14 @@ def test_dti_cmp_null_scalar_inequality(self, tz_naive_fixture, other, dti = pd.date_range('2016-01-01', periods=2, tz=tz) # FIXME: ValueError with transpose dtarr = tm.box_expected(dti, box_with_array, transpose=False) - - with pytest.raises(TypeError): + msg = 'Invalid comparison between' + with pytest.raises(TypeError, match=msg): dtarr < other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dtarr <= other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dtarr > other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dtarr >= other @pytest.mark.parametrize('dtype', [None, object]) @@ -584,22 +585,23 @@ def test_comparison_tzawareness_compat(self, op, box_with_array): dr = tm.box_expected(dr, box_with_array, transpose=False) dz = tm.box_expected(dz, box_with_array, transpose=False) - with pytest.raises(TypeError): + msg = 'Cannot compare tz-naive and tz-aware' + with pytest.raises(TypeError, match=msg): op(dr, dz) if box_with_array is not pd.DataFrame: # DataFrame op is invalid until transpose bug is fixed - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(dr, list(dz)) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(dr, np.array(list(dz), dtype=object)) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(dz, dr) if box_with_array is not pd.DataFrame: # DataFrame op is invalid until transpose bug is fixed - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(dz, list(dr)) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(dz, np.array(list(dr), dtype=object)) # Check that there isn't a problem aware-aware and naive-naive do not @@ -617,15 +619,15 @@ def test_comparison_tzawareness_compat(self, op, box_with_array): ts_tz = pd.Timestamp('2000-03-14 01:59', tz='Europe/Amsterdam') assert_all(dr > ts) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(dr, ts_tz) assert_all(dz > ts_tz) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(dz, ts) # GH#12601: Check comparison against Timestamps and DatetimeIndex - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(ts, dz) @pytest.mark.parametrize('op', [operator.eq, operator.ne, @@ -641,10 +643,10 @@ def test_scalar_comparison_tzawareness(self, op, other, tz_aware_fixture, # FIXME: ValueError with transpose dtarr = tm.box_expected(dti, box_with_array, transpose=False) - - with pytest.raises(TypeError): + msg = 'Cannot compare tz-naive and tz-aware' + with pytest.raises(TypeError, match=msg): op(dtarr, other) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): op(other, dtarr) @pytest.mark.parametrize('op', [operator.eq, operator.ne, @@ -714,14 +716,14 @@ def test_dt64arr_cmp_scalar_invalid(self, other, tz_naive_fixture, expected = np.array([True] * 10) expected = tm.box_expected(expected, xbox, transpose=False) tm.assert_equal(result, expected) - - with pytest.raises(TypeError): + msg = 'Invalid comparison between' + with pytest.raises(TypeError, match=msg): rng < other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): rng <= other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): rng > other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): rng >= other def test_dti_cmp_list(self): @@ -749,14 +751,14 @@ def test_dti_cmp_tdi_tzawareness(self, other): result = dti != other expected = np.array([True] * 10) tm.assert_numpy_array_equal(result, expected) - - with pytest.raises(TypeError): + msg = 'Invalid comparison between' + with pytest.raises(TypeError, match=msg): dti < other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dti <= other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dti > other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dti >= other def test_dti_cmp_object_dtype(self): @@ -770,7 +772,8 @@ def test_dti_cmp_object_dtype(self): tm.assert_numpy_array_equal(result, expected) other = dti.tz_localize(None) - with pytest.raises(TypeError): + msg = 'Cannot compare tz-naive and tz-aware' + with pytest.raises(TypeError, match=msg): # tzawareness failure dti != other @@ -778,8 +781,8 @@ def test_dti_cmp_object_dtype(self): result = dti == other expected = np.array([True] * 5 + [False] * 5) tm.assert_numpy_array_equal(result, expected) - - with pytest.raises(TypeError): + msg = "Cannot compare type" + with pytest.raises(TypeError, match=msg): dti >= other @@ -898,7 +901,8 @@ def test_dt64arr_add_sub_td64_nat(self, box_with_array, tz_naive_fixture): tm.assert_equal(result, expected) result = obj - other tm.assert_equal(result, expected) - with pytest.raises(TypeError): + msg = 'cannot subtract' + with pytest.raises(TypeError, match=msg): other - obj def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, @@ -927,8 +931,8 @@ def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, result = dtarr - tdarr tm.assert_equal(result, expected) - - with pytest.raises(TypeError): + msg = 'cannot subtract' + with pytest.raises(TypeError, match=msg): tdarr - dtarr # ----------------------------------------------------------------- @@ -1028,10 +1032,10 @@ def test_dt64arr_aware_sub_dt64ndarray_raises(self, tz_aware_fixture, dt64vals = dti.values dtarr = tm.box_expected(dti, box_with_array) - - with pytest.raises(TypeError): + msg = 'DatetimeArray subtraction must have the same timezones or' + with pytest.raises(TypeError, match=msg): dtarr - dt64vals - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dt64vals - dtarr # ------------------------------------------------------------- @@ -1048,17 +1052,17 @@ def test_dt64arr_add_dt64ndarray_raises(self, tz_naive_fixture, dt64vals = dti.values dtarr = tm.box_expected(dti, box_with_array) - - with pytest.raises(TypeError): + msg = 'cannot add' + with pytest.raises(TypeError, match=msg): dtarr + dt64vals - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dt64vals + dtarr def test_dt64arr_add_timestamp_raises(self, box_with_array): # GH#22163 ensure DataFrame doesn't cast Timestamp to i8 idx = DatetimeIndex(['2011-01-01', '2011-01-02']) idx = tm.box_expected(idx, box_with_array) - msg = "cannot add" + msg = 'cannot add' with pytest.raises(TypeError, match=msg): idx + Timestamp('2011-01-01') with pytest.raises(TypeError, match=msg): @@ -1071,13 +1075,14 @@ def test_dt64arr_add_timestamp_raises(self, box_with_array): def test_dt64arr_add_sub_float(self, other, box_with_array): dti = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') dtarr = tm.box_expected(dti, box_with_array) - with pytest.raises(TypeError): + msg = '|'.join(['unsupported operand type', 'cannot (add|subtract)']) + with pytest.raises(TypeError, match=msg): dtarr + other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): other + dtarr - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dtarr - other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): other - dtarr @pytest.mark.parametrize('pi_freq', ['D', 'W', 'Q', 'H']) @@ -1090,14 +1095,15 @@ def test_dt64arr_add_sub_parr(self, dti_freq, pi_freq, dtarr = tm.box_expected(dti, box_with_array) parr = tm.box_expected(pi, box_with_array2) - - with pytest.raises(TypeError): + msg = '|'.join(['cannot (add|subtract)', 'unsupported operand', + 'descriptor.*requires', 'ufunc.*cannot use operands']) + with pytest.raises(TypeError, match=msg): dtarr + parr - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): parr + dtarr - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dtarr - parr - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): parr - dtarr @pytest.mark.parametrize('dti_freq', [None, 'D']) @@ -1108,14 +1114,14 @@ def test_dt64arr_add_sub_period_scalar(self, dti_freq, box_with_array): idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq) dtarr = tm.box_expected(idx, box_with_array) - - with pytest.raises(TypeError): + msg = '|'.join(['unsupported operand type', 'cannot (add|subtract)']) + with pytest.raises(TypeError, match=msg): dtarr + per - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): per + dtarr - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dtarr - per - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): per - dtarr @@ -1156,8 +1162,8 @@ def test_dt64arr_series_sub_tick_DateOffset(self, box_with_array): result2 = -pd.offsets.Second(5) + ser tm.assert_equal(result2, expected) - - with pytest.raises(TypeError): + msg = "bad operand type for unary" + with pytest.raises(TypeError, match=msg): pd.offsets.Second(5) - ser @pytest.mark.parametrize('cls_name', ['Day', 'Hour', 'Minute', 'Second', @@ -1239,8 +1245,8 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array): expected = DatetimeIndex([x - off for x in vec_items]) expected = tm.box_expected(expected, box_with_array) tm.assert_equal(expected, vec - off) - - with pytest.raises(TypeError): + msg = "bad operand type for unary" + with pytest.raises(TypeError, match=msg): off - vec # ------------------------------------------------------------- @@ -1320,8 +1326,8 @@ def test_dt64arr_add_sub_DateOffsets(self, box_with_array, expected = DatetimeIndex([offset + x for x in vec_items]) expected = tm.box_expected(expected, box_with_array) tm.assert_equal(expected, offset + vec) - - with pytest.raises(TypeError): + msg = "bad operand type for unary" + with pytest.raises(TypeError, match=msg): offset - vec def test_dt64arr_add_sub_DateOffset(self, box_with_array): @@ -1440,13 +1446,14 @@ def test_dt64_series_arith_overflow(self): td = pd.Timedelta('20000 Days') dti = pd.date_range('1949-09-30', freq='100Y', periods=4) ser = pd.Series(dti) - with pytest.raises(OverflowError): + msg = 'Overflow in int64 addition' + with pytest.raises(OverflowError, match=msg): ser - dt - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): dt - ser - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): ser + td - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): td + ser ser.iloc[-1] = pd.NaT @@ -1480,9 +1487,9 @@ def test_datetimeindex_sub_timestamp_overflow(self): tspos.to_pydatetime(), tspos.to_datetime64().astype('datetime64[ns]'), tspos.to_datetime64().astype('datetime64[D]')] - + msg = 'Overflow in int64 addition' for variant in ts_neg_variants: - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): dtimax - variant expected = pd.Timestamp.max.value - tspos.value @@ -1496,7 +1503,7 @@ def test_datetimeindex_sub_timestamp_overflow(self): assert res[1].value == expected for variant in ts_pos_variants: - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): dtimin - variant def test_datetimeindex_sub_datetimeindex_overflow(self): @@ -1515,22 +1522,22 @@ def test_datetimeindex_sub_datetimeindex_overflow(self): expected = pd.Timestamp.min.value - ts_neg[1].value result = dtimin - ts_neg assert result[1].value == expected - - with pytest.raises(OverflowError): + msg = 'Overflow in int64 addition' + with pytest.raises(OverflowError, match=msg): dtimax - ts_neg - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): dtimin - ts_pos # Edge cases tmin = pd.to_datetime([pd.Timestamp.min]) t1 = tmin + pd.Timedelta.max + pd.Timedelta('1us') - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): t1 - tmin tmax = pd.to_datetime([pd.Timestamp.max]) t2 = tmax + pd.Timedelta.min - pd.Timedelta('1us') - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): tmax - t2 @@ -1543,7 +1550,8 @@ def test_empty_series_add_sub(self): tm.assert_series_equal(a, a + b) tm.assert_series_equal(a, a - b) tm.assert_series_equal(a, b + a) - with pytest.raises(TypeError): + msg = 'cannot subtract' + with pytest.raises(TypeError, match=msg): b - a def test_operators_datetimelike(self): @@ -1688,12 +1696,13 @@ def test_datetime64_ops_nat(self): # subtraction tm.assert_series_equal(-NaT + datetime_series, nat_series_dtype_timestamp) - with pytest.raises(TypeError): + msg = 'Unary negative expects' + with pytest.raises(TypeError, match=msg): -single_nat_dtype_datetime + datetime_series tm.assert_series_equal(-NaT + nat_series_dtype_timestamp, nat_series_dtype_timestamp) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): -single_nat_dtype_datetime + nat_series_dtype_timestamp # addition @@ -1718,15 +1727,16 @@ def test_datetime64_ops_nat(self): @pytest.mark.parametrize('one', [1, 1.0, np.array(1)]) def test_dt64_mul_div_numeric_invalid(self, one, dt64_series): # multiplication - with pytest.raises(TypeError): + msg = 'cannot perform .* with this index type' + with pytest.raises(TypeError, match=msg): dt64_series * one - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): one * dt64_series # division - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dt64_series / one - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): one / dt64_series @pytest.mark.parametrize('op', ['__add__', '__radd__', @@ -1740,13 +1750,17 @@ def test_dt64_series_add_intlike(self, tz, op): other = Series([20, 30, 40], dtype='uint8') method = getattr(ser, op) - with pytest.raises(TypeError): + msg = '|'.join(['incompatible type for a .* operation', + 'cannot evaluate a numeric op', + 'ufunc .* cannot use operands', + 'cannot (add|subtract)']) + with pytest.raises(TypeError, match=msg): method(1) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): method(other) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): method(other.values) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): method(pd.Index(other)) # ------------------------------------------------------------- @@ -1783,13 +1797,14 @@ def test_operators_datetimelike_with_timezones(self): result = dt1 - td1[0] exp = (dt1.dt.tz_localize(None) - td1[0]).dt.tz_localize(tz) tm.assert_series_equal(result, exp) - with pytest.raises(TypeError): + msg = "bad operand type for unary" + with pytest.raises(TypeError, match=msg): td1[0] - dt1 result = dt2 - td2[0] exp = (dt2.dt.tz_localize(None) - td2[0]).dt.tz_localize(tz) tm.assert_series_equal(result, exp) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): td2[0] - dt2 result = dt1 + td1 @@ -1807,10 +1822,10 @@ def test_operators_datetimelike_with_timezones(self): result = dt2 - td2 exp = (dt2.dt.tz_localize(None) - td2).dt.tz_localize(tz) tm.assert_series_equal(result, exp) - - with pytest.raises(TypeError): + msg = 'cannot (add|subtract)' + with pytest.raises(TypeError, match=msg): td1 - dt1 - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): td2 - dt2 @@ -1909,13 +1924,15 @@ def test_dti_add_intarray_no_freq(self, int_holder): # GH#19959 dti = pd.DatetimeIndex(['2016-01-01', 'NaT', '2017-04-05 06:07:08']) other = int_holder([9, 4, -1]) - with pytest.raises(NullFrequencyError): + nfmsg = 'Cannot shift with no freq' + tmsg = 'cannot subtract DatetimeArray from' + with pytest.raises(NullFrequencyError, match=nfmsg): dti + other - with pytest.raises(NullFrequencyError): + with pytest.raises(NullFrequencyError, match=nfmsg): other + dti - with pytest.raises(NullFrequencyError): + with pytest.raises(NullFrequencyError, match=nfmsg): dti - other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=tmsg): other - dti # ------------------------------------------------------------- @@ -2057,14 +2074,14 @@ def test_sub_dti_dti(self): result = dti_tz - dti_tz tm.assert_index_equal(result, expected) - - with pytest.raises(TypeError): + msg = 'DatetimeArray subtraction must have the same timezones or' + with pytest.raises(TypeError, match=msg): dti_tz - dti - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dti - dti_tz - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): dti_tz - dti_tz2 # isub @@ -2074,7 +2091,8 @@ def test_sub_dti_dti(self): # different length raises ValueError dti1 = date_range('20130101', periods=3) dti2 = date_range('20130101', periods=4) - with pytest.raises(ValueError): + msg = 'cannot add indices of unequal length' + with pytest.raises(ValueError, match=msg): dti1 - dti2 # NaN propagation @@ -2148,8 +2166,8 @@ def test_ops_nat_mixed_datetime64_timedelta64(self): tm.assert_series_equal(-single_nat_dtype_timedelta + nat_series_dtype_timestamp, nat_series_dtype_timestamp) - - with pytest.raises(TypeError): + msg = 'cannot subtract a datelike' + with pytest.raises(TypeError, match=msg): timedelta_series - single_nat_dtype_datetime # addition