Skip to content

Commit 0c59142

Browse files
authored
TST: Use pytest or decorator instead of checking ImportError (#45152)
* TST: Use pytest or decorator instead of checking ImportError * simplify test
1 parent 9a4fcea commit 0c59142

File tree

2 files changed

+33
-56
lines changed

2 files changed

+33
-56
lines changed

pandas/tests/computation/test_compat.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,24 @@
99

1010

1111
def test_compat():
12-
# test we have compat with our version of nu
12+
# test we have compat with our version of numexpr
1313

1414
from pandas.core.computation.check import NUMEXPR_INSTALLED
1515

16-
try:
17-
import numexpr as ne
16+
ne = pytest.importorskip("numexpr")
1817

19-
ver = ne.__version__
20-
if Version(ver) < Version(VERSIONS["numexpr"]):
21-
assert not NUMEXPR_INSTALLED
22-
else:
23-
assert NUMEXPR_INSTALLED
24-
except ImportError:
25-
pytest.skip("not testing numexpr version compat")
18+
ver = ne.__version__
19+
if Version(ver) < Version(VERSIONS["numexpr"]):
20+
assert not NUMEXPR_INSTALLED
21+
else:
22+
assert NUMEXPR_INSTALLED
2623

2724

2825
@pytest.mark.parametrize("engine", ENGINES)
2926
@pytest.mark.parametrize("parser", expr.PARSERS)
3027
def test_invalid_numexpr_version(engine, parser):
31-
def testit():
32-
a, b = 1, 2 # noqa:F841
33-
res = pd.eval("a + b", engine=engine, parser=parser)
34-
assert res == 3
35-
3628
if engine == "numexpr":
37-
try:
38-
import numexpr as ne # noqa:F401
39-
except ImportError:
40-
pytest.skip("no numexpr")
41-
else:
42-
testit()
43-
else:
44-
testit()
29+
pytest.importorskip("numexpr")
30+
a, b = 1, 2 # noqa:F841
31+
res = pd.eval("a + b", engine=engine, parser=parser)
32+
assert res == 3

pandas/tests/frame/test_reductions.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,11 @@ def test_stat_op_api(self, float_frame, float_string_frame):
285285
assert_stat_op_api("sem", float_frame, float_string_frame)
286286
assert_stat_op_api("median", float_frame, float_string_frame)
287287

288-
try:
289-
from scipy.stats import ( # noqa:F401
290-
kurtosis,
291-
skew,
292-
)
293-
294-
assert_stat_op_api("skew", float_frame, float_string_frame)
295-
assert_stat_op_api("kurt", float_frame, float_string_frame)
296-
except ImportError:
297-
pass
288+
@pytest.mark.filterwarnings("ignore:Dropping of nuisance:FutureWarning")
289+
@td.skip_if_no_scipy
290+
def test_stat_op_api_skew_kurt(self, float_frame, float_string_frame):
291+
assert_stat_op_api("skew", float_frame, float_string_frame)
292+
assert_stat_op_api("kurt", float_frame, float_string_frame)
298293

299294
def test_stat_op_calc(self, float_frame_with_na, mixed_float_frame):
300295
def count(s):
@@ -315,20 +310,6 @@ def std(x):
315310
def sem(x):
316311
return np.std(x, ddof=1) / np.sqrt(len(x))
317312

318-
def skewness(x):
319-
from scipy.stats import skew # noqa:F811
320-
321-
if len(x) < 3:
322-
return np.nan
323-
return skew(x, bias=False)
324-
325-
def kurt(x):
326-
from scipy.stats import kurtosis # noqa:F811
327-
328-
if len(x) < 4:
329-
return np.nan
330-
return kurtosis(x, bias=False)
331-
332313
assert_stat_op_calc(
333314
"nunique",
334315
nunique,
@@ -371,16 +352,24 @@ def kurt(x):
371352
check_dates=True,
372353
)
373354

374-
try:
375-
from scipy import ( # noqa:F401
376-
kurtosis,
377-
skew,
378-
)
355+
@td.skip_if_no_scipy
356+
def test_stat_op_calc_skew_kurtosis(self, float_frame_with_na):
357+
def skewness(x):
358+
from scipy.stats import skew
359+
360+
if len(x) < 3:
361+
return np.nan
362+
return skew(x, bias=False)
363+
364+
def kurt(x):
365+
from scipy.stats import kurtosis
366+
367+
if len(x) < 4:
368+
return np.nan
369+
return kurtosis(x, bias=False)
379370

380-
assert_stat_op_calc("skew", skewness, float_frame_with_na)
381-
assert_stat_op_calc("kurt", kurt, float_frame_with_na)
382-
except ImportError:
383-
pass
371+
assert_stat_op_calc("skew", skewness, float_frame_with_na)
372+
assert_stat_op_calc("kurt", kurt, float_frame_with_na)
384373

385374
# TODO: Ensure warning isn't emitted in the first place
386375
# ignore mean of empty slice and all-NaN

0 commit comments

Comments
 (0)