test_backend.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import sys
  2. import types
  3. import pkg_resources
  4. import pytest
  5. import pandas.util._test_decorators as td
  6. import pandas
  7. dummy_backend = types.ModuleType("pandas_dummy_backend")
  8. setattr(dummy_backend, "plot", lambda *args, **kwargs: "used_dummy")
  9. @pytest.fixture
  10. def restore_backend():
  11. """Restore the plotting backend to matplotlib"""
  12. pandas.set_option("plotting.backend", "matplotlib")
  13. yield
  14. pandas.set_option("plotting.backend", "matplotlib")
  15. def test_backend_is_not_module():
  16. msg = "Could not find plotting backend 'not_an_existing_module'."
  17. with pytest.raises(ValueError, match=msg):
  18. pandas.set_option("plotting.backend", "not_an_existing_module")
  19. assert pandas.options.plotting.backend == "matplotlib"
  20. def test_backend_is_correct(monkeypatch, restore_backend):
  21. monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend)
  22. pandas.set_option("plotting.backend", "pandas_dummy_backend")
  23. assert pandas.get_option("plotting.backend") == "pandas_dummy_backend"
  24. assert (
  25. pandas.plotting._core._get_plot_backend("pandas_dummy_backend") is dummy_backend
  26. )
  27. def test_backend_can_be_set_in_plot_call(monkeypatch, restore_backend):
  28. monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend)
  29. df = pandas.DataFrame([1, 2, 3])
  30. assert pandas.get_option("plotting.backend") == "matplotlib"
  31. assert df.plot(backend="pandas_dummy_backend") == "used_dummy"
  32. @td.skip_if_no_mpl
  33. def test_register_entrypoint(restore_backend):
  34. dist = pkg_resources.get_distribution("pandas")
  35. if dist.module_path not in pandas.__file__:
  36. # We are running from a non-installed pandas, and this test is invalid
  37. pytest.skip("Testing a non-installed pandas")
  38. mod = types.ModuleType("my_backend")
  39. mod.plot = lambda *args, **kwargs: 1
  40. backends = pkg_resources.get_entry_map("pandas")
  41. my_entrypoint = pkg_resources.EntryPoint(
  42. "pandas_plotting_backend", mod.__name__, dist=dist
  43. )
  44. backends["pandas_plotting_backends"]["my_backend"] = my_entrypoint
  45. # TODO: the docs recommend importlib.util.module_from_spec. But this works for now.
  46. sys.modules["my_backend"] = mod
  47. result = pandas.plotting._core._get_plot_backend("my_backend")
  48. assert result is mod
  49. # TODO: https://github.com/pandas-dev/pandas/issues/27517
  50. # Remove the td.skip_if_no_mpl
  51. with pandas.option_context("plotting.backend", "my_backend"):
  52. result = pandas.plotting._core._get_plot_backend()
  53. assert result is mod
  54. def test_setting_backend_without_plot_raises():
  55. # GH-28163
  56. module = types.ModuleType("pandas_plot_backend")
  57. sys.modules["pandas_plot_backend"] = module
  58. assert pandas.options.plotting.backend == "matplotlib"
  59. with pytest.raises(
  60. ValueError, match="Could not find plotting backend 'pandas_plot_backend'."
  61. ):
  62. pandas.set_option("plotting.backend", "pandas_plot_backend")
  63. assert pandas.options.plotting.backend == "matplotlib"
  64. @td.skip_if_mpl
  65. def test_no_matplotlib_ok():
  66. with pytest.raises(ImportError):
  67. pandas.plotting._core._get_plot_backend("matplotlib")
  68. def test_extra_kinds_ok(monkeypatch, restore_backend):
  69. # https://github.com/pandas-dev/pandas/pull/28647
  70. monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend)
  71. pandas.set_option("plotting.backend", "pandas_dummy_backend")
  72. df = pandas.DataFrame({"A": [1, 2, 3]})
  73. df.plot(kind="not a real kind")