7
7
Sequence ,
8
8
)
9
9
10
- # TODO: replace with `importlib.metadata` when python_requires >= 3.8.
11
10
import pkg_resources
12
11
13
12
from pandas ._config import get_option
@@ -1731,7 +1730,7 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
1731
1730
_backends : dict [str , types .ModuleType ] = {}
1732
1731
1733
1732
1734
- def _load_backend (backend : str ):
1733
+ def _load_backend (backend : str ) -> types . ModuleType :
1735
1734
"""
1736
1735
Load a pandas plotting backend.
1737
1736
@@ -1746,36 +1745,39 @@ def _load_backend(backend: str):
1746
1745
types.ModuleType
1747
1746
The imported backend.
1748
1747
"""
1749
- module : types .ModuleType | None = None
1750
-
1751
1748
if backend == "matplotlib" :
1752
1749
# Because matplotlib is an optional dependency and first-party backend,
1753
1750
# we need to attempt an import here to raise an ImportError if needed.
1754
1751
try :
1755
- module = __import__ ("pandas.plotting._matplotlib" )
1752
+ module = importlib . import_module ("pandas.plotting._matplotlib" )
1756
1753
except ImportError :
1757
1754
raise ImportError (
1758
1755
"matplotlib is required for plotting when the "
1759
1756
'default backend "matplotlib" is selected.'
1760
1757
) from None
1761
1758
return module
1762
1759
1760
+ found_backend = False
1761
+
1763
1762
for entry_point in pkg_resources .iter_entry_points ("pandas_plotting_backends" ):
1764
- if entry_point .name == backend :
1763
+ found_backend = entry_point .name == backend
1764
+ if found_backend :
1765
1765
module = entry_point .load ()
1766
1766
1767
- if module is None :
1767
+ if not found_backend :
1768
1768
# Fall back to unregistered, module name approach.
1769
1769
try :
1770
1770
module = importlib .import_module (backend )
1771
+ found_backend = True
1771
1772
except ImportError :
1772
1773
# We re-raise later on.
1773
1774
pass
1774
1775
1775
- if hasattr (module , "plot" ):
1776
- # Validate that the interface is implemented when the option is set,
1777
- # rather than at plot time.
1778
- return module
1776
+ if found_backend :
1777
+ if hasattr (module , "plot" ):
1778
+ # Validate that the interface is implemented when the option is set,
1779
+ # rather than at plot time.
1780
+ return module
1779
1781
1780
1782
raise ValueError (
1781
1783
f"Could not find plotting backend '{ backend } '. Ensure that you've "
0 commit comments