Skip to content

Commit ec56627

Browse files
committed
ERR: Better error message for missing matplotlib
Closes #19810
1 parent 14889f1 commit ec56627

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ Offsets
958958

959959
Numeric
960960
^^^^^^^
961+
- Better error message when matplotlib is not installed (:issue:`19810`).
961962
- 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`)
962963
- 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`)
963964
- 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`)

pandas/plotting/_core.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
try:
4545
from pandas.plotting import _converter
4646
except ImportError:
47-
pass
47+
_HAS_MPL = False
4848
else:
49+
_HAS_MPL = True
4950
if get_option('plotting.matplotlib.register_converters'):
5051
_converter.register(explicit=True)
5152

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

101+
if not _HAS_MPL:
102+
raise ImportError("matplotlib is required for plotting.")
103+
100104
_converter._WARN = False
101105
self.data = data
102106
self.by = by
@@ -2264,6 +2268,9 @@ def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
22642268
... }, index= ['pig', 'rabbit', 'duck', 'chicken', 'horse'])
22652269
>>> hist = df.hist(bins=3)
22662270
"""
2271+
if not _HAS_MPL:
2272+
raise ImportError("matplotlib is required for plotting.")
2273+
22672274
_converter._WARN = False
22682275
if by is not None:
22692276
axes = grouped_hist(data, column=column, by=by, ax=ax, grid=grid,
@@ -2403,6 +2410,9 @@ def grouped_hist(data, column=None, by=None, ax=None, bins=50, figsize=None,
24032410
-------
24042411
axes: collection of Matplotlib Axes
24052412
"""
2413+
if not _HAS_MPL:
2414+
raise ImportError("matplotlib is required for plotting.")
2415+
24062416
_converter._WARN = False
24072417

24082418
def plot_group(group, ax):
@@ -2469,6 +2479,9 @@ def boxplot_frame_groupby(grouped, subplots=True, column=None, fontsize=None,
24692479
>>> grouped = df.unstack(level='lvl1').groupby(level=0, axis=1)
24702480
>>> boxplot_frame_groupby(grouped, subplots=False)
24712481
"""
2482+
if not _HAS_MPL:
2483+
raise ImportError("matplotlib is required for plotting.")
2484+
24722485
_converter._WARN = False
24732486
if subplots is True:
24742487
naxes = len(grouped)

pandas/tests/plotting/test_misc.py

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works
1818

1919

20+
@td.skip_if_mpl
21+
def test_import_error_message():
22+
df = DataFrame({"A": [1, 2]})
23+
24+
with tm.assert_raises_regex(ImportError, 'matplotlib is required'):
25+
df.plot()
26+
27+
2028
@td.skip_if_no_mpl
2129
class TestSeriesPlots(TestPlotBase):
2230

pandas/util/_test_decorators.py

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def decorated_func(func):
160160

161161
skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(),
162162
reason="Missing matplotlib dependency")
163+
skip_if_mpl = pytest.mark.skipif(not _skip_if_no_mpl(),
164+
reason="matplotlib is pressent")
163165
skip_if_mpl_1_5 = pytest.mark.skipif(_skip_if_mpl_1_5(),
164166
reason="matplotlib 1.5")
165167
xfail_if_mpl_2_2 = pytest.mark.xfail(_skip_if_mpl_2_2(),

0 commit comments

Comments
 (0)