44import pytest
55import numpy as np # noqa
66from pandas import DataFrame
7+ from pandas .compat import PY3
78from pandas .util import testing as tm
9+ import importlib
10+
11+
12+ def import_module (name ):
13+ # we *only* want to skip if the module is truly not available
14+ # and NOT just an actual import error because of pandas changes
15+
16+ if PY3 :
17+ try :
18+ return importlib .import_module (name )
19+ except ModuleNotFoundError : # noqa
20+ pytest .skip ("skipping as {} not available" .format (name ))
21+
22+ else :
23+ try :
24+ return importlib .import_module (name )
25+ except ImportError as e :
26+ if "No module named {}" .format (name ) in str (e ):
27+ pytest .skip ("skipping as {} not available" .format (name ))
28+ raise
829
930
1031@pytest .fixture
@@ -14,8 +35,8 @@ def df():
1435
1536def test_dask (df ):
1637
17- toolz = pytest . importorskip ('toolz' ) # noqa
18- dask = pytest . importorskip ('dask' ) # noqa
38+ toolz = import_module ('toolz' ) # noqa
39+ dask = import_module ('dask' ) # noqa
1940
2041 import dask .dataframe as dd
2142
@@ -26,14 +47,14 @@ def test_dask(df):
2647
2748def test_xarray (df ):
2849
29- xarray = pytest . importorskip ('xarray' ) # noqa
50+ xarray = import_module ('xarray' ) # noqa
3051
3152 assert df .to_xarray () is not None
3253
3354
3455def test_statsmodels ():
3556
36- statsmodels = pytest . importorskip ('statsmodels' ) # noqa
57+ statsmodels = import_module ('statsmodels' ) # noqa
3758 import statsmodels .api as sm
3859 import statsmodels .formula .api as smf
3960 df = sm .datasets .get_rdataset ("Guerry" , "HistData" ).data
@@ -42,7 +63,7 @@ def test_statsmodels():
4263
4364def test_scikit_learn (df ):
4465
45- sklearn = pytest . importorskip ('sklearn' ) # noqa
66+ sklearn = import_module ('sklearn' ) # noqa
4667 from sklearn import svm , datasets
4768
4869 digits = datasets .load_digits ()
@@ -53,33 +74,34 @@ def test_scikit_learn(df):
5374
5475def test_seaborn ():
5576
56- seaborn = pytest . importorskip ('seaborn' )
77+ seaborn = import_module ('seaborn' )
5778 tips = seaborn .load_dataset ("tips" )
5879 seaborn .stripplot (x = "day" , y = "total_bill" , data = tips )
5980
6081
6182def test_pandas_gbq (df ):
6283
63- pandas_gbq = pytest . importorskip ( 'pandas-gbq ' ) # noqa
84+ pandas_gbq = import_module ( 'pandas_gbq ' ) # noqa
6485
6586
66- @tm .network
87+ @pytest .mark .xfail (reason = ("pandas_datareader<=0.3.0 "
88+ "broken w.r.t. pandas >= 0.20.0" ))
6789def test_pandas_datareader ():
6890
69- pandas_datareader = pytest . importorskip ( 'pandas-datareader ' ) # noqa
91+ pandas_datareader = import_module ( 'pandas_datareader ' ) # noqa
7092 pandas_datareader .get_data_yahoo ('AAPL' )
7193
7294
7395def test_geopandas ():
7496
75- geopandas = pytest . importorskip ('geopandas' ) # noqa
97+ geopandas = import_module ('geopandas' ) # noqa
7698 fp = geopandas .datasets .get_path ('naturalearth_lowres' )
7799 assert geopandas .read_file (fp ) is not None
78100
79101
80102def test_pyarrow (df ):
81103
82- pyarrow = pytest . importorskip ('pyarrow' ) # noqa
104+ pyarrow = import_module ('pyarrow' ) # noqa
83105 table = pyarrow .Table .from_pandas (df )
84106 result = table .to_pandas ()
85107 tm .assert_frame_equal (result , df )
0 commit comments