From bbdd8be0b5d6f43d2e090b3952a6cf19d835c963 Mon Sep 17 00:00:00 2001 From: Bharat123Rox Date: Tue, 5 Mar 2019 12:17:19 +0530 Subject: [PATCH 1/5] BUG: Fix #25481 by fixing the error message in TypeError --- pandas/plotting/_core.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 48d870bfc2e03..b565f0d33a965 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -360,8 +360,7 @@ def _compute_plot_data(self): # no empty 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 From f48d2f692d2f50431a58894ccc8b1b401efbe1af Mon Sep 17 00:00:00 2001 From: Bharat123Rox Date: Tue, 5 Mar 2019 16:55:30 +0530 Subject: [PATCH 2/5] Modified affected tests as suggested, added whatsnew entry --- doc/source/whatsnew/v0.25.0.rst | 2 +- pandas/tests/plotting/test_datetimelike.py | 2 +- pandas/tests/plotting/test_frame.py | 12 +++++++++--- pandas/tests/plotting/test_series.py | 8 ++++++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 124ec8f4ab92c..4a93c073e18c3 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 :func: `plot` for a `pd.Series` or a `pd.DataFrame` which had a confusing error message mentioning an "Empty" DataFrame, now raises ``TypeError: no numeric data to plot`` - Categorical 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): From fa942982d830567b94b6b9a50f6fb275379d0487 Mon Sep 17 00:00:00 2001 From: Bharat123Rox Date: Tue, 5 Mar 2019 16:57:01 +0530 Subject: [PATCH 3/5] Included issue number in whatsnew entry --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 4a93c073e18c3..7a9733af3b245 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 :func: `plot` for a `pd.Series` or a `pd.DataFrame` which had a confusing error message mentioning an "Empty" DataFrame, now raises ``TypeError: no numeric data to plot`` +- Bug in :func: `plot` for a `pd.Series` or a `pd.DataFrame` which had a confusing error message mentioning an "Empty" DataFrame, now raises ``TypeError: no numeric data to plot`` (:issue:`25481`) - Categorical From e1e9e538bcc4863c03b22c0f1b9420997dc680eb Mon Sep 17 00:00:00 2001 From: Bharat123Rox Date: Tue, 5 Mar 2019 20:33:06 +0530 Subject: [PATCH 4/5] Updated whatsnew entry with @TomAugspurger 's suggestion --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 7a9733af3b245..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 :func: `plot` for a `pd.Series` or a `pd.DataFrame` which had a confusing error message mentioning an "Empty" DataFrame, now raises ``TypeError: no numeric data to plot`` (:issue:`25481`) +- 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 From 83aaf2c45f6dab3f1f79c50a9303b3f879fb9f30 Mon Sep 17 00:00:00 2001 From: Bharat123Rox Date: Thu, 7 Mar 2019 10:36:35 +0530 Subject: [PATCH 5/5] Clarified comment asked by @WillAyd --- pandas/plotting/_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index b565f0d33a965..f2802372222cc 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -358,7 +358,7 @@ 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('no numeric data to plot')