From d12f8ac787affa8417ca2b6aca7c2eea616dc39e Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Thu, 19 Mar 2020 11:01:45 +0700 Subject: [PATCH 1/3] TST: bare pytest raises GH30999 --- pandas/tests/io/excel/test_readers.py | 13 +++-- pandas/tests/io/excel/test_writers.py | 9 +++- pandas/tests/io/excel/test_xlwt.py | 7 ++- pandas/tests/io/formats/test_format.py | 3 +- pandas/tests/io/formats/test_style.py | 67 +++++++++++++++++++----- pandas/tests/io/formats/test_to_latex.py | 3 +- 6 files changed, 80 insertions(+), 22 deletions(-) diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index 8732d4063d74c..8b3aba4dbad07 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,19 @@ 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. " + "Use header=None for no header or header=int or list-like of ints " + "to specify the row\\(s\\) making up the column names" + ) 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..b43395d41f2cc 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,42 @@ def test_display_dict(self): def test_bad_apply_shape(self): df = pd.DataFrame([[1, 2], [3, 4]]) - with pytest.raises(ValueError): + msg = ( + "Function . " + "at 0x[a-z0-9]+> returned the wrong shape.\n" + "Result has shape: \\(2,\\)\nExpected shape: \\(2, 2\\)" + ) + with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: "x", subset=pd.IndexSlice[[0, 1], :]) - with pytest.raises(ValueError): + msg = ( + "Function . " + "at 0x[a-z0-9]+> returned the wrong shape.\n" + "Result has shape: \\(1, 2\\)\nExpected shape: \\(2, 2\\)" + ) + with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: [""], subset=pd.IndexSlice[[0, 1], :]) - with pytest.raises(ValueError): + msg = ( + "Function . " + "at 0x[a-z0-9]+> returned the wrong shape.\n" + "Result has shape: \\(4, 2\\)\nExpected shape: \\(2, 2\\)" + ) + with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: ["", "", "", ""]) - with pytest.raises(ValueError): + msg = ( + "Function . " + "at 0x[a-z0-9]+> returned the wrong shape.\n" + "Result has shape: \\(3, 2\\)\nExpected shape: \\(1, 2\\)" + ) + 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, new values have 2 elements" + ) + with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: ["", "", ""], axis=1) def test_apply_bad_return(self): @@ -1355,7 +1385,12 @@ def f(x): return "" df = pd.DataFrame([[1, 2], [3, 4]]) - with pytest.raises(TypeError): + msg = ( + "Function .f " + "at 0x[a-z0-9]+> 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 +1398,11 @@ def f(x): return pd.DataFrame(index=[1, 2], columns=["a", "b"]) df = pd.DataFrame([[1, 2], [3, 4]]) - with pytest.raises(ValueError): + msg = ( + "Result of .f " + "at 0x[a-z0-9]+> 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): From ee362976cad476d457fb2b712e6a3a2e2bfc7ccd Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Thu, 19 Mar 2020 11:41:30 +0700 Subject: [PATCH 2/3] add A-Z for windows memory address --- pandas/tests/io/formats/test_style.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/tests/io/formats/test_style.py b/pandas/tests/io/formats/test_style.py index b43395d41f2cc..f53ad53a02e7f 100644 --- a/pandas/tests/io/formats/test_style.py +++ b/pandas/tests/io/formats/test_style.py @@ -1344,7 +1344,7 @@ def test_bad_apply_shape(self): df = pd.DataFrame([[1, 2], [3, 4]]) msg = ( "Function . " - "at 0x[a-z0-9]+> returned the wrong shape.\n" + "at 0x[a-zA-Z0-9]+> returned the wrong shape.\n" "Result has shape: \\(2,\\)\nExpected shape: \\(2, 2\\)" ) with pytest.raises(ValueError, match=msg): @@ -1352,7 +1352,7 @@ def test_bad_apply_shape(self): msg = ( "Function . " - "at 0x[a-z0-9]+> returned the wrong shape.\n" + "at 0x[a-zA-Z0-9]+> returned the wrong shape.\n" "Result has shape: \\(1, 2\\)\nExpected shape: \\(2, 2\\)" ) with pytest.raises(ValueError, match=msg): @@ -1360,7 +1360,7 @@ def test_bad_apply_shape(self): msg = ( "Function . " - "at 0x[a-z0-9]+> returned the wrong shape.\n" + "at 0x[a-zA-Z0-9]+> returned the wrong shape.\n" "Result has shape: \\(4, 2\\)\nExpected shape: \\(2, 2\\)" ) with pytest.raises(ValueError, match=msg): @@ -1368,7 +1368,7 @@ def test_bad_apply_shape(self): msg = ( "Function . " - "at 0x[a-z0-9]+> returned the wrong shape.\n" + "at 0x[a-zA-Z0-9]+> returned the wrong shape.\n" "Result has shape: \\(3, 2\\)\nExpected shape: \\(1, 2\\)" ) with pytest.raises(ValueError, match=msg): @@ -1387,7 +1387,7 @@ def f(x): df = pd.DataFrame([[1, 2], [3, 4]]) msg = ( "Function .f " - "at 0x[a-z0-9]+> must return a DataFrame " + "at 0x[a-zA-Z0-9]+> must return a DataFrame " "when passed to `Styler.apply` with axis=None" ) with pytest.raises(TypeError, match=msg): @@ -1400,7 +1400,7 @@ def f(x): df = pd.DataFrame([[1, 2], [3, 4]]) msg = ( "Result of .f " - "at 0x[a-z0-9]+> must have identical index and columns as the input" + "at 0x[a-zA-Z0-9]+> must have identical index and columns as the input" ) with pytest.raises(ValueError, match=msg): df.style._apply(f, axis=None) From 45fb9c23ba687d2558864869e429c8d28812dfb2 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Fri, 20 Mar 2020 10:30:38 +0700 Subject: [PATCH 3/3] shorten the msg --- pandas/tests/io/excel/test_readers.py | 6 +---- pandas/tests/io/formats/test_style.py | 36 +++------------------------ 2 files changed, 5 insertions(+), 37 deletions(-) diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index 8b3aba4dbad07..b1502ed3f3c09 100644 --- a/pandas/tests/io/excel/test_readers.py +++ b/pandas/tests/io/excel/test_readers.py @@ -823,11 +823,7 @@ 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. " - "Use header=None for no header or header=int or list-like of ints " - "to specify the row\\(s\\) making up the column names" - ) + msg = "Passing a bool to header is invalid" for arg in [True, False]: with pytest.raises(TypeError, match=msg): pd.read_excel("test1" + read_ext, header=arg) diff --git a/pandas/tests/io/formats/test_style.py b/pandas/tests/io/formats/test_style.py index f53ad53a02e7f..ec4614538004c 100644 --- a/pandas/tests/io/formats/test_style.py +++ b/pandas/tests/io/formats/test_style.py @@ -1342,41 +1342,20 @@ def test_display_dict(self): def test_bad_apply_shape(self): df = pd.DataFrame([[1, 2], [3, 4]]) - msg = ( - "Function . " - "at 0x[a-zA-Z0-9]+> returned the wrong shape.\n" - "Result has shape: \\(2,\\)\nExpected shape: \\(2, 2\\)" - ) + msg = "returned the wrong shape" with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: "x", subset=pd.IndexSlice[[0, 1], :]) - msg = ( - "Function . " - "at 0x[a-zA-Z0-9]+> returned the wrong shape.\n" - "Result has shape: \\(1, 2\\)\nExpected shape: \\(2, 2\\)" - ) with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: [""], subset=pd.IndexSlice[[0, 1], :]) - msg = ( - "Function . " - "at 0x[a-zA-Z0-9]+> returned the wrong shape.\n" - "Result has shape: \\(4, 2\\)\nExpected shape: \\(2, 2\\)" - ) with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: ["", "", "", ""]) - msg = ( - "Function . " - "at 0x[a-zA-Z0-9]+> returned the wrong shape.\n" - "Result has shape: \\(3, 2\\)\nExpected shape: \\(1, 2\\)" - ) with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: ["", "", ""], subset=1) - msg = ( - "Length mismatch: Expected axis has 3 elements, new values have 2 elements" - ) + msg = "Length mismatch: Expected axis has 3 elements" with pytest.raises(ValueError, match=msg): df.style._apply(lambda x: ["", "", ""], axis=1) @@ -1385,11 +1364,7 @@ def f(x): return "" df = pd.DataFrame([[1, 2], [3, 4]]) - msg = ( - "Function .f " - "at 0x[a-zA-Z0-9]+> must return a DataFrame " - "when passed to `Styler.apply` with axis=None" - ) + 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) @@ -1398,10 +1373,7 @@ def f(x): return pd.DataFrame(index=[1, 2], columns=["a", "b"]) df = pd.DataFrame([[1, 2], [3, 4]]) - msg = ( - "Result of .f " - "at 0x[a-zA-Z0-9]+> must have identical index and columns as the input" - ) + msg = "must have identical index and columns as the input" with pytest.raises(ValueError, match=msg): df.style._apply(f, axis=None)