test_downstream.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. import pandas.util._test_decorators as td
  10. from pandas import DataFrame
  11. import pandas._testing as tm
  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. try:
  16. return importlib.import_module(name)
  17. except ModuleNotFoundError:
  18. pytest.skip(f"skipping as {name} not available")
  19. @pytest.fixture
  20. def df():
  21. return DataFrame({"A": [1, 2, 3]})
  22. # TODO(ArrayManager) dask is still accessing the blocks
  23. # https://github.com/dask/dask/pull/7318
  24. @td.skip_array_manager_not_yet_implemented
  25. def test_dask(df):
  26. toolz = import_module("toolz") # noqa
  27. dask = import_module("dask") # noqa
  28. import dask.dataframe as dd
  29. ddf = dd.from_pandas(df, npartitions=3)
  30. assert ddf.A is not None
  31. assert ddf.compute() is not None
  32. def test_xarray(df):
  33. xarray = import_module("xarray") # noqa
  34. assert df.to_xarray() is not None
  35. @td.skip_if_no("cftime")
  36. @td.skip_if_no("xarray", "0.10.4")
  37. def test_xarray_cftimeindex_nearest():
  38. # https://github.com/pydata/xarray/issues/3751
  39. import cftime
  40. import xarray
  41. times = xarray.cftime_range("0001", periods=2)
  42. result = times.get_loc(cftime.DatetimeGregorian(2000, 1, 1), method="nearest")
  43. expected = 1
  44. assert result == expected
  45. def test_oo_optimizable():
  46. # GH 21071
  47. subprocess.check_call([sys.executable, "-OO", "-c", "import pandas"])
  48. @tm.network
  49. # Cython import warning
  50. @pytest.mark.filterwarnings("ignore:pandas.util.testing is deprecated")
  51. @pytest.mark.filterwarnings("ignore:can't:ImportWarning")
  52. @pytest.mark.filterwarnings(
  53. # patsy needs to update their imports
  54. "ignore:Using or importing the ABCs from 'collections:DeprecationWarning"
  55. )
  56. def test_statsmodels():
  57. statsmodels = import_module("statsmodels") # noqa
  58. import statsmodels.api as sm
  59. import statsmodels.formula.api as smf
  60. df = sm.datasets.get_rdataset("Guerry", "HistData").data
  61. smf.ols("Lottery ~ Literacy + np.log(Pop1831)", data=df).fit()
  62. # Cython import warning
  63. @pytest.mark.filterwarnings("ignore:can't:ImportWarning")
  64. def test_scikit_learn(df):
  65. sklearn = import_module("sklearn") # noqa
  66. from sklearn import (
  67. datasets,
  68. svm,
  69. )
  70. digits = datasets.load_digits()
  71. clf = svm.SVC(gamma=0.001, C=100.0)
  72. clf.fit(digits.data[:-1], digits.target[:-1])
  73. clf.predict(digits.data[-1:])
  74. # Cython import warning and traitlets
  75. @tm.network
  76. @pytest.mark.filterwarnings("ignore")
  77. def test_seaborn():
  78. seaborn = import_module("seaborn")
  79. tips = seaborn.load_dataset("tips")
  80. seaborn.stripplot(x="day", y="total_bill", data=tips)
  81. def test_pandas_gbq(df):
  82. pandas_gbq = import_module("pandas_gbq") # noqa
  83. @pytest.mark.xfail(reason="0.8.1 tries to import urlencode from pd.io.common")
  84. @tm.network
  85. def test_pandas_datareader():
  86. pandas_datareader = import_module("pandas_datareader")
  87. pandas_datareader.DataReader("F", "quandl", "2017-01-01", "2017-02-01")
  88. # importing from pandas, Cython import warning
  89. @pytest.mark.filterwarnings("ignore:can't resolve:ImportWarning")
  90. def test_geopandas():
  91. geopandas = import_module("geopandas")
  92. fp = geopandas.datasets.get_path("naturalearth_lowres")
  93. assert geopandas.read_file(fp) is not None
  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")
  99. table = pyarrow.Table.from_pandas(df)
  100. result = table.to_pandas()
  101. tm.assert_frame_equal(result, df)
  102. def test_missing_required_dependency():
  103. # GH 23868
  104. # To ensure proper isolation, we pass these flags
  105. # -S : disable site-packages
  106. # -s : disable user site-packages
  107. # -E : disable PYTHON* env vars, especially PYTHONPATH
  108. # https://github.com/MacPython/pandas-wheels/pull/50
  109. pyexe = sys.executable.replace("\\", "/")
  110. # We skip this test if pandas is installed as a site package. We first
  111. # import the package normally and check the path to the module before
  112. # executing the test which imports pandas with site packages disabled.
  113. call = [pyexe, "-c", "import pandas;print(pandas.__file__)"]
  114. output = subprocess.check_output(call).decode()
  115. if "site-packages" in output:
  116. pytest.skip("pandas installed as site package")
  117. # This test will fail if pandas is installed as a site package. The flags
  118. # prevent pandas being imported and the test will report Failed: DID NOT
  119. # RAISE <class 'subprocess.CalledProcessError'>
  120. call = [pyexe, "-sSE", "-c", "import pandas"]
  121. msg = (
  122. rf"Command '\['{pyexe}', '-sSE', '-c', 'import pandas'\]' "
  123. "returned non-zero exit status 1."
  124. )
  125. with pytest.raises(subprocess.CalledProcessError, match=msg) as exc:
  126. subprocess.check_output(call, stderr=subprocess.STDOUT)
  127. output = exc.value.stdout.decode()
  128. for name in ["numpy", "pytz", "dateutil"]:
  129. assert name in output