Skip to content

add messages to tests #31852

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 9 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion pandas/tests/extension/decimal/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class Reduce:
def check_reduce(self, s, op_name, skipna):

if op_name in ["median", "skew", "kurt"]:
with pytest.raises(NotImplementedError):
msg = r"decimal does not support the .* operation"
with pytest.raises(NotImplementedError, match=msg):
getattr(s, op_name)(skipna=skipna)

else:
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/extension/json/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ def test_custom_asserts(self):
self.assert_frame_equal(a.to_frame(), a.to_frame())

b = pd.Series(data.take([0, 0, 1]))
with pytest.raises(AssertionError):
msg = r"ExtensionArray are different"
with pytest.raises(AssertionError, match=msg):
self.assert_series_equal(a, b)

with pytest.raises(AssertionError):
with pytest.raises(AssertionError, match=msg):
self.assert_frame_equal(a.to_frame(), b.to_frame())


Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/extension/test_boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
# subtraction for bools raises TypeError (but not yet in 1.13)
if _np_version_under1p14:
pytest.skip("__sub__ does not yet raise in numpy 1.13")
with pytest.raises(TypeError):
msg = r"numpy boolean subtract"
with pytest.raises(TypeError, match=msg):
op(s, other)

return

result = op(s, other)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/extension/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ def _compare_other(self, s, data, op_name, other):
assert (result == expected).all()

else:
with pytest.raises(TypeError):
msg = "Unordered Categoricals can only compare equality or not"
with pytest.raises(TypeError, match=msg):
op(data, other)


Expand Down
47 changes: 26 additions & 21 deletions pandas/tests/frame/indexing/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ def test_assigning_ops(self):
tm.assert_frame_equal(df, exp_single_cats_value)

# - assign a single value not in the current categories set
with pytest.raises(ValueError):
msg1 = (
"Cannot setitem on a Categorical with a new category, "
"set the categories first"
)
msg2 = "Cannot set a Categorical with another, without identical categories"
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.iloc[2, 0] = "c"

Expand All @@ -125,7 +130,7 @@ def test_assigning_ops(self):
tm.assert_frame_equal(df, exp_single_row)

# - assign a complete row (mixed values) not in categories set
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.iloc[2, :] = ["c", 2]

Expand All @@ -134,7 +139,7 @@ def test_assigning_ops(self):
df.iloc[2:4, :] = [["b", 2], ["b", 2]]
tm.assert_frame_equal(df, exp_multi_row)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.iloc[2:4, :] = [["c", 2], ["c", 2]]

Expand All @@ -144,12 +149,12 @@ def test_assigning_ops(self):
df.iloc[2:4, 0] = Categorical(["b", "b"], categories=["a", "b"])
tm.assert_frame_equal(df, exp_parts_cats_col)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg2):
# different categories -> not sure if this should fail or pass
df = orig.copy()
df.iloc[2:4, 0] = Categorical(list("bb"), categories=list("abc"))

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg2):
# different values
df = orig.copy()
df.iloc[2:4, 0] = Categorical(list("cc"), categories=list("abc"))
Expand All @@ -160,7 +165,7 @@ def test_assigning_ops(self):
df.iloc[2:4, 0] = ["b", "b"]
tm.assert_frame_equal(df, exp_parts_cats_col)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df.iloc[2:4, 0] = ["c", "c"]

# loc
Expand All @@ -175,7 +180,7 @@ def test_assigning_ops(self):
tm.assert_frame_equal(df, exp_single_cats_value)

# - assign a single value not in the current categories set
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.loc["j", "cats"] = "c"

Expand All @@ -185,7 +190,7 @@ def test_assigning_ops(self):
tm.assert_frame_equal(df, exp_single_row)

# - assign a complete row (mixed values) not in categories set
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.loc["j", :] = ["c", 2]

Expand All @@ -194,7 +199,7 @@ def test_assigning_ops(self):
df.loc["j":"k", :] = [["b", 2], ["b", 2]]
tm.assert_frame_equal(df, exp_multi_row)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.loc["j":"k", :] = [["c", 2], ["c", 2]]

