diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 124ec8f4ab92c..9468641211720 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -121,7 +121,7 @@ Performance Improvements Bug Fixes ~~~~~~~~~ - Bug in :func:`to_datetime` which would raise an (incorrect) ``ValueError`` when called with a date far into the future and the ``format`` argument specified instead of raising ``OutOfBoundsDatetime`` (:issue:`23830`) -- +- Bug in an error message in :meth:`DataFrame.plot`. Improved the error message if non-numerics are passed to :meth:`DataFrame.plot` (:issue:`25481`) - Categorical diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 48d870bfc2e03..f2802372222cc 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -358,10 +358,9 @@ def _compute_plot_data(self): except AttributeError: is_empty = not len(numeric_data) - # no empty frames or series allowed + # no non-numeric frames or series allowed if is_empty: - raise TypeError('Empty {0!r}: no numeric data to ' - 'plot'.format(numeric_data.__class__.__name__)) + raise TypeError('no numeric data to plot') self.data = numeric_data diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 6702ad6cfb761..b9a29cc4ac27e 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -97,7 +97,7 @@ def test_nonnumeric_exclude(self): assert len(ax.get_lines()) == 1 # B was plotted self.plt.close(fig) - msg = "Empty 'DataFrame': no numeric data to plot" + msg = "no numeric data to plot" with pytest.raises(TypeError, match=msg): df['A'].plot() diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 98b241f5c8206..28806bb67c896 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -485,7 +485,9 @@ def test_subplots_timeseries_y_axis(self): ax_datetime_all_tz = testdata.plot(y="datetime_all_tz") assert (ax_datetime_all_tz.get_lines()[0].get_data()[1] == testdata["datetime_all_tz"].values).all() - with pytest.raises(TypeError): + + msg = "no numeric data to plot" + with pytest.raises(TypeError, match=msg): testdata.plot(y="text") @pytest.mark.xfail(reason='not support for period, categorical, ' @@ -2219,7 +2221,9 @@ def test_all_invalid_plot_data(self): for kind in plotting._core._common_kinds: if not _ok_for_gaussian_kde(kind): continue - with pytest.raises(TypeError): + + msg = "no numeric data to plot" + with pytest.raises(TypeError, match=msg): df.plot(kind=kind) @pytest.mark.slow @@ -2230,7 +2234,9 @@ def test_partially_invalid_plot_data(self): for kind in plotting._core._common_kinds: if not _ok_for_gaussian_kde(kind): continue - with pytest.raises(TypeError): + + msg = "no numeric data to plot" + with pytest.raises(TypeError, match=msg): df.plot(kind=kind) with tm.RNGContext(42): diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index 07a4b168a66f1..f5c44ed35819c 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -694,7 +694,9 @@ def test_invalid_plot_data(self): for kind in plotting._core._common_kinds: if not _ok_for_gaussian_kde(kind): continue - with pytest.raises(TypeError): + + msg = "no numeric data to plot" + with pytest.raises(TypeError, match=msg): s.plot(kind=kind, ax=ax) @pytest.mark.slow @@ -711,7 +713,9 @@ def test_partially_invalid_plot_data(self): for kind in plotting._core._common_kinds: if not _ok_for_gaussian_kde(kind): continue - with pytest.raises(TypeError): + + msg = "no numeric data to plot" + with pytest.raises(TypeError, match=msg): s.plot(kind=kind, ax=ax) def test_invalid_kind(self):