-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Add match=msg to pytest.raises or convert to external_error_raised for modules in tests/plotting #38684
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
jreback
merged 7 commits into
pandas-dev:master
from
moink:add_match_msg_to_pytest_raises_plotting
Dec 24, 2020
Merged
Add match=msg to pytest.raises or convert to external_error_raised for modules in tests/plotting #38684
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
caa4672
TST: GH30999 Add match=msg to pandas/tests/plotting/test_backend.py
moink 4bdf9a4
TST: GH30999 Add match=msg to all pytest.raises in pandas/tests/plott…
moink bbe6587
TST: GH30999 Add match=msg to all pytest.raises in pandas/tests/plott…
moink 55e5256
TST: GH30999 Add match=msg to all pytest.raises in pandas/tests/plott…
moink fb9b4ae
TST: GH30999 Add match=msg to all pytest.raises in pandas/tests/plott…
moink beb2feb
TST: GH30999 Add match=msg to all pytest.raises in pandas/tests/plott…
moink c8f0d56
TST: GH30999 Fix typo in test_errorbar_asymmetrical that was causing …
moink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1232,8 +1232,8 @@ def _get_stacked_values(cls, ax: "Axes", stacking_id, values, label): | |
|
||
raise ValueError( | ||
"When stacked is True, each column must be either " | ||
"all positive or negative." | ||
f"{label} contains both positive and negative values" | ||
"all positive or all negative. " | ||
f"Column '{label}' contains both positive and negative values" | ||
) | ||
|
||
@classmethod | ||
|
@@ -1555,7 +1555,7 @@ class PiePlot(MPLPlot): | |
def __init__(self, data, kind=None, **kwargs): | ||
data = data.fillna(value=0) | ||
if (data < 0).any().any(): | ||
raise ValueError(f"{kind} doesn't allow negative values") | ||
raise ValueError(f"{self._kind} plot doesn't allow negative values") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the test, |
||
MPLPlot.__init__(self, data, kind=kind, **kwargs) | ||
|
||
def _args_adjust(self): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
""" Test cases for DataFrame.plot """ | ||
|
||
from datetime import date, datetime | ||
import itertools | ||
import re | ||
import string | ||
import warnings | ||
|
||
|
@@ -358,10 +358,10 @@ def test_negative_log(self): | |
index=list(string.ascii_letters[:6]), | ||
columns=["x", "y", "z", "four"], | ||
) | ||
|
||
with pytest.raises(ValueError): | ||
msg = "Log-y scales are not supported in area plot" | ||
with pytest.raises(ValueError, match=msg): | ||
df.plot.area(logy=True) | ||
with pytest.raises(ValueError): | ||
with pytest.raises(ValueError, match=msg): | ||
df.plot.area(loglog=True) | ||
|
||
def _compare_stacked_y_cood(self, normal_lines, stacked_lines): | ||
|
@@ -406,7 +406,12 @@ def test_line_area_stacked(self): | |
self._compare_stacked_y_cood(ax1.lines[2:], ax2.lines[2:]) | ||
|
||
_check_plot_works(mixed_df.plot, stacked=False) | ||
with pytest.raises(ValueError): | ||
msg = ( | ||
"When stacked is True, each column must be either all positive or " | ||
"all negative. Column 'w' contains both positive and negative " | ||
"values" | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
mixed_df.plot(stacked=True) | ||
|
||
# Use an index with strictly positive values, preventing | ||
|
@@ -650,9 +655,11 @@ def test_plot_scatter(self): | |
_check_plot_works(df.plot.scatter, x="x", y="y") | ||
_check_plot_works(df.plot.scatter, x=1, y=2) | ||
|
||
with pytest.raises(TypeError): | ||
msg = re.escape("scatter() missing 1 required positional argument: 'y'") | ||
with pytest.raises(TypeError, match=msg): | ||
df.plot.scatter(x="x") | ||
with pytest.raises(TypeError): | ||
msg = re.escape("scatter() missing 1 required positional argument: 'x'") | ||
with pytest.raises(TypeError, match=msg): | ||
df.plot.scatter(y="y") | ||
|
||
# GH 6951 | ||
|
@@ -850,8 +857,9 @@ def test_boxplot_return_type(self): | |
index=list(string.ascii_letters[:6]), | ||
columns=["one", "two", "three", "four"], | ||
) | ||
with pytest.raises(ValueError): | ||
df.plot.box(return_type="NOTATYPE") | ||
msg = "return_type must be {None, 'axes', 'dict', 'both'}" | ||
with pytest.raises(ValueError, match=msg): | ||
df.plot.box(return_type="not_a_type") | ||
|
||
result = df.plot.box(return_type="dict") | ||
self._check_box_return_type(result, "dict") | ||
|
@@ -1309,44 +1317,47 @@ def test_partially_invalid_plot_data(self): | |
df = DataFrame(np.random.randn(10, 2), dtype=object) | ||
df[np.random.rand(df.shape[0]) > 0.5] = "a" | ||
for kind in plotting.PlotAccessor._common_kinds: | ||
|
||
msg = "no numeric data to plot" | ||
with pytest.raises(TypeError, match=msg): | ||
df.plot(kind=kind) | ||
|
||
with tm.RNGContext(42): | ||
# area plot doesn't support positive/negative mixed data | ||
kinds = ["area"] | ||
df = DataFrame(np.random.rand(10, 2), dtype=object) | ||
df[np.random.rand(df.shape[0]) > 0.5] = "a" | ||
for kind in kinds: | ||
with pytest.raises(TypeError): | ||
df.plot(kind=kind) | ||
with pytest.raises(TypeError, match="no numeric data to plot"): | ||
df.plot(kind="area") | ||
|
||
def test_invalid_kind(self): | ||
df = DataFrame(np.random.randn(10, 2)) | ||
with pytest.raises(ValueError): | ||
df.plot(kind="aasdf") | ||
msg = "invalid_plot_kind is not a valid plot kind" | ||
with pytest.raises(ValueError, match=msg): | ||
df.plot(kind="invalid_plot_kind") | ||
|
||
@pytest.mark.parametrize( | ||
"x,y,lbl", | ||
[ | ||
(["B", "C"], "A", "a"), | ||
(["A"], ["B", "C"], ["b", "c"]), | ||
("A", ["B", "C"], "badlabel"), | ||
], | ||
) | ||
def test_invalid_xy_args(self, x, y, lbl): | ||
# GH 18671, 19699 allows y to be list-like but not x | ||
df = DataFrame({"A": [1, 2], "B": [3, 4], "C": [5, 6]}) | ||
with pytest.raises(ValueError): | ||
with pytest.raises(ValueError, match="x must be a label or position"): | ||
df.plot(x=x, y=y, label=lbl) | ||
|
||
def test_bad_label(self): | ||
df = DataFrame({"A": [1, 2], "B": [3, 4], "C": [5, 6]}) | ||
msg = "label should be list-like and same length as y" | ||
with pytest.raises(ValueError, match=msg): | ||
df.plot(x="A", y=["B", "C"], label="bad_label") | ||
|
||
@pytest.mark.parametrize("x,y", [("A", "B"), (["A"], "B")]) | ||
def test_invalid_xy_args_dup_cols(self, x, y): | ||
# GH 18671, 19699 allows y to be list-like but not x | ||
df = DataFrame([[1, 3, 5], [2, 4, 6]], columns=list("AAB")) | ||
with pytest.raises(ValueError): | ||
with pytest.raises(ValueError, match="x must be a label or position"): | ||
df.plot(x=x, y=y) | ||
|
||
@pytest.mark.parametrize( | ||
|
@@ -1416,7 +1427,8 @@ def test_pie_df(self): | |
columns=["X", "Y", "Z"], | ||
index=["a", "b", "c", "d", "e"], | ||
) | ||
with pytest.raises(ValueError): | ||
msg = "pie requires either y column or 'subplots=True'" | ||
with pytest.raises(ValueError, match=msg): | ||
df.plot.pie() | ||
|
||
ax = _check_plot_works(df.plot.pie, y="Y") | ||
|
@@ -1520,11 +1532,11 @@ def test_errorbar_plot(self): | |
ax = _check_plot_works(s_df.plot, y="y", x="x", yerr=yerr) | ||
self._check_has_errorbars(ax, xerr=0, yerr=1) | ||
|
||
with pytest.raises(ValueError): | ||
with tm.external_error_raised(ValueError): | ||
df.plot(yerr=np.random.randn(11)) | ||
|
||
df_err = DataFrame({"x": ["zzz"] * 12, "y": ["zzz"] * 12}) | ||
with pytest.raises((ValueError, TypeError)): | ||
with tm.external_error_raised(TypeError): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure why this previously allowed it to be a |
||
df.plot(yerr=df_err) | ||
|
||
@pytest.mark.parametrize("kind", ["line", "bar", "barh"]) | ||
|
@@ -1647,7 +1659,10 @@ def test_errorbar_asymmetrical(self): | |
expected_0_0 = err[0, :, 0] * np.array([-1, 1]) | ||
tm.assert_almost_equal(yerr_0_0, expected_0_0) | ||
|
||
with pytest.raises(ValueError): | ||
msg = re.escape( | ||
"Asymmetrical error bars should be provided with the shape (3, 2, 5)" | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
df.plot(yerr=err.T) | ||
|
||
tm.close() | ||
|
@@ -1837,9 +1852,10 @@ def test_memory_leak(self): | |
tm.close() | ||
# force a garbage collection | ||
gc.collect() | ||
msg = "weakly-referenced object no longer exists" | ||
for key in results: | ||
# check that every plot was collected | ||
with pytest.raises(ReferenceError): | ||
with pytest.raises(ReferenceError, match=msg): | ||
# need to actually access something to get an error | ||
results[key].lines | ||
|
||
|
@@ -2095,7 +2111,7 @@ def test_plot_no_rows(self): | |
|
||
def test_plot_no_numeric_data(self): | ||
df = DataFrame(["a", "b", "c"]) | ||
with pytest.raises(TypeError): | ||
with pytest.raises(TypeError, match="no numeric data to plot"): | ||
df.plot() | ||
|
||
def test_missing_markers_legend(self): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes the error message a bit clearer.