Skip to content

Commit 9922e7f

Browse files
committed
PERF: load plotting entrypoint only when necessary
1 parent f7dd14b commit 9922e7f

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

pandas/plotting/_core.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ def _get_call_args(backend_name, data, args, kwargs):
865865
if args and isinstance(data, ABCSeries):
866866
positional_args = str(args)[1:-1]
867867
keyword_args = ", ".join(
868-
f"{name}={repr(value)}" for (name, default), value in zip(arg_def, args)
868+
f"{name}={repr(value)}" for (name, _), value in zip(arg_def, args)
869869
)
870870
msg = (
871871
"`Series.plot()` should not be called with positional "
@@ -876,7 +876,7 @@ def _get_call_args(backend_name, data, args, kwargs):
876876
)
877877
raise TypeError(msg)
878878

879-
pos_args = {name: value for value, (name, _) in zip(args, arg_def)}
879+
pos_args = {name: value for (name, _), value in zip(arg_def, args)}
880880
if backend_name == "pandas.plotting._matplotlib":
881881
kwargs = dict(arg_def, **pos_args, **kwargs)
882882
else:
@@ -1729,7 +1729,7 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
17291729

17301730
def _find_backend(backend: str):
17311731
"""
1732-
Find a pandas plotting backend>
1732+
Find a pandas plotting backend.
17331733
17341734
Parameters
17351735
----------
@@ -1749,11 +1749,8 @@ def _find_backend(backend: str):
17491749
import pkg_resources # Delay import for performance.
17501750

17511751
for entry_point in pkg_resources.iter_entry_points("pandas_plotting_backends"):
1752-
if entry_point.name == "matplotlib":
1753-
# matplotlib is an optional dependency. When
1754-
# missing, this would raise.
1755-
continue
1756-
_backends[entry_point.name] = entry_point.load()
1752+
if entry_point.name == backend:
1753+
_backends[entry_point.name] = entry_point.load()
17571754

17581755
try:
17591756
return _backends[backend]
@@ -1778,21 +1775,26 @@ def _find_backend(backend: str):
17781775
)
17791776

17801777

1781-
def _get_plot_backend(backend=None):
1778+
def _get_plot_backend(backend: str | None = None):
17821779
"""
17831780
Return the plotting backend to use (e.g. `pandas.plotting._matplotlib`).
17841781
1785-
The plotting system of pandas has been using matplotlib, but the idea here
1786-
is that it can also work with other third-party backends. In the future,
1787-
this function will return the backend from a pandas option, and all the
1788-
rest of the code in this file will use the backend specified there for the
1789-
plotting.
1782+
The plotting system of pandas uses matplotlib by default, but the idea here
1783+
is that it can also work with other third-party backends. This function
1784+
returns the module which provides a top-level `.plot` method that will
1785+
actually do the plotting. The backend is specified from a string, which
1786+
either comes from the keyword argument `backend`, or, if not specified, from
1787+
the option `pandas.options.plotting.backend`. All the rest of the code in
1788+
this file uses the backend specified there for the plotting.
17901789
17911790
The backend is imported lazily, as matplotlib is a soft dependency, and
17921791
pandas can be used without it being installed.
17931792
"""
17941793
backend = backend or get_option("plotting.backend")
17951794

1795+
if backend in _backends:
1796+
return _backends[backend]
1797+
17961798
if backend == "matplotlib":
17971799
# Because matplotlib is an optional dependency and first-party backend,
17981800
# we need to attempt an import here to raise an ImportError if needed.
@@ -1805,10 +1807,7 @@ def _get_plot_backend(backend=None):
18051807
) from None
18061808

18071809
_backends["matplotlib"] = module
1808-
1809-
if backend in _backends:
1810-
return _backends[backend]
1810+
return module
18111811

18121812
module = _find_backend(backend)
1813-
_backends[backend] = module
18141813
return module

0 commit comments

Comments
 (0)