Expand All @@ -204,14 +209,14 @@ def test_assigning_ops(self):
df.loc["j":"k", "cats"] = Categorical(["b", "b"], categories=["a", "b"])
tm.assert_frame_equal(df, exp_parts_cats_col)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg2):
# different categories -> not sure if this should fail or pass
df = orig.copy()
df.loc["j":"k", "cats"] = Categorical(
["b", "b"], categories=["a", "b", "c"]
)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg2):
# different values
df = orig.copy()
df.loc["j":"k", "cats"] = Categorical(
Expand All @@ -224,7 +229,7 @@ def test_assigning_ops(self):
df.loc["j":"k", "cats"] = ["b", "b"]
tm.assert_frame_equal(df, exp_parts_cats_col)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df.loc["j":"k", "cats"] = ["c", "c"]

# loc
Expand All @@ -239,7 +244,7 @@ def test_assigning_ops(self):
tm.assert_frame_equal(df, exp_single_cats_value)

# - assign a single value not in the current categories set
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.loc["j", df.columns[0]] = "c"

Expand All @@ -249,7 +254,7 @@ def test_assigning_ops(self):
tm.assert_frame_equal(df, exp_single_row)

# - assign a complete row (mixed values) not in categories set
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.loc["j", :] = ["c", 2]

Expand All @@ -258,7 +263,7 @@ def test_assigning_ops(self):
df.loc["j":"k", :] = [["b", 2], ["b", 2]]
tm.assert_frame_equal(df, exp_multi_row)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.loc["j":"k", :] = [["c", 2], ["c", 2]]

Expand All @@ -268,14 +273,14 @@ def test_assigning_ops(self):
df.loc["j":"k", df.columns[0]] = Categorical(["b", "b"], categories=["a", "b"])
tm.assert_frame_equal(df, exp_parts_cats_col)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg2):
# different categories -> not sure if this should fail or pass
df = orig.copy()
df.loc["j":"k", df.columns[0]] = Categorical(
["b", "b"], categories=["a", "b", "c"]
)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg2):
# different values
df = orig.copy()
df.loc["j":"k", df.columns[0]] = Categorical(
Expand All @@ -288,7 +293,7 @@ def test_assigning_ops(self):
df.loc["j":"k", df.columns[0]] = ["b", "b"]
tm.assert_frame_equal(df, exp_parts_cats_col)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df.loc["j":"k", df.columns[0]] = ["c", "c"]

# iat
Expand All @@ -297,7 +302,7 @@ def test_assigning_ops(self):
tm.assert_frame_equal(df, exp_single_cats_value)

# - assign a single value not in the current categories set
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.iat[2, 0] = "c"

Expand All @@ -308,7 +313,7 @@ def test_assigning_ops(self):
tm.assert_frame_equal(df, exp_single_cats_value)

# - assign a single value not in the current categories set
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.at["j", "cats"] = "c"

Expand All @@ -332,7 +337,7 @@ def test_assigning_ops(self):
df.at["j", "cats"] = "b"
tm.assert_frame_equal(df, exp_single_cats_value)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg1):
df = orig.copy()
df.at["j", "cats"] = "c"

Expand Down
18 changes: 11 additions & 7 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,8 @@ def test_setitem(self, float_frame):
# so raise/warn
smaller = float_frame[:2]

with pytest.raises(com.SettingWithCopyError):
msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
with pytest.raises(com.SettingWithCopyError, match=msg):
smaller["col10"] = ["1", "2"]

assert smaller["col10"].dtype == np.object_
Expand Down Expand Up @@ -865,7 +866,8 @@ def test_fancy_getitem_slice_mixed(self, float_frame, float_string_frame):
# setting it triggers setting with copy
sliced = float_frame.iloc[:, -3:]

with pytest.raises(com.SettingWithCopyError):
msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
with pytest.raises(com.SettingWithCopyError, match=msg):
sliced["C"] = 4.0

assert (float_frame["C"] == 4).all()
Expand Down Expand Up @@ -992,7 +994,7 @@ def test_getitem_setitem_fancy_exceptions(self, float_frame):
with pytest.raises(IndexingError, match="Too many indexers"):
ix[:, :, :]

