test_backend.py 3.3 KB

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