8000 FIX: more holistic fix · matplotlib/matplotlib@509626d · GitHub
[go: up one dir, main page]

Skip to content

Commit 509626d

Browse files
committed
FIX: more holistic fix
1 parent 5052af5 commit 509626d

File tree

2 files changed

+10
-39
lines changed

2 files changed

+10
-39
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,48 +1300,16 @@ 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-
# ValueError: https://github.com/matplotlib/matplotlib/issues/22125
1343-
except (AssertionError, IndexError, TypeError, ValueError):
1309+
ndim = x[:, None].ndim
1310+
if ndim < 2:
13441311
return np.atleast_1d(x)
1312+
return x
13451313

13461314

13471315
def _reshape_2D(X, name):

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1750,11 +1750,14 @@ def test_bar_hatches(fig_test, fig_ref):
17501750
def test_pandas_minimal_plot(pd):
17511751
# smoke test that series and index objcets do not warn
17521752
for x in [pd.Series([1, 2], dtype="float64"),
1753-
pd.Series([1, 2], dtype="Float32")]:
1753+
pd.Series([1, 2], dtype="Float64")]:
17541754
plt.plot(x, x)
17551755
plt.plot(x.index, x)
17561756
plt.plot(x)
17571757
plt.plot(x.index)
1758+
df = pd.DataFrame({'col': [1, 2, 3]})
1759+
plt.plot(df)
1760+
plt.plot(df, df)
17581761

17591762

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

0 commit comments

Comments
 (0)
0