Skip to content

Commit b00df5e

Browse files
committed
BUG: add raise_on_error to plot when handling errors raised by attempting to plot non-numeric columns #3287
1 parent d635f38 commit b00df5e

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

pandas/tests/test_graphics.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,12 @@ def test_nonnumeric_exclude(self):
239239
import matplotlib.pyplot as plt
240240
plt.close('all')
241241

242-
ax = DataFrame({'A': ["x", "y", "z"], 'B': [1,2,3]}).plot() # it works
242+
df = DataFrame({'A': ["x", "y", "z"], 'B': [1,2,3]})
243+
ax = df.plot(raise_on_error=False) # it works
243244
self.assert_(len(ax.get_lines()) == 1) #B was plotted
244245

246+
self.assertRaises(Exception, df.plot)
247+
245248
@slow
246249
def test_label(self):
247250
import matplotlib.pyplot as plt

pandas/tools/plotting.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,10 @@ class MPLPlot(object):
691691
"""
692692
_default_rot = 0
693693

694-
_pop_attributes = ['label', 'style', 'logy', 'logx', 'loglog']
695-
_attr_defaults = {'logy': False, 'logx': False, 'loglog': False}
694+
_pop_attributes = ['label', 'style', 'logy', 'logx', 'loglog',
695+
'raise_on_error']
696+
_attr_defaults = {'logy': False, 'logx': False, 'loglog': False,
697+
'raise_on_error': True}
696698

697699
def __init__(self, data, kind=None, by=None, subplots=False, sharex=True,
698700
sharey=False, use_index=True,
@@ -1185,7 +1187,12 @@ def _make_plot(self):
11851187
except AttributeError as inst: # non-numeric
11861188
msg = ('Unable to plot data %s vs index %s,\n'
11871189
'error was: %s' % (str(y), str(x), str(inst)))
1188-
print msg
1190+
if not self.raise_on_error:
1191+
print msg
1192+
else:
1193+
msg = msg + ('\nConsider setting raise_on_error=False'
1194+
'to suppress')
1195+
raise Exception(msg)
11891196

11901197
self._make_legend(lines, labels)
11911198

@@ -1214,7 +1221,12 @@ def _plot(data, col_num, ax, label, style, **kwds):
12141221
except AttributeError as inst: #non-numeric
12151222
msg = ('Unable to plot %s,\n'
12161223
'error was: %s' % (str(data), str(inst)))
1217-
print msg
1224+
if not self.raise_on_error:
1225+
print msg
1226+
else:
1227+
msg = msg + ('\nConsider setting raise_on_error=False'
1228+
'to suppress')
1229+
raise Exception(msg)
12181230

12191231
if isinstance(data, Series):
12201232
ax = self._get_ax(0) # self.axes[0]

pandas/tseries/tests/test_plotting.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,17 @@ def test_nonnumeric_exclude(self):
8787

8888
idx = date_range('1/1/1987', freq='A', periods=3)
8989
df = DataFrame({'A': ["x", "y", "z"], 'B': [1,2,3]}, idx)
90-
ax = df.plot() # it works
90+
self.assertRaises(Exception, df.plot)
91+
92+
plt.close('all')
93+
ax = df.plot(raise_on_error=False) # it works
9194
self.assert_(len(ax.get_lines()) == 1) #B was plotted
9295

9396
plt.close('all')
94-
ax = df['A'].plot() # it works
97+
self.assertRaises(Exception, df.A.plot)
98+
99+
plt.close('all')
100+
ax = df['A'].plot(raise_on_error=False) # it works
95101
self.assert_(len(ax.get_lines()) == 0)
96102

97103
@slow

0 commit comments

Comments
 (0)