test_backend.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import sys
  2. import types
  3. import pytest
  4. import pandas.util._test_decorators as td
  5. import pandas
  6. dummy_backend = types.ModuleType("pandas_dummy_backend")
  7. setattr(dummy_backend, "plot", lambda *args, **kwargs: "used_dummy")
  8. @pytest.fixture
  9. def restore_backend():
  10. """Restore the plotting backend to matplotlib"""
  11. with pandas.option_context("plotting.backend", "matplotlib"):
  12. yield
  13. def test_backend_is_not_module():
  14. msg = "Could not find plotting backend 'not_an_existing_module'."
  15. with pytest.raises(ValueError, match=msg):
  16. pandas.set_option("plotting.backend", "not_an_existing_module")
  17. assert pandas.options.plotting.backend == "matplotlib"
  18. def test_backend_is_correct(monkeypatch, restore_backend):
  19. monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend)
  20. pandas.set_option("plotting.backend", "pandas_dummy_backend")
  21. assert pandas.get_option("plotting.backend") == "pandas_dummy_backend"
  22. assert (
  23. pandas.plotting._core._get_plot_backend("pandas_dummy_backend") is dummy_backend
  24. )
  25. def test_backend_can_be_set_in_plot_call(monkeypatch, restore_backend):
  26. monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend)
  27. df = pandas.DataFrame([1, 2, 3])
  28. assert pandas.get_option("plotting.backend") == "matplotlib"
  29. assert df.plot(backend="pandas_dummy_backend") == "used_dummy"
  30. def test_register_entrypoint(restore_backend, tmp_path, monkeypatch):
  31. monkeypatch.syspath_prepend(tmp_path)
  32. monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend)
  33. dist_info = tmp_path / "my_backend-0.0.0.dist-info"
  34. dist_info.mkdir()
  35. # entry_point name should not match module name - otherwise pandas will
  36. # fall back to backend lookup by module name
  37. (dist_info / "entry_points.txt").write_bytes(
  38. b"[pandas_plotting_backends]\nmy_ep_backend = pandas_dummy_backend\n"
  39. )
  40. assert pandas.plotting._core._get_plot_backend("my_ep_backend") is dummy_backend
  41. with pandas.option_context("plotting.backend", "my_ep_backend"):
  42. assert pandas.plotting._core._get_plot_backend() is dummy_backend
  43. def test_setting_backend_without_plot_raises(monkeypatch):
  44. # GH-28163
  45. module = types.ModuleType("pandas_plot_backend")
  46. monkeypatch.setitem(sys.modules, "pandas_plot_backend", module)
  47. assert pandas.options.plotting.backend == "matplotlib"
  48. with pytest.raises(
  49. ValueError, match="Could not find plotting backend 'pandas_plot_backend'."
  50. ):
  51. pandas.set_option("plotting.backend", "pandas_plot_backend")
  52. assert pandas.options.plotting.backend == "matplotlib"
  53. @td.skip_if_mpl
  54. def test_no_matplotlib_ok():
  55. msg = (
  56. 'matplotlib is required for plotting when the default backend "matplotlib" is '
  57. "selected."
  58. )
  59. with pytest.raises(ImportError, match=msg):
  60. pandas.plotting._core._get_plot_backend("matplotlib")
  61. def test_extra_kinds_ok(monkeypatch, restore_backend):
  62. # https://github.com/pandas-dev/pandas/pull/28647
  63. monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend)
  64. pandas.set_option("plotting.backend", "pandas_dummy_backend")
  65. df = pandas.DataFrame({"A": [1, 2, 3]})
  66. df.plot(kind="not a real kind")