Skip to content

ERR: Better error message for missing matplotlib #20538

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

Merged
merged 5 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ Offsets

Numeric
^^^^^^^
- Better error message when attempting to plot but matplotlib is not installed (:issue:`19810`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to Plotting section

- Bug in :meth:`DataFrame.rank` and :meth:`Series.rank` when ``method='dense'`` and ``pct=True`` in which percentile ranks were not being used with the number of distinct observations (:issue:`15630`)
- Bug in :class:`Series` constructor with an int or float list where specifying ``dtype=str``, ``dtype='str'`` or ``dtype='U'`` failed to convert the data elements to strings (:issue:`16605`)
- Bug in :class:`Index` multiplication and division methods where operating with a ``Series`` would return an ``Index`` object instead of a ``Series`` object (:issue:`19042`)
Expand Down
15 changes: 14 additions & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
try:
from pandas.plotting import _converter
except ImportError:
pass
_HAS_MPL = False
else:
_HAS_MPL = True
if get_option('plotting.matplotlib.register_converters'):
_converter.register(explicit=True)

Expand Down Expand Up @@ -97,6 +98,9 @@ def __init__(self, data, kind=None, by=None, subplots=False, sharex=None,
secondary_y=False, colormap=None,
table=False, layout=None, **kwds):

if not _HAS_MPL:
raise ImportError("matplotlib is required for plotting.")
Copy link
Member

@gfyoung gfyoung Mar 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just write a separate function for this check? You use this same code 4 times.


_converter._WARN = False
self.data = data
self.by = by
Expand Down Expand Up @@ -2264,6 +2268,9 @@ def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
... }, index= ['pig', 'rabbit', 'duck', 'chicken', 'horse'])
>>> hist = df.hist(bins=3)
"""
if not _HAS_MPL:
raise ImportError("matplotlib is required for plotting.")

_converter._WARN = False
if by is not None:
axes = grouped_hist(data, column=column, by=by, ax=ax, grid=grid,
Expand Down Expand Up @@ -2403,6 +2410,9 @@ def grouped_hist(data, column=None, by=None, ax=None, bins=50, figsize=None,
-------
axes: collection of Matplotlib Axes
"""
if not _HAS_MPL:
raise ImportError("matplotlib is required for plotting.")

_converter._WARN = False

def plot_group(group, ax):
Expand Down Expand Up @@ -2469,6 +2479,9 @@ def boxplot_frame_groupby(grouped, subplots=True, column=None, fontsize=None,
>>> grouped = df.unstack(level='lvl1').groupby(level=0, axis=1)
>>> boxplot_frame_groupby(grouped, subplots=False)
"""
if not _HAS_MPL:
raise ImportError("matplotlib is required for plotting.")

_converter._WARN = False
if subplots is True:
naxes = len(grouped)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works


@td.skip_if_mpl
def test_import_error_message():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reference the issue number here.

df = DataFrame({"A": [1, 2]})

with tm.assert_raises_regex(ImportError, 'matplotlib is required'):
df.plot()


@td.skip_if_no_mpl
class TestSeriesPlots(TestPlotBase):

Expand Down
2 changes: 2 additions & 0 deletions pandas/util/_test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def decorated_func(func):

skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(),
reason="Missing matplotlib dependency")
skip_if_mpl = pytest.mark.skipif(not _skip_if_no_mpl(),
reason="matplotlib is pressent")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test build that does not have matplotlib ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pressent --> present

skip_if_mpl_1_5 = pytest.mark.skipif(_skip_if_mpl_1_5(),
reason="matplotlib 1.5")
xfail_if_mpl_2_2 = pytest.mark.xfail(_skip_if_mpl_2_2(),
Expand Down