From 4715affe0245b2c5e121f935577f64fcc38293a3 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 3 Mar 2022 15:51:30 -0500 Subject: [PATCH] Backport PR #22141: Fix check 1d --- lib/matplotlib/cbook/__init__.py | 44 ++++--------------------------- lib/matplotlib/tests/test_axes.py | 14 ++++++---- 2 files changed, 14 insertions(+), 44 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 9f2a75ce3df0..2722b6c8a3be 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1300,47 +1300,13 @@ 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 - except (AssertionError, IndexError, TypeError): - return np.atleast_1d(x) + return x def _reshape_2D(X, name): @@ -1649,7 +1615,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 cc93bfae4976..204e3cbd984f 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1762,11 +1762,15 @@ 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="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) 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