From 6911891b21032954f13c5552afc97d38767ab62a Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 31 Dec 2021 17:20:14 -0800 Subject: [PATCH 1/2] TST: Use pytest or decorator instead of checking ImportError --- pandas/tests/computation/test_compat.py | 25 ++++------- pandas/tests/frame/test_reductions.py | 55 ++++++++++--------------- 2 files changed, 30 insertions(+), 50 deletions(-) diff --git a/pandas/tests/computation/test_compat.py b/pandas/tests/computation/test_compat.py index 5e76fa4cbdb4f..051afc6a2a867 100644 --- a/pandas/tests/computation/test_compat.py +++ b/pandas/tests/computation/test_compat.py @@ -13,16 +13,13 @@ def test_compat(): from pandas.core.computation.check import NUMEXPR_INSTALLED - try: - import numexpr as ne + ne = pytest.importorskip("numexpr") - ver = ne.__version__ - if Version(ver) < Version(VERSIONS["numexpr"]): - assert not NUMEXPR_INSTALLED - else: - assert NUMEXPR_INSTALLED - except ImportError: - pytest.skip("not testing numexpr version compat") + ver = ne.__version__ + if Version(ver) < Version(VERSIONS["numexpr"]): + assert not NUMEXPR_INSTALLED + else: + assert NUMEXPR_INSTALLED @pytest.mark.parametrize("engine", ENGINES) @@ -34,11 +31,5 @@ def testit(): assert res == 3 if engine == "numexpr": - try: - import numexpr as ne # noqa:F401 - except ImportError: - pytest.skip("no numexpr") - else: - testit() - else: - testit() + pytest.importorskip("numexpr") + testit() diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 391a3df5233d1..245e54d665745 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -285,16 +285,11 @@ def test_stat_op_api(self, float_frame, float_string_frame): assert_stat_op_api("sem", float_frame, float_string_frame) assert_stat_op_api("median", float_frame, float_string_frame) - try: - from scipy.stats import ( # noqa:F401 - kurtosis, - skew, - ) - - assert_stat_op_api("skew", float_frame, float_string_frame) - assert_stat_op_api("kurt", float_frame, float_string_frame) - except ImportError: - pass + @pytest.mark.filterwarnings("ignore:Dropping of nuisance:FutureWarning") + @td.skip_if_no_scipy + def test_stat_op_api_skew_kurt(self, float_frame, float_string_frame): + assert_stat_op_api("skew", float_frame, float_string_frame) + assert_stat_op_api("kurt", float_frame, float_string_frame) def test_stat_op_calc(self, float_frame_with_na, mixed_float_frame): def count(s): @@ -315,20 +310,6 @@ def std(x): def sem(x): return np.std(x, ddof=1) / np.sqrt(len(x)) - def skewness(x): - from scipy.stats import skew # noqa:F811 - - if len(x) < 3: - return np.nan - return skew(x, bias=False) - - def kurt(x): - from scipy.stats import kurtosis # noqa:F811 - - if len(x) < 4: - return np.nan - return kurtosis(x, bias=False) - assert_stat_op_calc( "nunique", nunique, @@ -371,16 +352,24 @@ def kurt(x): check_dates=True, ) - try: - from scipy import ( # noqa:F401 - kurtosis, - skew, - ) + @td.skip_if_no_scipy + def test_stat_op_calc_skew_kurtosis(self, float_frame_with_na): + def skewness(x): + from scipy.stats import skew + + if len(x) < 3: + return np.nan + return skew(x, bias=False) + + def kurt(x): + from scipy.stats import kurtosis + + if len(x) < 4: + return np.nan + return kurtosis(x, bias=False) - assert_stat_op_calc("skew", skewness, float_frame_with_na) - assert_stat_op_calc("kurt", kurt, float_frame_with_na) - except ImportError: - pass + assert_stat_op_calc("skew", skewness, float_frame_with_na) + assert_stat_op_calc("kurt", kurt, float_frame_with_na) # TODO: Ensure warning isn't emitted in the first place # ignore mean of empty slice and all-NaN From 57645532756a4e7e833cd308cd73cb1968ed471a Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 1 Jan 2022 22:18:13 -0800 Subject: [PATCH 2/2] simplify test --- pandas/tests/computation/test_compat.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pandas/tests/computation/test_compat.py b/pandas/tests/computation/test_compat.py index 051afc6a2a867..cfc08426f84e3 100644 --- a/pandas/tests/computation/test_compat.py +++ b/pandas/tests/computation/test_compat.py @@ -9,7 +9,7 @@ def test_compat(): - # test we have compat with our version of nu + # test we have compat with our version of numexpr from pandas.core.computation.check import NUMEXPR_INSTALLED @@ -25,11 +25,8 @@ def test_compat(): @pytest.mark.parametrize("engine", ENGINES) @pytest.mark.parametrize("parser", expr.PARSERS) def test_invalid_numexpr_version(engine, parser): - def testit(): - a, b = 1, 2 # noqa:F841 - res = pd.eval("a + b", engine=engine, parser=parser) - assert res == 3 - if engine == "numexpr": pytest.importorskip("numexpr") - testit() + a, b = 1, 2 # noqa:F841 + res = pd.eval("a + b", engine=engine, parser=parser) + assert res == 3