Skip to content

FIX: catch warnings from pandas in cbook._check_1d #16347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,11 +1367,22 @@ def _check_1d(x):
return np.atleast_1d(x)
else:
try:
ndim = x[:, None].ndim
# work around https://github.com/pandas-dev/pandas/issues/27775
# which mean the shape is not as expected. That this ever worked
# was an unintentional quirk of pandas the above line will raise
# an exception in the future.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the above line is getting deleted, this comment is no longer relevant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still relevant, the line above was moved down and put inside the context manager below.

Copy link
Member

@story645 story645 Jan 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then it should be changed to "the line below" or moved to be next to the line it's referring to:

 ndim = x[:, None].ndim  #will raise exception in future

# This warns in pandas >= 1.0 via
# https://github.com/pandas-dev/pandas/pull/30588
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings("always",
category=DeprecationWarning,
module='pandas[.*]')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this mean we're just kicking the can down the road and will need yet another fix when pandas actually change this behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change will be to raise one of the Exceptions we already catch below so we will still be happy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough


ndim = x[:, None].ndim
# we have definitely hit a pandas index or series object
# cast to a numpy array.
if len(w) != 0:
return np.asanyarray(x)
if ndim < 2:
return np.atleast_1d(x)
return x
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,14 @@ def test_bar_pandas_indexed(pd):
ax.bar(df.x, 1., width=df.width)


def test_pandas_smoke(pd):
# This should not raise any warnings
x = pd.Series([], dtype="float64")
plt.plot(x, x)
plt.plot(x.index, x)
plt.plot(x)


@image_comparison(['hist_log'], remove_text=True)
def test_hist_log():
data0 = np.linspace(0, 1, 200)**3
Expand Down