diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index 8732d4063d74c..b1502ed3f3c09 100644 --- a/pandas/tests/io/excel/test_readers.py +++ b/pandas/tests/io/excel/test_readers.py @@ -426,7 +426,8 @@ def test_reader_dtype(self, read_ext): expected["c"] = ["001", "002", "003", "004"] tm.assert_frame_equal(actual, expected) - with pytest.raises(ValueError): + msg = "Unable to convert column d to type int64" + with pytest.raises(ValueError, match=msg): pd.read_excel(basename + read_ext, dtype={"d": "int64"}) @pytest.mark.parametrize( @@ -822,13 +823,15 @@ def test_excel_old_index_format(self, read_ext): def test_read_excel_bool_header_arg(self, read_ext): # GH 6114 + msg = "Passing a bool to header is invalid" for arg in [True, False]: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): pd.read_excel("test1" + read_ext, header=arg) def test_read_excel_chunksize(self, read_ext): # GH 8011 - with pytest.raises(NotImplementedError): + msg = "chunksize keyword of read_excel is not implemented" + with pytest.raises(NotImplementedError, match=msg): pd.read_excel("test1" + read_ext, chunksize=100) def test_read_excel_skiprows_list(self, read_ext): diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index 7ef4c454c5a5d..0811f2f822198 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -330,7 +330,8 @@ def test_excel_sheet_by_name_raise(self, path): tm.assert_frame_equal(gt, df) - with pytest.raises(xlrd.XLRDError): + msg = "No sheet named <'0'>" + with pytest.raises(xlrd.XLRDError, match=msg): pd.read_excel(xl, "0") def test_excel_writer_context_manager(self, frame, path): @@ -973,7 +974,11 @@ def roundtrip(data, header=True, parser_hdr=0, index=True): # This if will be removed once multi-column Excel writing # is implemented. For now fixing gh-9794. if c_idx_nlevels > 1: - with pytest.raises(NotImplementedError): + msg = ( + "Writing to Excel with MultiIndex columns and no index " + "\\('index'=False\\) is not yet implemented." + ) + with pytest.raises(NotImplementedError, match=msg): roundtrip(df, use_headers, index=False) else: res = roundtrip(df, use_headers) diff --git a/pandas/tests/io/excel/test_xlwt.py b/pandas/tests/io/excel/test_xlwt.py index 01feab08eb5e3..a2d8b9fce9767 100644 --- a/pandas/tests/io/excel/test_xlwt.py +++ b/pandas/tests/io/excel/test_xlwt.py @@ -18,7 +18,12 @@ def test_excel_raise_error_on_multiindex_columns_and_no_index(ext): [("site", ""), ("2014", "height"), ("2014", "weight")] ) df = DataFrame(np.random.randn(10, 3), columns=cols) - with pytest.raises(NotImplementedError): + + msg = ( + "Writing to Excel with MultiIndex columns and no index " + "\\('index'=False\\) is not yet implemented." + ) + with pytest.raises(NotImplementedError, match=msg): with tm.ensure_clean(ext) as path: df.to_excel(path, index=False) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index bf7b98eb78f11..1a5d122d732a9 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1508,7 +1508,8 @@ def test_to_string_specified_header(self): assert df_s == expected - with pytest.raises(ValueError): + msg = "Writing 2 cols but got 1 aliases" + with pytest.raises(ValueError, match=msg): df.to_string(header=["X"]) def test_to_string_no_index(self): diff --git a/pandas/tests/io/formats/test_style.py b/pandas/tests/io/formats/test_style.py index a94667a3f0c34..ec4614538004c 100644 --- a/pandas/tests/io/formats/test_style.py +++ b/pandas/tests/io/formats/test_style.py @@ -37,7 +37,8 @@ def h(x, foo="bar"): ] def test_init_non_pandas(self): - with pytest.raises(TypeError): + msg = "``data`` must be a Series or DataFrame" + with pytest.raises(TypeError, match=msg): Styler([1, 2, 3]) def test_init_series(self): @@ -1013,7 +1014,8 @@ def test_bar_align_zero_nans(self): def test_bar_bad_align_raises(self): df = pd.DataFrame({"A": [-100, -60, -30, -20]}) - with pytest.raises(ValueError): + msg = "`align` must be one of {'left', 'zero',' mid'}" + with pytest.raises(ValueError, match=msg): df.style.bar(align="poorly", color=["#d65f5f", "#5fba7d"]) def test_format_with_na_rep(self): @@ -1082,7 +1084,8 @@ def test_format_non_numeric_na(self): def test_format_with_bad_na_rep(self): # GH 21527 28358 df = pd.DataFrame([[None, None], [1.1, 1.2]], columns=["A", "B"]) - with pytest.raises(TypeError): + msg = "Expected a string, got -1 instead" + with pytest.raises(TypeError, match=msg): df.style.format(None, na_rep=-1) def test_highlight_null(self, null_color="red"): @@ -1110,10 +1113,11 @@ def test_highlight_null_subset(self): def test_nonunique_raises(self): df = pd.DataFrame([[1, 2]], columns=["A", "A"]) - with pytest.raises(ValueError): + msg = "style is not supported for non-unique indices." + with pytest.raises(ValueError, match=msg): df.style - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): Styler(df) def test_caption(self): @@ -1260,9 +1264,12 @@ def test_display_format(self): def test_display_format_raises(self): df = pd.DataFrame(np.random.randn(2, 2)) - with pytest.raises(TypeError): + msg = "Expected a template string or callable, got 5 instead" + with pytest.raises(TypeError, match=msg): df.style.format(5) - with pytest.raises(TypeError): + + msg = "Expected a template string or callable, got True instead" + with pytest.raises(TypeError, match=msg): df.style.format(True) def test_display_set_precision(self): @@ -1335,19 +1342,21 @@ def test_display_dict(self): def test_bad_apply_shape(self): df = pd.DataFrame([[1, 2], [3, 4]]) - with pytest.raises(ValueError): + msg = "returned the wrong shape" + with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: "x", subset=pd.IndexSlice[[0, 1], :]) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: [""], subset=pd.IndexSlice[[0, 1], :]) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: ["", "", "", ""]) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: ["", "", ""], subset=1) - with pytest.raises(ValueError): + msg = "Length mismatch: Expected axis has 3 elements" + with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: ["", "", ""], axis=1) def test_apply_bad_return(self): @@ -1355,7 +1364,8 @@ def f(x): return "" df = pd.DataFrame([[1, 2], [3, 4]]) - with pytest.raises(TypeError): + msg = "must return a DataFrame when passed to `Styler.apply` with axis=None" + with pytest.raises(TypeError, match=msg): df.style._apply(f, axis=None) def test_apply_bad_labels(self): @@ -1363,7 +1373,8 @@ def f(x): return pd.DataFrame(index=[1, 2], columns=["a", "b"]) df = pd.DataFrame([[1, 2], [3, 4]]) - with pytest.raises(ValueError): + msg = "must have identical index and columns as the input" + with pytest.raises(ValueError, match=msg): df.style._apply(f, axis=None) def test_get_level_lengths(self): diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index c2fbc59b8f482..509e5bcb33304 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -664,7 +664,8 @@ def test_to_latex_specified_header(self): assert withoutescape_result == withoutescape_expected - with pytest.raises(ValueError): + msg = "Writing 2 cols but got 1 aliases" + with pytest.raises(ValueError, match=msg): df.to_latex(header=["A"]) def test_to_latex_decimal(self, float_frame):