8000 Merge pull request #16362 from tacaswell/auto-backport-of-pr-16347-on… · matplotlib/matplotlib@bbf4c7c · GitHub
[go: up one dir, main page]

Skip to content

Commit bbf4c7c

Browse files
authored
Merge pull request #16362 from tacaswell/auto-backport-of-pr-16347-on-v3.1.x
Backport PR #16347: FIX: catch warnings from pandas in cbook._check_1d
2 parents 37163f6 + fa8ac63 commit bbf4c7c

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,11 +1399,33 @@ def _check_1d(x):
13991399
return np.atleast_1d(x)
14001400
else:
14011401
try:
1402-
ndim = x[:, None].ndim
1403-
# work around https://github.com/pandas-dev/pandas/issues/27775
1404-
# which mean the shape is not as expected. That this ever worked
1405-
# was an unintentional quirk of pandas the above line will raise
1406-
# an exception in the future.
1402+
# work around
1403+
# https://github.com/pandas-dev/pandas/issues/27775 which
1404+
# means the shape of multi-dimensional slicing is not as
1405+
# expected. That this ever worked was an unintentional
1406+
# quirk of pandas and will raise an exception in the
1407+
# future. This slicing warns in pandas >= 1.0rc0 via
1408+
# https://github.com/pandas-dev/pandas/pull/30588
1409+
#
1410+
# < 1.0rc0 : x[:, None].ndim == 1, no warning, custom type
1411+
# >= 1.0rc1 : x[:, None].ndim == 2, warns, numpy array
1412+
# future : x[:, None] -> raises
1413+
#
1414+
# This code should correctly identify and coerce to a
1415+
# numpy array all pandas versions.
1416+
with warnings.catch_warnings(record=True) as w:
1417+
warnings.filterwarnings("always",
1418+
category=DeprecationWarning,
1419+
module='pandas[.*]')
1420+
1421+
ndim = x[:, None].ndim
1422+
# we have definitely hit a pandas index or series object
1423+
# cast to a numpy array.
1424+
if len(w) > 0:
1425+
return np.asanyarray(x)
1426+
# We have likely hit a pandas object, or at least
1427+
# something where 2D slicing does not result in a 2D
1428+
# object.
14071429
if ndim < 2:
14081430
return np.atleast_1d(x)
14091431
return x

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,15 @@ def test_bar_pandas_indexed(pd):
16001600
ax.bar(df.x, 1., width=df.width)
16011601

16021602

1603+
def test_pandas_minimal_plot(pd):
1604+
# smoke test that series and index objcets do not warn
1605+
x = pd.Series([1, 2], dtype="float64")
1606+
plt.plot(x, x)
1607+
plt.plot(x.index, x)
1608+
plt.plot(x)
1609+
plt.plot(x.index)
1610+
1611+
16031612
@image_comparison(baseline_images=['hist_log'],
16041613
remove_text=True)
16051614
def test_hist_log():

0 commit comments

Comments
 (0)
0