diff --git a/doc/source/whatsnew/v0.16.1.txt b/doc/source/whatsnew/v0.16.1.txt index ab57f1fb6ea10..a3fd8ae1b86fb 100644 --- a/doc/source/whatsnew/v0.16.1.txt +++ b/doc/source/whatsnew/v0.16.1.txt @@ -107,6 +107,7 @@ Bug Fixes - Bug in plotting continuously using ``secondary_y`` may not show legend properly. (:issue:`9610`, :issue:`9779`) +- Bug in ``DataFrame.plot(kind="hist")`` results in ``TypeError`` when ``DataFrame`` contains non-numeric columns (:issue:`9853`) - Bug in ``Series.quantile`` on empty Series of type ``Datetime`` or ``Timedelta`` (:issue:`9675`) - Bug in ``where`` causing incorrect results when upcasting was required (:issue:`9731`) diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py index 36c19cd39f76c..7d489ce66c288 100644 --- a/pandas/tests/test_graphics.py +++ b/pandas/tests/test_graphics.py @@ -678,6 +678,18 @@ def test_hist_df_kwargs(self): ax = df.plot(kind='hist', bins=5) self.assertEqual(len(ax.patches), 10) + @slow + def test_hist_df_with_nonnumerics(self): + # GH 9853 + with tm.RNGContext(1): + df = DataFrame(np.random.randn(10, 4), columns=['A', 'B', 'C', 'D']) + df['E'] = ['x', 'y'] * 5 + ax = df.plot(kind='hist', bins=5) + self.assertEqual(len(ax.patches), 20) + + ax = df.plot(kind='hist') # bins=10 + self.assertEqual(len(ax.patches), 40) + @slow def test_hist_legacy(self): _check_plot_works(self.ts.hist) diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 358c5b0dd5940..1accc48b0d3c4 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -1948,7 +1948,8 @@ def __init__(self, data, bins=10, bottom=0, **kwargs): def _args_adjust(self): if com.is_integer(self.bins): # create common bin edge - values = np.ravel(self.data.values) + values = self.data.convert_objects()._get_numeric_data() + values = np.ravel(values) values = values[~com.isnull(values)] hist, self.bins = np.histogram(values, bins=self.bins,