6
6
Sequence ,
7
7
)
8
8
9
+ # TODO: replace with `importlib.metadata` when python_requires >= 3.8.
10
+ import pkg_resources
11
+
9
12
from pandas ._config import get_option
10
13
11
14
from pandas ._typing import IndexLabel
@@ -1737,10 +1740,6 @@ def _load_backend(backend: str):
1737
1740
The identifier for the backend. Either an entrypoint item registered
1738
1741
with pkg_resources, "matplotlib", or a module name.
1739
1742
1740
- Notes
1741
- -----
1742
- Modifies _backends with imported backends as a side effect.
1743
-
1744
1743
Returns
1745
1744
-------
1746
1745
types.ModuleType
@@ -1756,29 +1755,25 @@ def _load_backend(backend: str):
1756
1755
"matplotlib is required for plotting when the "
1757
1756
'default backend "matplotlib" is selected.'
1758
1757
) from None
1758
+ return module
1759
1759
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
1765
1761
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 ()
1769
1765
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
1777
1773
1778
1774
if hasattr (module , "plot" ):
1779
1775
# Validate that the interface is implemented when the option is set,
1780
1776
# rather than at plot time.
1781
- _backends [backend ] = module
1782
1777
return module
1783
1778
1784
1779
raise ValueError (
@@ -1802,10 +1797,16 @@ def _get_plot_backend(backend: str | None = None):
1802
1797
1803
1798
The backend is imported lazily, as matplotlib is a soft dependency, and
1804
1799
pandas can be used without it being installed.
1800
+
1801
+ Notes
1802
+ -----
1803
+ Modifies `_backends` with imported backend as a side effect.
1805
1804
"""
1806
1805
backend = backend or get_option ("plotting.backend" )
1807
1806
1808
1807
if backend in _backends :
1809
1808
return _backends [backend ]
1810
1809
1811
- return _load_backend (backend )
1810
+ module = _load_backend (backend )
1811
+ _backends [backend ] = module
1812
+ return module
0 commit comments