with pytest.raises(IndexingError):
with pytest.raises(IndexingError, match="Too many indexers"):
ix[:, :, :] = 1

def test_getitem_setitem_boolean_misaligned(self, float_frame):
Expand Down Expand Up @@ -1071,10 +1073,10 @@ def test_getitem_setitem_float_labels(self):

cp = df.copy()

with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
cp.iloc[1.0:5] = 0

with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
result = cp.iloc[1.0:5] == 0 # noqa

assert result.values.all()
Expand Down Expand Up @@ -1470,7 +1472,8 @@ def test_iloc_row(self):

# verify slice is view
# setting it makes it raise/warn
with pytest.raises(com.SettingWithCopyError):
msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
with pytest.raises(com.SettingWithCopyError, match=msg):
result[2] = 0.0

exp_col = df[2].copy()
Expand Down Expand Up @@ -1501,7 +1504,8 @@ def test_iloc_col(self):

# verify slice is view
# and that we are setting a copy
with pytest.raises(com.SettingWithCopyError):
msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
with pytest.raises(com.SettingWithCopyError, match=msg):
result[8] = 0.0

assert (df[8] == 0).all()
Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def _check_get(df, cond, check_dtypes=True):
# check getting
df = where_frame
if df is float_string_frame:
with pytest.raises(TypeError):
msg = "'>' not supported between instances of 'str' and 'int'"
with pytest.raises(TypeError, match=msg):
df > 0
return
cond = df > 0
Expand Down Expand Up @@ -114,7 +115,8 @@ def _check_align(df, cond, other, check_dtypes=True):

df = where_frame
if df is float_string_frame:
with pytest.raises(TypeError):
msg = "'>' not supported between instances of 'str' and 'int'"
with pytest.raises(TypeError, match=msg):
df > 0
return

Expand Down Expand Up @@ -172,7 +174,8 @@ def _check_set(df, cond, check_dtypes=True):

df = where_frame
if df is float_string_frame:
with pytest.raises(TypeError):
msg = "'>' not supported between instances of 'str' and 'int'"
with pytest.raises(TypeError, match=msg):
df > 0
return

Expand Down Expand Up @@ -358,7 +361,8 @@ def test_where_datetime(self):
)

stamp = datetime(2013, 1, 3)
with pytest.raises(TypeError):
msg = "'>' not supported between instances of 'float' and 'datetime.datetime'"
with pytest.raises(TypeError, match=msg):
df > stamp

result = df[df.iloc[:, :-1] > stamp]
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/methods/test_explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def test_error():
df = pd.DataFrame(
{"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1}
)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="column must be a scalar"):
df.explode(list("AA"))

df.columns = list("AA")
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="columns must be unique"):
df.explode("A")


Expand Down
15 changes: 10 additions & 5 deletions pandas/tests/frame/methods/test_isin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ def test_isin_with_string_scalar(self):
},
index=["foo", "bar", "baz", "qux"],
)
with pytest.raises(TypeError):
msg = (
r"only list-like or dict-like objects are allowed "
r"to be passed to DataFrame.isin\(\), you passed a 'str'"
)
with pytest.raises(TypeError, match=msg):
df.isin("a")

with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
df.isin("aaa")

def test_isin_df(self):
Expand Down Expand Up @@ -92,7 +96,8 @@ def test_isin_df_dupe_values(self):
df1 = DataFrame({"A": [1, 2, 3, 4], "B": [2, np.nan, 4, 4]})
# just cols duped
df2 = DataFrame([[0, 2], [12, 4], [2, np.nan], [4, 5]], columns=["B", "B"])
with pytest.raises(ValueError):
msg = r"cannot compute isin with a duplicate axis\."
with pytest.raises(ValueError, match=msg):
df1.isin(df2)

# just index duped
Expand All @@ -101,12 +106,12 @@ def test_isin_df_dupe_values(self):
columns=["A", "B"],
index=[0, 0, 1, 1],
)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
df1.isin(df2)

# cols and index:
df2.columns = ["B", "B"]
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
df1.isin(df2)

def test_isin_dupe_self(self):
Expand Down
Loading