From df2ab2af898c2dc1d29f7c6bf9fdb6b902874505 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 1 Jan 2022 22:09:31 -0800 Subject: [PATCH 1/2] TST: Parameterize --- pandas/tests/computation/test_eval.py | 38 +++++++++------------------ 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py index cdfafb6560de3..3014b3fc0b16a 100644 --- a/pandas/tests/computation/test_eval.py +++ b/pandas/tests/computation/test_eval.py @@ -875,7 +875,8 @@ def test_medium_complex_frame_alignment(self, engine, parser, r1, c1, r2, c2): def test_basic_frame_series_alignment( self, engine, parser, index_name, r_idx_type, c_idx_type ): - def testit(r_idx_type, c_idx_type, index_name): + with warnings.catch_warnings(record=True): + warnings.simplefilter("always", RuntimeWarning) df = tm.makeCustomDataframe( 10, 10, data_gen_f=f, r_idx_type=r_idx_type, c_idx_type=c_idx_type ) @@ -894,14 +895,17 @@ def testit(r_idx_type, c_idx_type, index_name): expected = df + s tm.assert_frame_equal(res, expected) - with warnings.catch_warnings(record=True): - warnings.simplefilter("always", RuntimeWarning) - - testit(r_idx_type, c_idx_type, index_name) - @pytest.mark.parametrize("index_name", ["index", "columns"]) - def test_basic_series_frame_alignment(self, engine, parser, index_name): - def testit(r_idx_type, c_idx_type, index_name): + @pytest.mark.parametrize( + "r_idx_type, c_idx_type", + list(product(["i", "u", "s"], ["i", "u", "s"])) + [("dt", "dt")], + ) + def test_basic_series_frame_alignment( + self, engine, parser, index_name, r_idx_type, c_idx_type + ): + with warnings.catch_warnings(record=True): + # avoid warning about comparing strings and ints + warnings.simplefilter("ignore", RuntimeWarning) df = tm.makeCustomDataframe( 10, 7, data_gen_f=f, r_idx_type=r_idx_type, c_idx_type=c_idx_type ) @@ -919,24 +923,6 @@ def testit(r_idx_type, c_idx_type, index_name): expected = s + df tm.assert_frame_equal(res, expected) - # only test dt with dt, otherwise weird joins result - args = product(["i", "u", "s"], ["i", "u", "s"]) - with warnings.catch_warnings(record=True): - # avoid warning about comparing strings and ints - warnings.simplefilter("ignore", RuntimeWarning) - - for r_idx_type, c_idx_type in args: - testit(r_idx_type, c_idx_type, index_name) - - # dt with dt - args = product(["dt"], ["dt"]) - with warnings.catch_warnings(record=True): - # avoid warning about comparing strings and ints - warnings.simplefilter("ignore", RuntimeWarning) - - for r_idx_type, c_idx_type in args: - testit(r_idx_type, c_idx_type, index_name) - @pytest.mark.parametrize("c_idx_type", index_types) @pytest.mark.parametrize("r_idx_type", lhs_index_types) @pytest.mark.parametrize("index_name", ["index", "columns"]) From 3b9b86e3b3d7d6829eef1a271a032d5e7a85870c Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sun, 2 Jan 2022 10:20:22 -0800 Subject: [PATCH 2/2] Use pytest.mark.filterwarnings --- pandas/tests/computation/test_eval.py | 32 +++++++++++++-------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py index 3014b3fc0b16a..948eeeecb2ce4 100644 --- a/pandas/tests/computation/test_eval.py +++ b/pandas/tests/computation/test_eval.py @@ -900,28 +900,26 @@ def test_basic_frame_series_alignment( "r_idx_type, c_idx_type", list(product(["i", "u", "s"], ["i", "u", "s"])) + [("dt", "dt")], ) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") def test_basic_series_frame_alignment( self, engine, parser, index_name, r_idx_type, c_idx_type ): - with warnings.catch_warnings(record=True): - # avoid warning about comparing strings and ints - warnings.simplefilter("ignore", RuntimeWarning) - df = tm.makeCustomDataframe( - 10, 7, data_gen_f=f, r_idx_type=r_idx_type, c_idx_type=c_idx_type - ) - index = getattr(df, index_name) - s = Series(np.random.randn(5), index[:5]) - if should_warn(s.index, df.index): - with tm.assert_produces_warning(RuntimeWarning): - res = pd.eval("s + df", engine=engine, parser=parser) - else: + df = tm.makeCustomDataframe( + 10, 7, data_gen_f=f, r_idx_type=r_idx_type, c_idx_type=c_idx_type + ) + index = getattr(df, index_name) + s = Series(np.random.randn(5), index[:5]) + if should_warn(s.index, df.index): + with tm.assert_produces_warning(RuntimeWarning): res = pd.eval("s + df", engine=engine, parser=parser) + else: + res = pd.eval("s + df", engine=engine, parser=parser) - if r_idx_type == "dt" or c_idx_type == "dt": - expected = df.add(s) if engine == "numexpr" else s + df - else: - expected = s + df - tm.assert_frame_equal(res, expected) + if r_idx_type == "dt" or c_idx_type == "dt": + expected = df.add(s) if engine == "numexpr" else s + df + else: + expected = s + df + tm.assert_frame_equal(res, expected) @pytest.mark.parametrize("c_idx_type", index_types) @pytest.mark.parametrize("r_idx_type", lhs_index_types)