From 5052af52420a9122a615182c70c0bdf9b4d9290d Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 7 Jan 2022 10:22:24 +0100 Subject: [PATCH 1/3] FIX: accomodate pandas type that doesn't return numpy from .values --- lib/matplotlib/cbook/__init__.py | 5 +++-- lib/matplotlib/tests/test_axes.py | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index efaabd1e09d4..6c77e992638e 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1339,7 +1339,8 @@ def _check_1d(x): # AssertionError for some Series objects, but should be # IndexError as described in # https://github.com/pandas-dev/pandas/issues/35527 - except (AssertionError, IndexError, TypeError): + # ValueError: https://github.com/matplotlib/matplotlib/issues/22125 + except (AssertionError, IndexError, TypeError, ValueError): return np.atleast_1d(x) @@ -1649,7 +1650,7 @@ def index_of(y): The x and y values to plot. """ try: - return y.index.values, y.values + return y.index.to_numpy(), y.to_numpy() except AttributeError: pass try: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index cec795fa0e29..68f1edb694fb 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1749,11 +1749,12 @@ def test_bar_hatches(fig_test, fig_ref): 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) + for x in [pd.Series([1, 2], dtype="float64"), + pd.Series([1, 2], dtype="Float32")]: + plt.plot(x, x) + plt.plot(x.index, x) + plt.plot(x) + plt.plot(x.index) @image_comparison(['hist_log'], remove_text=True) From 509626d7d818c08ade1deca38b2c5210fb4af834 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 27 Jan 2022 13:36:33 +0100 Subject: [PATCH 2/3] FIX: more holistic fix --- lib/matplotlib/cbook/__init__.py | 44 +++++-------------------------- lib/matplotlib/tests/test_axes.py | 5 +++- 2 files changed, 10 insertions(+), 39 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 6c77e992638e..02e8e1d467cf 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1300,48 +1300,16 @@ def _to_unmasked_float_array(x): def _check_1d(x): """Convert scalars to 1D arrays; pass-through arrays as is.""" + if hasattr(x, 'to_numpy'): + # if we are given an object that creates a numpy, we should use it... + x = x.to_numpy() if not hasattr(x, 'shape') or len(x.shape) < 1: return np.atleast_1d(x) else: - try: - # 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=Warning, - message='Support for multi-dimensional indexing') - - 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) - # 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 - # In pandas 1.1.0, multidimensional indexing leads to an - # AssertionError for some Series objects, but should be - # IndexError as described in - # https://github.com/pandas-dev/pandas/issues/35527 - # ValueError: https://github.com/matplotlib/matplotlib/issues/22125 - except (AssertionError, IndexError, TypeError, ValueError): + ndim = x[:, None].ndim + if ndim < 2: return np.atleast_1d(x) + return x def _reshape_2D(X, name): diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 68f1edb694fb..9eb11a2fbb06 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1750,11 +1750,14 @@ def test_bar_hatches(fig_test, fig_ref): def test_pandas_minimal_plot(pd): # smoke test that series and index objcets do not warn for x in [pd.Series([1, 2], dtype="float64"), - pd.Series([1, 2], dtype="Float32")]: + pd.Series([1, 2], dtype="Float64")]: plt.plot(x, x) plt.plot(x.index, x) plt.plot(x) plt.plot(x.index) + df = pd.DataFrame({'col': [1, 2, 3]}) + plt.plot(df) + plt.plot(df, df) @image_comparison(['hist_log'], remove_text=True) From 6dfa93ad7656a4a2eed6c62f4fdc63e47ab13673 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 18 Feb 2022 17:20:25 +0100 Subject: [PATCH 3/3] FIX: simplify a bit more --- lib/matplotlib/cbook/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 02e8e1d467cf..b440e32f326e 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1306,9 +1306,6 @@ def _check_1d(x): if not hasattr(x, 'shape') or len(x.shape) < 1: return np.atleast_1d(x) else: - ndim = x[:, None].ndim - if ndim < 2: - return np.atleast_1d(x) return x