From c7eddd61c28dc198b35243a96e1b35f7030ac072 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 27 Jan 2020 14:29:18 -0500 Subject: [PATCH 1/3] FIX: catch warnings from pandas in cbook._check_1d If we catch the warning or the exception, we need to cast to numpy because later on in `_plot_args` we again use multi-dimensional indexing to up-cast to a 2D array. closes #16295 --- lib/matplotlib/cbook/__init__.py | 13 ++++++++++++- lib/matplotlib/tests/test_axes.py | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index fbb9ee5f1ec0..5617b5f071ba 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1367,11 +1367,22 @@ def _check_1d(x): return np.atleast_1d(x) else: try: - ndim = x[:, None].ndim # work around https://github.com/pandas-dev/pandas/issues/27775 # which mean the shape is not as expected. That this ever worked # was an unintentional quirk of pandas the above line will raise # an exception in the future. + # This warns in pandas >= 1.0 via + # https://github.com/pandas-dev/pandas/pull/30588 + with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings("always", + category=DeprecationWarning, + module='pandas[.*]') + + ndim = x[:, None].ndim + # we have definitely hit a pandas index or series object + # cast to a numpy array. + if len(w) != 0: + return np.asanyarray(x) if ndim < 2: return np.atleast_1d(x) return x diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 3c7d3c1adcdd..293b81141e26 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1695,6 +1695,14 @@ def test_bar_pandas_indexed(pd): ax.bar(df.x, 1., width=df.width) +def test_pandas_smoke(pd): + # This should not raise any warnings + x = pd.Series([], dtype="float64") + plt.plot(x, x) + plt.plot(x.index, x) + plt.plot(x) + + @image_comparison(['hist_log'], remove_text=True) def test_hist_log(): data0 = np.linspace(0, 1, 200)**3 From 676f3eb2b02d519eeda9aa6270155602dcb79ee4 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 28 Jan 2020 15:39:19 -0500 Subject: [PATCH 2/3] DOC: clarified comments in code about what this code is doing --- lib/matplotlib/cbook/__init__.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 5617b5f071ba..7ea6fec67920 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1367,12 +1367,20 @@ def _check_1d(x): return np.atleast_1d(x) else: try: - # work around https://github.com/pandas-dev/pandas/issues/27775 - # which mean the shape is not as expected. That this ever worked - # was an unintentional quirk of pandas the above line will raise - # an exception in the future. - # This warns in pandas >= 1.0 via + # work around + # https://github.com/pandas-dev/pandas/issues/27775 which + # means the shape of multi-dimensional slicing is not as + # expected. That this ever worked was an unintentional + # quirk of pandas and will raise an exception in the + # future. This slicing warns in pandas >= 1.0rc0 via # https://github.com/pandas-dev/pandas/pull/30588 + # + # < 1.0rc0 : x[:, None].ndim == 1, no warning, custom type + # >= 1.0rc1 : x[:, None].ndim == 2, warns, numpy array + # future : x[:, None] -> raises + # + # This code should correctly identify and coerce to a + # numpy array all pandas versions. with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always", category=DeprecationWarning, @@ -1381,8 +1389,11 @@ def _check_1d(x): ndim = x[:, None].ndim # we have definitely hit a pandas index or series object # cast to a numpy array. - if len(w) != 0: + if len(w) > 0: return np.asanyarray(x) + # We have likely hit a pandas object, or at least + # something where 2D slicing does not result in a 2D + # object. if ndim < 2: return np.atleast_1d(x) return x From 77e7b314fb4a9bc4bc2ab8ee8ee2a057a558ed1e Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 28 Jan 2020 15:41:06 -0500 Subject: [PATCH 3/3] TST: renamed test to be less idiomatic --- lib/matplotlib/tests/test_axes.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 293b81141e26..cdc81bfa4bbc 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1695,12 +1695,13 @@ def test_bar_pandas_indexed(pd): ax.bar(df.x, 1., width=df.width) -def test_pandas_smoke(pd): - # This should not raise any warnings - x = pd.Series([], dtype="float64") +def test_pandas_minimal_plot(pd): + # smoke test that series and index objcets do not warn + x = pd.Series([1, 2], dtype="float64") plt.plot(x, x) plt.plot(x.index, x) plt.plot(x) + plt.plot(x.index) @image_comparison(['hist_log'], remove_text=True) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy