Skip to content

Better error message for DataFrame.hist() without numerical columns (… #26483

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 4 commits into from
May 24, 2019
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
4 changes: 4 additions & 0 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,10 @@ def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
data = data._get_numeric_data()
naxes = len(data.columns)

if naxes == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if naxes == 0:
if not naxes:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer how it is. This is more explicit.

raise ValueError("hist method requires numerical columns, "
"nothing to plot.")

fig, axes = _subplots(naxes=naxes, ax=ax, squeeze=False,
sharex=sharex, sharey=sharey, figsize=figsize,
layout=layout)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/plotting/test_hist_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ def test_hist_df_legacy(self):
with pytest.raises(AttributeError):
ser.hist(foo='bar')

@pytest.mark.slow
def test_hist_non_numerical_raises(self):
# gh-10444
df = DataFrame(np.random.rand(10, 2))
df_o = df.astype(np.object)

msg = "hist method requires numerical columns, nothing to plot."
with pytest.raises(ValueError, match=msg):
df_o.hist()

@pytest.mark.slow
def test_hist_layout(self):
df = DataFrame(randn(100, 3))
Expand Down