From 576f0a8b52cdcc20ad104a1e3ce15c1c5555401f Mon Sep 17 00:00:00 2001 From: matsmaiwald Date: Tue, 21 May 2019 21:01:12 +0200 Subject: [PATCH 1/4] Better error message for DataFrame.hist() without numerical columns (#10444) --- pandas/plotting/_core.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 90297ecfa3415..fed4b0d90983c 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -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: + 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) From 99da02eb9a374d2a47b9c08ac3ced1cc14bf3fce Mon Sep 17 00:00:00 2001 From: matsmaiwald Date: Tue, 21 May 2019 23:36:41 +0200 Subject: [PATCH 2/4] Better error message for DataFrame.hist() without numerical columns (#10444) --- pandas/tests/plotting/test_hist_method.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index c62ed21c2fb17..d881bfad1f1dc 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -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(self): + # check non-numerical df raises appropriate exception and error message + 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)) From d25bbc2646aa1e2510fc05d08928955322c1fd89 Mon Sep 17 00:00:00 2001 From: Mats Maiwald <32721837+matsmaiwald@users.noreply.github.com> Date: Tue, 21 May 2019 23:46:39 +0200 Subject: [PATCH 3/4] Update pandas/tests/plotting/test_hist_method.py Co-Authored-By: Simon Hawkins --- pandas/tests/plotting/test_hist_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index d881bfad1f1dc..b95a292e344b1 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -210,7 +210,7 @@ def test_hist_df_legacy(self): ser.hist(foo='bar') @pytest.mark.slow - def test_hist_non_numerical(self): + def test_hist_non_numerical_raises(self): # check non-numerical df raises appropriate exception and error message df = DataFrame(np.random.rand(10, 2)) df_o = df.astype(np.object) From 91ab6d84ace23cef570450adb81cf69f4656d389 Mon Sep 17 00:00:00 2001 From: Mats Maiwald <32721837+matsmaiwald@users.noreply.github.com> Date: Tue, 21 May 2019 23:54:18 +0200 Subject: [PATCH 4/4] Update pandas/tests/plotting/test_hist_method.py Co-Authored-By: Simon Hawkins --- pandas/tests/plotting/test_hist_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index b95a292e344b1..f3f6c9c7fc2d4 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -211,7 +211,7 @@ def test_hist_df_legacy(self): @pytest.mark.slow def test_hist_non_numerical_raises(self): - # check non-numerical df raises appropriate exception and error message + # gh-10444 df = DataFrame(np.random.rand(10, 2)) df_o = df.astype(np.object)