Skip to content

BUG: plot(kind=hist) results in TypeError if it contains non-numeric data #9853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.16.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down