Skip to content

Commit 3858db5

Browse files
committed
BUG: plot(kind=hist) results in TypeError for non-numeric data
1 parent 2734fff commit 3858db5

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v0.16.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Bug Fixes
107107

108108
- Bug in plotting continuously using ``secondary_y`` may not show legend properly. (:issue:`9610`, :issue:`9779`)
109109

110+
- Bug in ``DataFrame.plot(kind="hist")`` results in ``TypeError`` when ``DataFrame`` contains non-numeric columns (:issue:`9853`)
110111

111112
- Bug in ``Series.quantile`` on empty Series of type ``Datetime`` or ``Timedelta`` (:issue:`9675`)
112113
- Bug in ``where`` causing incorrect results when upcasting was required (:issue:`9731`)

pandas/tests/test_graphics.py

+12
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,18 @@ def test_hist_df_kwargs(self):
678678
ax = df.plot(kind='hist', bins=5)
679679
self.assertEqual(len(ax.patches), 10)
680680

681+
@slow
682+
def test_hist_df_with_nonnumerics(self):
683+
# GH 9853
684+
with tm.RNGContext(1):
685+
df = DataFrame(np.random.randn(10, 4), columns=['A', 'B', 'C', 'D'])
686+
df['E'] = ['x', 'y'] * 5
687+
ax = df.plot(kind='hist', bins=5)
688+
self.assertEqual(len(ax.patches), 20)
689+
690+
ax = df.plot(kind='hist') # bins=10
691+
self.assertEqual(len(ax.patches), 40)
692+
681693
@slow
682694
def test_hist_legacy(self):
683695
_check_plot_works(self.ts.hist)

pandas/tools/plotting.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,8 @@ def __init__(self, data, bins=10, bottom=0, **kwargs):
19481948
def _args_adjust(self):
19491949
if com.is_integer(self.bins):
19501950
# create common bin edge
1951-
values = np.ravel(self.data.values)
1951+
values = self.data.convert_objects()._get_numeric_data()
1952+
values = np.ravel(values)
19521953
values = values[~com.isnull(values)]
19531954

19541955
hist, self.bins = np.histogram(values, bins=self.bins,

0 commit comments

Comments
 (0)