Skip to content

Commit 0040b0a

Browse files
authored
Merge pull request #22592 from meeseeksmachine/auto-backport-of-pr-22141-on-v3.5.x
Backport PR #22141 on branch v3.5.x (Fix check 1d)
2 parents 17b4e10 + 4715aff commit 0040b0a

File tree

2 files changed

+14
-44
lines changed

2 files changed

+14
-44
lines changed

lib/matplotlib/cbook/__init__.py

+5-39
Original file line numberDiff line numberDiff line change
@@ -1300,47 +1300,13 @@ def _to_unmasked_float_array(x):
13001300

13011301
def _check_1d(x):
13021302
"""Convert scalars to 1D arrays; pass-through arrays as is."""
1303+
if hasattr(x, 'to_numpy'):
1304+
# if we are given an object that creates a numpy, we should use it...
1305+
x = x.to_numpy()
13031306
if not hasattr(x, 'shape') or len(x.shape) < 1:
13041307
return np.atleast_1d(x)
13051308
else:
1306-
try:
1307-
# work around
1308-
# https://github.com/pandas-dev/pandas/issues/27775 which
1309-
# means the shape of multi-dimensional slicing is not as
1310-
# expected. That this ever worked was an unintentional
1311-
# quirk of pandas and will raise an exception in the
1312-
# future. This slicing warns in pandas >= 1.0rc0 via
1313-
# https://github.com/pandas-dev/pandas/pull/30588
1314-
#
1315-
# < 1.0rc0 : x[:, None].ndim == 1, no warning, custom type
1316-
# >= 1.0rc1 : x[:, None].ndim == 2, warns, numpy array
1317-
# future : x[:, None] -> raises
1318-
#
1319-
# This code should correctly identify and coerce to a
1320-
# numpy array all pandas versions.
1321-
with warnings.catch_warnings(record=True) as w:
1322-
warnings.filterwarnings(
1323-
"always",
1324-
category=Warning,
1325-
message='Support for multi-dimensional indexing')
1326-
1327-
ndim = x[:, None].ndim
1328-
# we have definitely hit a pandas index or series object
1329-
# cast to a numpy array.
1330-
if len(w) > 0:
1331-
return np.asanyarray(x)
1332-
# We have likely hit a pandas object, or at least
1333-
# something where 2D slicing does not result in a 2D
1334-
# object.
1335-
if ndim < 2:
1336-
return np.atleast_1d(x)
1337-
return x
1338-
# In pandas 1.1.0, multidimensional indexing leads to an
1339-
# AssertionError for some Series objects, but should be
1340-
# IndexError as described in
1341-
# https://github.com/pandas-dev/pandas/issues/35527
1342-
except (AssertionError, IndexError, TypeError):
1343-
return np.atleast_1d(x)
1309+
return x
13441310

13451311

13461312
def _reshape_2D(X, name):
@@ -1649,7 +1615,7 @@ def index_of(y):
16491615
The x and y values to plot.
16501616
"""
16511617
try:
1652-
return y.index.values, y.values
1618+
return y.index.to_numpy(), y.to_numpy()
16531619
except AttributeError:
16541620
pass
16551621
try:

lib/matplotlib/tests/test_axes.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1762,11 +1762,15 @@ def test_bar_hatches(fig_test, fig_ref):
17621762

17631763
def test_pandas_minimal_plot(pd):
17641764
# smoke test that series and index objcets do not warn
1765-
x = pd.Series([1, 2], dtype="float64")
1766-
plt.plot(x, x)
1767-
plt.plot(x.index, x)
1768-
plt.plot(x)
1769-
plt.plot(x.index)
1765+
for x in [pd.Series([1, 2], dtype="float64"),
1766+
pd.Series([1, 2], dtype="Float64")]:
1767+
plt.plot(x, x)
1768+
plt.plot(x.index, x)
1769+
plt.plot(x)
1770+
plt.plot(x.index)
1771+
df = pd.DataFrame({'col': [1, 2, 3]})
1772+
plt.plot(df)
1773+
plt.plot(df, df)
17701774

17711775

17721776
@image_comparison(['hist_log'], remove_text=True)

0 commit comments

Comments
 (0)