8000 Merge pull request #16360 from meeseeksmachine/auto-backport-of-pr-16… · matplotlib/matplotlib@89ff308 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89ff308

Browse files
authored
Merge pull request #16360 from meeseeksmachine/auto-backport-of-pr-16347-on-v3.2.x
Backport PR #16347 on branch v3.2.x (FIX: catch warnings from pandas in cbook._check_1d)
2 parents 0c43f2a + ff8b1a9 commit 89ff308

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
@@ -1326,11 +1326,33 @@ def _check_1d(x):
13261326
return np.atleast_1d(x)
13271327
else:
13281328
try:
1329-
ndim = x[:, None].ndim
1330-
# work around https://github.com/pandas-dev/pandas/issues/27775
1331-
# which mean the shape is not as expected. That this ever worked
1332-
# was an unintentional quirk of pandas the above line will raise
1333-
# an exception in the future.
1329+
# work around
1330+
# https://github.com/pandas-dev/pandas/issues/27775 which
1331+
# means the shape of multi-dimensional slicing is not as
1332+
# expected. That this ever worked was an unintentional
1333+
# quirk of pandas and will raise an exception in the
1334+
# future. This slicing warns in pandas >= 1.0rc0 via
1335+
# https://github.com/pandas-dev/pandas/pull/30588
1336+
#
1337+
# < 1.0rc0 : x[:, None].ndim == 1, no warning, custom type
1338+
# >= 1.0rc1 : x[:, None].ndim == 2, warns, numpy array
1339+
# future : x[:, None] -> raises
1340+
#
1341+
# This code should correctly identify and coerce to a
1342+
# numpy array all pandas versions.
1343+
with warnings.catch_warnings(record=True) as w:
1344+
warnings.filterwarnings("always",
1345+
category=DeprecationWarning,
1346+
module='pandas[.*]')
1347+
1348+
ndim = x[:, None].ndim
1349+
# we have definitely hit a pandas index or series object
1350+
# cast to a numpy array.
1351+
if len(w) > 0:
1352+
return np.asanyarray(x)
1353+
# We have likely hit a pandas object, or at least
1354+
# something where 2D slicing does not result in a 2D
1355+
# object.
13341356
if ndim < 2:
13351357
return np.atleast_1d(x)
13361358
return x

lib/matplotlib/tests/test_axes.py

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

16711671

1672+
def test_pandas_minimal_plot(pd):
1673+
# smoke test that series and index objcets do not warn
1674+
x = pd.Series([1, 2], dtype="float64")
1675+
plt.plot(x, x)
1676+
plt.plot(x.index, x)
1677+
plt.plot(x)
1678+
plt.plot(x.index)
1679+
1680+
16721681
@image_comparison(['hist_log'], remove_text=True)
16731682
def test_hist_log():
16741683
data0 = np.linspace(0, 1, 200)**3

0 commit comments

Comments
 (0)
0