Skip to content

Commit d635f38

Browse files
committed
BUG: suppress error raise by nonnumeric columns when plotting DataFrame #3108
1 parent 9dcac7e commit d635f38

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

pandas/tests/test_graphics.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ def test_plot(self):
234234
index=index)
235235
_check_plot_works(df.plot, title=u'\u03A3')
236236

237+
@slow
238+
def test_nonnumeric_exclude(self):
239+
import matplotlib.pyplot as plt
240+
plt.close('all')
241+
242+
ax = DataFrame({'A': ["x", "y", "z"], 'B': [1,2,3]}).plot() # it works
243+
self.assert_(len(ax.get_lines()) == 1) #B was plotted
244+
237245
@slow
238246
def test_label(self):
239247
import matplotlib.pyplot as plt

pandas/tools/plotting.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,17 +1170,22 @@ def _make_plot(self):
11701170
else:
11711171
args = (ax, x, y, style)
11721172

1173-
newline = plotf(*args, **kwds)[0]
1174-
lines.append(newline)
1175-
leg_label = label
1176-
if self.mark_right and self.on_right(i):
1177-
leg_label += ' (right)'
1178-
labels.append(leg_label)
1179-
ax.grid(self.grid)
1180-
1181-
if self._is_datetype():
1182-
left, right = _get_xlim(lines)
1183-
ax.set_xlim(left, right)
1173+
try:
1174+
newline = plotf(*args, **kwds)[0]
1175+
lines.append(newline)
1176+
leg_label = label
1177+
if self.mark_right and self.on_right(i):
1178+
leg_label += ' (right)'
1179+
labels.append(leg_label)
1180+
ax.grid(self.grid)
1181+
1182+
if self._is_datetype():
1183+
left, right = _get_xlim(lines)
1184+
ax.set_xlim(left, right)
1185+
except AttributeError as inst: # non-numeric
1186+
msg = ('Unable to plot data %s vs index %s,\n'
1187+
'error was: %s' % (str(y), str(x), str(inst)))
1188+
print msg
11841189

11851190
self._make_legend(lines, labels)
11861191

@@ -1198,19 +1203,27 @@ def to_leg_label(label, i):
11981203
return label + ' (right)'
11991204
return label
12001205

1206+
def _plot(data, col_num, ax, label, style, **kwds):
1207+
try:
1208+
newlines = tsplot(data, plotf, ax=ax, label=label,
1209+
style=style, **kwds)
1210+
ax.grid(self.grid)
1211+
lines.append(newlines[0])
1212+
leg_label = to_leg_label(label, col_num)
1213+
labels.append(leg_label)
1214+
except AttributeError as inst: #non-numeric
1215+
msg = ('Unable to plot %s,\n'
1216+
'error was: %s' % (str(data), str(inst)))
1217+
print msg
1218+
12011219
if isinstance(data, Series):
12021220
ax = self._get_ax(0) # self.axes[0]
12031221
style = self.style or ''
12041222
label = com.pprint_thing(self.label)
12051223
kwds = kwargs.copy()
12061224
self._maybe_add_color(colors, kwds, style, 0)
12071225

1208-
newlines = tsplot(data, plotf, ax=ax, label=label,
1209-
style=self.style, **kwds)
1210-
ax.grid(self.grid)
1211-
lines.append(newlines[0])
1212-
leg_label = to_leg_label(label, 0)
1213-
labels.append(leg_label)
1226+
_plot(data, 0, ax, label, self.style, **kwds)
12141227
else:
12151228
for i, col in enumerate(data.columns):
12161229
label = com.pprint_thing(col)
@@ -1220,13 +1233,7 @@ def to_leg_label(label, i):
12201233

12211234
self._maybe_add_color(colors, kwds, style, i)
12221235

1223-
newlines = tsplot(data[col], plotf, ax=ax, label=label,
1224-
style=style, **kwds)
1225-
1226-
lines.append(newlines[0])
1227-
leg_label = to_leg_label(label, i)
1228-
labels.append(leg_label)
1229-
ax.grid(self.grid)
1236+
_plot(data[col], i, ax, label, style, **kwds)
12301237

12311238
self._make_legend(lines, labels)
12321239

pandas/tseries/tests/test_plotting.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ def test_frame_inferred(self):
8080
df = DataFrame(np.random.randn(len(idx), 3), index=idx)
8181
df.plot()
8282

83+
@slow
84+
def test_nonnumeric_exclude(self):
85+
import matplotlib.pyplot as plt
86+
plt.close('all')
87+
88+
idx = date_range('1/1/1987', freq='A', periods=3)
89+
df = DataFrame({'A': ["x", "y", "z"], 'B': [1,2,3]}, idx)
90+
ax = df.plot() # it works
91+
self.assert_(len(ax.get_lines()) == 1) #B was plotted
92+
93+
plt.close('all')
94+
ax = df['A'].plot() # it works
95+
self.assert_(len(ax.get_lines()) == 0)
96+
8397
@slow
8498
def test_tsplot(self):
8599
from pandas.tseries.plotting import tsplot

0 commit comments

Comments
 (0)