Skip to content

Setting the plot backend should validate .plot immediately #28163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TomAugspurger opened this issue Aug 27, 2019 · 2 comments · Fixed by #28164
Closed

Setting the plot backend should validate .plot immediately #28163

TomAugspurger opened this issue Aug 27, 2019 · 2 comments · Fixed by #28164
Labels
Milestone

Comments

@TomAugspurger
Copy link
Contributor

Right now a pd.set_option('display.backend', name) will

  1. look for an entrypoint
  2. fall back to importing the module name and setting that to the backend

In the second case, the backend's implementation isn't validated until a plot is actually requested. We should validate that name.plot is a thing when the backend is set.

In [16]: module = types.ModuleType("foo")

In [17]: sys.modules['foo'] = module

In [18]: pd.set_option('plotting.backend', 'foo')

In [19]: df = pd.DataFrame({"A": [1, 2]})

In [20]: df.plot()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-848b80e64df8> in <module>
----> 1 df.plot()

~/sandbox/pandas/pandas/plotting/_core.py in __call__(self, *args, **kwargs)
    794                     data.columns = label_name
    795
--> 796         return plot_backend.plot(data, kind=kind, **kwargs)
    797
    798     def line(self, x=None, y=None, **kwargs):

AttributeError: module 'foo' has no attribute 'plot'

Rather than raising an AttributeError on df.plot, we should raise a ValueError on pd.set_option('plotting.backend', 'foo').

@TomAugspurger TomAugspurger added the Visualization plotting label Aug 27, 2019
TomAugspurger added a commit to TomAugspurger/pandas that referenced this issue Aug 27, 2019
```python
In [1]: import pandas as pd

In [2]: import sys

In [3]: import types

In [4]: module = types.ModuleType("foo")

In [5]: sys.modules['foo'] = module

In [6]: pd.set_option('plotting.backend', 'foo')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-123b6513deb2> in <module>
----> 1 pd.set_option('plotting.backend', 'foo')

...

~/sandbox/pandas/pandas/plotting/_core.py in _find_backend(backend)
   1588         "top-level `.plot` method."
   1589     )
-> 1590     raise ValueError(msg.format(name=backend))
   1591
   1592

ValueError: Could not find plotting backend 'foo'. Ensure that you've installed the package providing the 'foo' entrypoint, or that the package has atop-level `.plot` method.

```

Closes pandas-dev#28163
@TomAugspurger TomAugspurger added this to the 1.0 milestone Aug 27, 2019
@TomAugspurger
Copy link
Contributor Author

cc @datapythonista

@datapythonista
Copy link
Member

Makes sense to me, but I think it's trickier than it looks. My first implementation had some validations when setting the backend, but iirc @jreback preference was to not import the module until a plot is called. I think because matplotlib needs to be loaded even if plot() is never used, unless some hacky things are done.

TomAugspurger added a commit that referenced this issue Sep 3, 2019
* Validate plot backend when setting.

Closes #28163
proost pushed a commit to proost/pandas that referenced this issue Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this issue Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants