From 5f66d83026f2de8096a88c814a11731192528f93 Mon Sep 17 00:00:00 2001 From: Lee Johnston Date: Tue, 4 Aug 2020 09:54:29 -0500 Subject: [PATCH 1/3] Catch Pandas AssertionError on deprecated multidimensional indexing Fixes #18158 due to pandas-dev/pandas#35527 --- lib/matplotlib/cbook/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index a5cd846b4de6..c21fad60b44b 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1323,8 +1323,11 @@ def _check_1d(x): "always", category=Warning, message='Support for multi-dimensional indexing') - - ndim = x[:, None].ndim + try: + ndim = x[:, None].ndim + except AssertionError: + # catch https://github.com/pandas-dev/pandas/issues/35527 + return np.asanyarray(x) # we have definitely hit a pandas index or series object # cast to a numpy array. if len(w) > 0: From 6365ae569251299efb3be1a77858200c78ef5808 Mon Sep 17 00:00:00 2001 From: Lee Johnston Date: Tue, 4 Aug 2020 13:52:33 -0500 Subject: [PATCH 2/3] Simplify try...except and add additional comment --- lib/matplotlib/cbook/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index c21fad60b44b..19f65c80f0d8 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1323,11 +1323,8 @@ def _check_1d(x): "always", category=Warning, message='Support for multi-dimensional indexing') - try: - ndim = x[:, None].ndim - except AssertionError: - # catch https://github.com/pandas-dev/pandas/issues/35527 - return np.asanyarray(x) + + ndim = x[:, None].ndim # we have definitely hit a pandas index or series object # cast to a numpy array. if len(w) > 0: @@ -1338,7 +1335,10 @@ def _check_1d(x): if ndim < 2: return np.atleast_1d(x) return x - except (IndexError, TypeError): + # 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) From 0f36be13d1899ba8eca0d28672598d36e0f474c9 Mon Sep 17 00:00:00 2001 From: Lee Johnston Date: Tue, 4 Aug 2020 14:39:24 -0500 Subject: [PATCH 3/3] Fix flake8 line length complaint --- lib/matplotlib/cbook/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 19f65c80f0d8..b5d561535975 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1335,8 +1335,9 @@ def _check_1d(x): 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 + # 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)