Skip to content

Commit 0359832

Browse files
authored
Merge pull request #22141 from jklymak/fix-check-1d
FIX: check 1d
2 parents f33f5ab + 6dfa93a commit 0359832

File tree

2 files changed

+14
-44
lines changed

2 files changed

+14
-44
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,47 +1311,13 @@ def _to_unmasked_float_array(x):
13111311

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

13561322

13571323
def _reshape_2D(X, name):
@@ -1660,7 +1626,7 @@ def index_of(y):
16601626
The x and y values to plot.
16611627
"""
16621628
try:
1663-
return y.index.values, y.values
1629+
return y.index.to_numpy(), y.to_numpy()
16641630
except AttributeError:
16651631
pass
16661632
try:

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,11 +1787,15 @@ def test_bar_hatches(fig_test, fig_ref):
17871787

17881788
def test_pandas_minimal_plot(pd):
17891789
# smoke test that series and index objcets do not warn
1790-
x = pd.Series([1, 2], dtype="float64")
1791-
plt.plot(x, x)
1792-
plt.plot(x.index, x)
1793-
plt.plot(x)
1794-
plt.plot(x.index)
1790+
for x in [pd.Series([1, 2], dtype="float64"),
1791+
pd.Series([1, 2], dtype="Float64")]:
1792+
plt.plot(x, x)
1793+
plt.plot(x.index, x)
1794+
plt.plot(x)
1795+
plt.plot(x.index)
1796+
df = pd.DataFrame({'col': [1, 2, 3]})
1797+
plt.plot(df)
1798+
plt.plot(df, df)
17951799

17961800

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

0 commit comments

Comments
 (0)
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