Skip to content

Commit 76a912e

Browse files
committed
Mutate _backends in_get_plot_backend import top
1 parent 4554f44 commit 76a912e

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

pandas/plotting/_core.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
Sequence,
77
)
88

9+
# TODO: replace with `importlib.metadata` when python_requires >= 3.8.
10+
import pkg_resources
11+
912
from pandas._config import get_option
1013

1114
from pandas._typing import IndexLabel
@@ -1737,10 +1740,6 @@ def _load_backend(backend: str):
17371740
The identifier for the backend. Either an entrypoint item registered
17381741
with pkg_resources, "matplotlib", or a module name.
17391742
1740-
Notes
1741-
-----
1742-
Modifies _backends with imported backends as a side effect.
1743-
17441743
Returns
17451744
-------
17461745
types.ModuleType
@@ -1756,29 +1755,25 @@ def _load_backend(backend: str):
17561755
"matplotlib is required for plotting when the "
17571756
'default backend "matplotlib" is selected.'
17581757
) from None
1758+
return module
17591759

1760-
else:
1761-
module = None
1762-
# Delay import for performance.
1763-
# TODO: replace with `importlib.metadata` when python_requires >= 3.8.
1764-
from pkg_resources import iter_entry_points
1760+
module = None
17651761

1766-
for entry_point in iter_entry_points("pandas_plotting_backends"):
1767-
if entry_point.name == backend:
1768-
module = entry_point.load()
1762+
for entry_point in pkg_resources.iter_entry_points("pandas_plotting_backends"):
1763+
if entry_point.name == backend:
1764+
module = entry_point.load()
17691765

1770-
if module is None:
1771-
# Fall back to unregistered, module name approach.
1772-
try:
1773-
module = importlib.import_module(backend)
1774-
except ImportError:
1775-
# We re-raise later on.
1776-
pass
1766+
if module is None:
1767+
# Fall back to unregistered, module name approach.
1768+
try:
1769+
module = importlib.import_module(backend)
1770+
except ImportError:
1771+
# We re-raise later on.
1772+
pass
17771773

17781774
if hasattr(module, "plot"):
17791775
# Validate that the interface is implemented when the option is set,
17801776
# rather than at plot time.
1781-
_backends[backend] = module
17821777
return module
17831778

17841779
raise ValueError(
@@ -1802,10 +1797,16 @@ def _get_plot_backend(backend: str | None = None):
18021797
18031798
The backend is imported lazily, as matplotlib is a soft dependency, and
18041799
pandas can be used without it being installed.
1800+
1801+
Notes
1802+
-----
1803+
Modifies `_backends` with imported backend as a side effect.
18051804
"""
18061805
backend = backend or get_option("plotting.backend")
18071806

18081807
if backend in _backends:
18091808
return _backends[backend]
18101809

1811-
return _load_backend(backend)
1810+
module = _load_backend(backend)
1811+
_backends[backend] = module
1812+
return module

0 commit comments

Comments
 (0)