Skip to content

Commit 0a83a1f

Browse files
committed
FIX/ENH: attempt soft conversion of object series before raising a TypeError when plotting
1 parent e4b8ed4 commit 0a83a1f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

pandas/tests/test_graphics.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,21 @@ def test_bootstrap_plot(self):
189189
from pandas.tools.plotting import bootstrap_plot
190190
_check_plot_works(bootstrap_plot, self.ts, size=10)
191191

192-
@slow
193-
def test_all_invalid_plot_data(self):
192+
def test_invalid_plot_data(self):
194193
s = Series(list('abcd'))
195194
kinds = 'line', 'bar', 'barh', 'kde', 'density'
196195

197196
for kind in kinds:
198197
self.assertRaises(TypeError, s.plot, kind=kind)
199198

200199
@slow
200+
def test_valid_object_plot(self):
201+
s = Series(range(10), dtype=object)
202+
kinds = 'line', 'bar', 'barh', 'kde', 'density'
203+
204+
for kind in kinds:
205+
_check_plot_works(s.plot, kind=kind)
206+
201207
def test_partially_invalid_plot_data(self):
202208
s = Series(['a', 'b', 1.0, 2])
203209
kinds = 'line', 'bar', 'barh', 'kde', 'density'

pandas/tools/plotting.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -881,12 +881,12 @@ def _compute_plot_data(self):
881881
# might be a frame
882882
numeric_data = self.data._get_numeric_data()
883883
except AttributeError:
884-
# a series, but no object dtypes allowed!
885-
if self.data.dtype == np.object_:
886-
raise TypeError('invalid dtype for plotting, please cast to a '
887-
'numeric dtype explicitly if you want to plot')
884+
# attempt soft conversion
885+
numeric_data = self.data.convert_objects()
888886

889-
numeric_data = self.data
887+
# a series, but no object dtypes allowed!
888+
if numeric_data.dtype == np.object_:
889+
raise TypeError('invalid dtype for plotting')
890890

891891
try:
892892
is_empty = numeric_data.empty

0 commit comments

Comments
 (0)