test_downstream.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. """
  2. Testing that we work in the downstream packages
  3. """
  4. import importlib
  5. import subprocess
  6. import sys
  7. import numpy as np # noqa
  8. import pytest
  9. from pandas import DataFrame, Series
  10. import pandas._testing as tm
  11. def import_module(name):
  12. # we *only* want to skip if the module is truly not available
  13. # and NOT just an actual import error because of pandas changes
  14. try:
  15. return importlib.import_module(name)
  16. except ModuleNotFoundError: # noqa
  17. pytest.skip("skipping as {} not available".format(name))
  18. @pytest.fixture
  19. def df():
  20. return DataFrame({"A": [1, 2, 3]})
  21. def test_dask(df):
  22. toolz = import_module("toolz") # noqa
  23. dask = import_module("dask") # noqa
  24. import dask.dataframe as dd
  25. ddf = dd.from_pandas(df, npartitions=3)
  26. assert ddf.A is not None
  27. assert ddf.compute() is not None
  28. @pytest.mark.filterwarnings("ignore:Panel class is removed")
  29. def test_xarray(df):
  30. xarray = import_module("xarray") # noqa
  31. assert df.to_xarray() is not None
  32. def test_oo_optimizable():
  33. # GH 21071
  34. subprocess.check_call([sys.executable, "-OO", "-c", "import pandas"])
  35. @tm.network
  36. # Cython import warning
  37. @pytest.mark.filterwarnings("ignore:can't:ImportWarning")
  38. @pytest.mark.filterwarnings(
  39. # patsy needs to update their imports
  40. "ignore:Using or importing the ABCs from 'collections:DeprecationWarning"
  41. )
  42. def test_statsmodels():
  43. statsmodels = import_module("statsmodels") # noqa
  44. import statsmodels.api as sm
  45. import statsmodels.formula.api as smf
  46. df = sm.datasets.get_rdataset("Guerry", "HistData").data
  47. smf.ols("Lottery ~ Literacy + np.log(Pop1831)", data=df).fit()
  48. # Cython import warning
  49. @pytest.mark.filterwarnings("ignore:can't:ImportWarning")
  50. def test_scikit_learn(df):
  51. sklearn = import_module("sklearn") # noqa
  52. from sklearn import svm, datasets
  53. digits = datasets.load_digits()
  54. clf = svm.SVC(gamma=0.001, C=100.0)
  55. clf.fit(digits.data[:-1], digits.target[:-1])
  56. clf.predict(digits.data[-1:])
  57. # Cython import warning and traitlets
  58. @tm.network
  59. @pytest.mark.filterwarnings("ignore")
  60. def test_seaborn():
  61. seaborn = import_module("seaborn")
  62. tips = seaborn.load_dataset("tips")
  63. seaborn.stripplot(x="day", y="total_bill", data=tips)
  64. def test_pandas_gbq(df):
  65. pandas_gbq = import_module("pandas_gbq") # noqa
  66. @pytest.mark.xfail(reason="0.7.0 pending")
  67. @tm.network
  68. def test_pandas_datareader():
  69. pandas_datareader = import_module("pandas_datareader") # noqa
  70. pandas_datareader.DataReader("F", "quandl", "2017-01-01", "2017-02-01")
  71. # importing from pandas, Cython import warning
  72. @pytest.mark.filterwarnings("ignore:can't resolve:ImportWarning")
  73. @pytest.mark.skip(reason="Anaconda installation issue - GH32144")
  74. def test_geopandas():
  75. geopandas = import_module("geopandas") # noqa
  76. fp = geopandas.datasets.get_path("naturalearth_lowres")
  77. assert geopandas.read_file(fp) is not None
  78. def test_geopandas_coordinate_indexer():
  79. # this test is included to have coverage of one case in the indexing.py
  80. # code that is only kept for compatibility with geopandas, see
  81. # https://github.com/pandas-dev/pandas/issues/27258
  82. # We should be able to remove this after some time when its usage is
  83. # removed in geopandas
  84. from pandas.core.indexing import _NDFrameIndexer
  85. class _CoordinateIndexer(_NDFrameIndexer):
  86. def _getitem_tuple(self, tup):
  87. obj = self.obj
  88. xs, ys = tup
  89. return obj[xs][ys]
  90. Series._create_indexer("cx", _CoordinateIndexer)
  91. s = Series(range(5))
  92. res = s.cx[:, :]
  93. tm.assert_series_equal(s, res)
  94. # Cython import warning
  95. @pytest.mark.filterwarnings("ignore:can't resolve:ImportWarning")
  96. @pytest.mark.filterwarnings("ignore:RangeIndex.* is deprecated:DeprecationWarning")
  97. def test_pyarrow(df):
  98. pyarrow = import_module("pyarrow") # noqa
  99. table = pyarrow.Table.from_pandas(df)
  100. result = table.to_pandas()
  101. tm.assert_frame_equal(result, df)
  102. @pytest.mark.xfail(reason="pandas-wheels-50", strict=False)
  103. def test_missing_required_dependency():
  104. # GH 23868
  105. # To ensure proper isolation, we pass these flags
  106. # -S : disable site-packages
  107. # -s : disable user site-packages
  108. # -E : disable PYTHON* env vars, especially PYTHONPATH
  109. # And, that's apparently not enough, so we give up.
  110. # https://github.com/MacPython/pandas-wheels/pull/50
  111. call = ["python", "-sSE", "-c", "import pandas"]
  112. with pytest.raises(subprocess.CalledProcessError) as exc:
  113. subprocess.check_output(call, stderr=subprocess.STDOUT)
  114. output = exc.value.stdout.decode()
  115. for name in ["numpy", "pytz", "dateutil"]:
  116. assert name in output