diff --git a/pandas/tests/series/methods/test_asof.py b/pandas/tests/series/methods/test_asof.py index ad5a2de6eabac..19caf4eccf748 100644 --- a/pandas/tests/series/methods/test_asof.py +++ b/pandas/tests/series/methods/test_asof.py @@ -148,14 +148,14 @@ def test_errors(self): # non-monotonic assert not s.index.is_monotonic - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="requires a sorted index"): s.asof(s.index[0]) # subset with Series N = 10 rng = date_range("1/1/1990", periods=N, freq="53s") s = Series(np.random.randn(N), index=rng) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="not valid for Series"): s.asof(s.index[0], subset="foo") def test_all_nans(self): diff --git a/pandas/tests/series/methods/test_droplevel.py b/pandas/tests/series/methods/test_droplevel.py index 435eb5751de4b..449ddd1cd0e49 100644 --- a/pandas/tests/series/methods/test_droplevel.py +++ b/pandas/tests/series/methods/test_droplevel.py @@ -15,5 +15,5 @@ def test_droplevel(self): result = ser.droplevel("b", axis="index") tm.assert_series_equal(result, expected) # test that droplevel raises ValueError on axis != 0 - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="No axis named columns"): ser.droplevel(1, axis="columns") diff --git a/pandas/tests/series/methods/test_tz_localize.py b/pandas/tests/series/methods/test_tz_localize.py index 44c55edf77c0a..532b8d16f0d5c 100644 --- a/pandas/tests/series/methods/test_tz_localize.py +++ b/pandas/tests/series/methods/test_tz_localize.py @@ -35,7 +35,7 @@ def test_series_tz_localize_ambiguous_bool(self): expected0 = Series([expected0]) expected1 = Series([expected1]) - with pytest.raises(pytz.AmbiguousTimeError): + with tm.external_error_raised(pytz.AmbiguousTimeError): ser.dt.tz_localize("US/Central") result = ser.dt.tz_localize("US/Central", ambiguous=True) @@ -66,10 +66,10 @@ def test_series_tz_localize_nonexistent(self, tz, method, exp): dti = date_range(start="2015-03-29 02:00:00", periods=n, freq="min") s = Series(1, dti) if method == "raise": - with pytest.raises(pytz.NonExistentTimeError): + with tm.external_error_raised(pytz.NonExistentTimeError): s.tz_localize(tz, nonexistent=method) elif exp == "invalid": - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="argument must be one of"): dti.tz_localize(tz, nonexistent=method) else: result = s.tz_localize(tz, nonexistent=method) diff --git a/pandas/tests/series/test_apply.py b/pandas/tests/series/test_apply.py index 0661828814888..54cf307a6ee66 100644 --- a/pandas/tests/series/test_apply.py +++ b/pandas/tests/series/test_apply.py @@ -248,18 +248,21 @@ def test_transform(self, string_series): def test_transform_and_agg_error(self, string_series): # we are trying to transform with an aggregator - with pytest.raises(ValueError): + msg = "transforms cannot produce aggregated results" + with pytest.raises(ValueError, match=msg): string_series.transform(["min", "max"]) - with pytest.raises(ValueError): + msg = "cannot combine transform and aggregation" + with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): string_series.agg(["sqrt", "max"]) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): string_series.transform(["sqrt", "max"]) - with pytest.raises(ValueError): + msg = "cannot perform both aggregation and transformation" + with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): string_series.agg({"foo": np.sqrt, "bar": "sum"}) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index c7a04843b8296..5c8a0d224c4f9 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -335,12 +335,13 @@ class TestSeriesComparison: def test_comparison_different_length(self): a = Series(["a", "b", "c"]) b = Series(["b", "a"]) - with pytest.raises(ValueError): + msg = "only compare identically-labeled Series" + with pytest.raises(ValueError, match=msg): a < b a = Series([1, 2]) b = Series([2, 3, 4]) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): a == b @pytest.mark.parametrize("opname", ["eq", "ne", "gt", "lt", "ge", "le"]) @@ -474,23 +475,25 @@ def test_categorical_comparisons(self): assert (~(f == a) == (f != a)).all() # non-equality is not comparable - with pytest.raises(TypeError): + msg = "can only compare equality or not" + with pytest.raises(TypeError, match=msg): a < b - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): b < a - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): a > b - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): b > a def test_unequal_categorical_comparison_raises_type_error(self): # unequal comparison should raise for unordered cats cat = Series(Categorical(list("abc"))) - with pytest.raises(TypeError): + msg = "can only compare equality or not" + with pytest.raises(TypeError, match=msg): cat > "b" cat = Series(Categorical(list("abc"), ordered=False)) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): cat > "b" # https://github.com/pandas-dev/pandas/issues/9836#issuecomment-92123057 @@ -498,13 +501,14 @@ def test_unequal_categorical_comparison_raises_type_error(self): # for unequal comps, but not for equal/not equal cat = Series(Categorical(list("abc"), ordered=True)) - with pytest.raises(TypeError): + msg = "Cannot compare a Categorical for op.+with a scalar" + with pytest.raises(TypeError, match=msg): cat < "d" - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): cat > "d" - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): "d" < cat - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): "d" > cat tm.assert_series_equal(cat == "d", Series([False, False, False])) @@ -668,10 +672,11 @@ def test_series_add_aware_naive_raises(self): ser_utc = ser.tz_localize("utc") - with pytest.raises(Exception): + msg = "Cannot join tz-naive with tz-aware DatetimeIndex" + with pytest.raises(Exception, match=msg): ser + ser_utc - with pytest.raises(Exception): + with pytest.raises(Exception, match=msg): ser_utc + ser def test_datetime_understood(self): diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index 0d7fd0529dc1f..8b1f58414e175 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -244,8 +244,9 @@ def get_dir(s): s.dt.hour = 5 # trying to set a copy + msg = "modifications to a property of a datetimelike.+not supported" with pd.option_context("chained_assignment", "raise"): - with pytest.raises(com.SettingWithCopyError): + with pytest.raises(com.SettingWithCopyError, match=msg): s.dt.hour[0] = 5 @pytest.mark.parametrize( @@ -311,7 +312,7 @@ def test_dt_round_tz_ambiguous(self, method): tm.assert_series_equal(result, expected) # raise - with pytest.raises(pytz.AmbiguousTimeError): + with tm.external_error_raised(pytz.AmbiguousTimeError): getattr(df1.date.dt, method)("H", ambiguous="raise") @pytest.mark.parametrize( diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index ba1b3e9d0ca8e..e1c9682329271 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -121,23 +121,25 @@ def test_logical_operators_int_dtype_with_float(self): # GH#9016: support bitwise op for integer types s_0123 = Series(range(4), dtype="int64") - with pytest.raises(TypeError): + msg = "Cannot perform.+with a dtyped.+array and scalar of type" + with pytest.raises(TypeError, match=msg): s_0123 & np.NaN - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): s_0123 & 3.14 - with pytest.raises(TypeError): + msg = "unsupported operand type.+for &:" + with pytest.raises(TypeError, match=msg): s_0123 & [0.1, 4, 3.14, 2] - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): s_0123 & np.array([0.1, 4, 3.14, 2]) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): s_0123 & Series([0.1, 4, -3.14, 2]) def test_logical_operators_int_dtype_with_str(self): s_1111 = Series([1] * 4, dtype="int8") - - with pytest.raises(TypeError): + msg = "Cannot perform 'and_' with a dtyped.+array and scalar of type" + with pytest.raises(TypeError, match=msg): s_1111 & "a" - with pytest.raises(TypeError): + with pytest.raises(TypeError, match="unsupported operand.+for &"): s_1111 & ["a", "b", "c", "d"] def test_logical_operators_int_dtype_with_bool(self): @@ -255,7 +257,8 @@ def test_logical_operators_int_dtype_with_bool_dtype_and_reindex(self): def test_scalar_na_logical_ops_corners(self): s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10]) - with pytest.raises(TypeError): + msg = "Cannot perform.+with a dtyped.+array and scalar of type" + with pytest.raises(TypeError, match=msg): s & datetime(2005, 1, 1) s = Series([2, 3, 4, 5, 6, 7, 8, 9, datetime(2005, 1, 1)]) @@ -451,8 +454,9 @@ def test_logical_ops_label_based(self): expected = Series([True, True, True], index=index) tm.assert_series_equal(result, expected) + msg = "Cannot perform.+with a dtyped.+array and scalar of type" for v in [np.nan, "foo"]: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): t | v for v in [False, 0]: @@ -469,8 +473,9 @@ def test_logical_ops_label_based(self): result = Series([True, False, True], index=index) & v expected = Series([False, False, False], index=index) tm.assert_series_equal(result, expected) + msg = "Cannot perform.+with a dtyped.+array and scalar of type" for v in [np.nan]: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): t & v def test_logical_ops_df_compat(self): diff --git a/pandas/tests/tools/test_to_time.py b/pandas/tests/tools/test_to_time.py index 17ab492aca725..937570d89fb77 100644 --- a/pandas/tests/tools/test_to_time.py +++ b/pandas/tests/tools/test_to_time.py @@ -46,7 +46,8 @@ def test_parsers_time(self): res = to_time(arg, format="%I:%M%p", errors="ignore") tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_)) - with pytest.raises(ValueError): + msg = "Cannot convert.+to a time with given format" + with pytest.raises(ValueError, match=msg): to_time(arg, format="%I:%M%p", errors="raise") tm.assert_series_equal(