From 9262c70fad9e0e1d28c8a080e85bb91d703f885a Mon Sep 17 00:00:00 2001 From: Raisa Dzhamtyrova Date: Mon, 10 Feb 2020 16:09:05 +0000 Subject: [PATCH 1/9] add messages to tests --- .../tests/extension/decimal/test_decimal.py | 5 +- pandas/tests/extension/json/test_json.py | 5 +- pandas/tests/extension/test_boolean.py | 5 +- pandas/tests/extension/test_categorical.py | 3 +- .../tests/frame/indexing/test_categorical.py | 46 ++++++++++--------- pandas/tests/frame/indexing/test_indexing.py | 18 +++++--- pandas/tests/frame/indexing/test_where.py | 12 +++-- pandas/tests/frame/methods/test_diff.py | 2 +- pandas/tests/frame/methods/test_explode.py | 5 +- pandas/tests/frame/methods/test_isin.py | 13 ++++-- pandas/tests/frame/methods/test_quantile.py | 3 +- pandas/tests/frame/methods/test_replace.py | 4 +- pandas/tests/frame/methods/test_round.py | 23 ++++++---- .../tests/frame/methods/test_sort_values.py | 2 +- pandas/tests/frame/methods/test_to_dict.py | 6 ++- 15 files changed, 92 insertions(+), 60 deletions(-) diff --git a/pandas/tests/extension/decimal/test_decimal.py b/pandas/tests/extension/decimal/test_decimal.py index a78e4bb34e42a..fa69f025bd0fd 100644 --- a/pandas/tests/extension/decimal/test_decimal.py +++ b/pandas/tests/extension/decimal/test_decimal.py @@ -146,9 +146,10 @@ class TestMissing(BaseDecimal, base.BaseMissingTests): class Reduce: def check_reduce(self, s, op_name, skipna): - if op_name in ["median", "skew", "kurt"]: - with pytest.raises(NotImplementedError): + # breakpoint() + msg = r"decimal does not support the .* operation" + with pytest.raises(NotImplementedError, match=msg): getattr(s, op_name)(skipna=skipna) else: diff --git a/pandas/tests/extension/json/test_json.py b/pandas/tests/extension/json/test_json.py index f7ca99be2adea..6a85c4efe29d4 100644 --- a/pandas/tests/extension/json/test_json.py +++ b/pandas/tests/extension/json/test_json.py @@ -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()) diff --git a/pandas/tests/extension/test_boolean.py b/pandas/tests/extension/test_boolean.py index 0c6b187eac1fc..26795b9d23eff 100644 --- a/pandas/tests/extension/test_boolean.py +++ b/pandas/tests/extension/test_boolean.py @@ -112,7 +112,8 @@ 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, the \`-\` operator, is not supported.*" + with pytest.raises(TypeError, match=msg): op(s, other) return @@ -139,7 +140,7 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError): expected[result.isna()] = np.nan self.assert_series_equal(result, expected) else: - with pytest.raises(exc): + with pytest.raises(exc, match=msg): op(s, other) def _check_divmod_op(self, s, op, other, exc=None): diff --git a/pandas/tests/extension/test_categorical.py b/pandas/tests/extension/test_categorical.py index 336b23e54d74c..69a97f5c9fe02 100644 --- a/pandas/tests/extension/test_categorical.py +++ b/pandas/tests/extension/test_categorical.py @@ -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) diff --git a/pandas/tests/frame/indexing/test_categorical.py b/pandas/tests/frame/indexing/test_categorical.py index 3a472a8b58b6c..e2a7b11eb7a13 100644 --- a/pandas/tests/frame/indexing/test_categorical.py +++ b/pandas/tests/frame/indexing/test_categorical.py @@ -115,7 +115,11 @@ 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): + msg = ( + r"(:?Cannot setitem on a Categorical with a new category.*)" + r"|(:?Cannot set a Categorical with another, without identical categories)" + ) + with pytest.raises(ValueError, match=msg): df = orig.copy() df.iloc[2, 0] = "c" @@ -125,7 +129,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=msg): df = orig.copy() df.iloc[2, :] = ["c", 2] @@ -134,7 +138,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=msg): df = orig.copy() df.iloc[2:4, :] = [["c", 2], ["c", 2]] @@ -144,12 +148,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=msg): # 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=msg): # different values df = orig.copy() df.iloc[2:4, 0] = Categorical(list("cc"), categories=list("abc")) @@ -160,7 +164,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=msg): df.iloc[2:4, 0] = ["c", "c"] # loc @@ -175,7 +179,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=msg): df = orig.copy() df.loc["j", "cats"] = "c" @@ -185,7 +189,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=msg): df = orig.copy() df.loc["j", :] = ["c", 2] @@ -194,7 +198,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=msg): df = orig.copy() df.loc["j":"k", :] = [["c", 2], ["c", 2]] @@ -204,14 +208,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=msg): # 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=msg): # different values df = orig.copy() df.loc["j":"k", "cats"] = Categorical( @@ -224,7 +228,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=msg): df.loc["j":"k", "cats"] = ["c", "c"] # loc @@ -239,7 +243,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=msg): df = orig.copy() df.loc["j", df.columns[0]] = "c" @@ -249,7 +253,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=msg): df = orig.copy() df.loc["j", :] = ["c", 2] @@ -258,7 +262,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=msg): df = orig.copy() df.loc["j":"k", :] = [["c", 2], ["c", 2]] @@ -268,14 +272,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=msg): # 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=msg): # different values df = orig.copy() df.loc["j":"k", df.columns[0]] = Categorical( @@ -288,7 +292,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=msg): df.loc["j":"k", df.columns[0]] = ["c", "c"] # iat @@ -297,7 +301,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=msg): df = orig.copy() df.iat[2, 0] = "c" @@ -308,7 +312,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=msg): df = orig.copy() df.at["j", "cats"] = "c" @@ -332,7 +336,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=msg): df = orig.copy() df.at["j", "cats"] = "c" diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index d892e3d637772..6d7f1e465dfa3 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -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_ @@ -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() @@ -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): @@ -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() @@ -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() @@ -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() diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index 507b2e9cd237b..eee754a47fb8c 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -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 @@ -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 @@ -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 @@ -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] diff --git a/pandas/tests/frame/methods/test_diff.py b/pandas/tests/frame/methods/test_diff.py index ffdb6d41ebda5..bc52ab8a20bef 100644 --- a/pandas/tests/frame/methods/test_diff.py +++ b/pandas/tests/frame/methods/test_diff.py @@ -74,7 +74,7 @@ def test_diff_datetime_axis1(self, tz): ) tm.assert_frame_equal(result, expected) else: - with pytest.raises(NotImplementedError): + with pytest.raises(NotImplementedError, match=""): result = df.diff(axis=1) def test_diff_timedelta(self): diff --git a/pandas/tests/frame/methods/test_explode.py b/pandas/tests/frame/methods/test_explode.py index 76c87ed355492..f4ce766c57d5b 100644 --- a/pandas/tests/frame/methods/test_explode.py +++ b/pandas/tests/frame/methods/test_explode.py @@ -9,11 +9,12 @@ 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): + msg = r"(:?columns must be unique)|(:?column must be a scalar)" + with pytest.raises(ValueError, match=msg): df.explode(list("AA")) df.columns = list("AA") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.explode("A") diff --git a/pandas/tests/frame/methods/test_isin.py b/pandas/tests/frame/methods/test_isin.py index 0eb94afc99d94..7900e82505e6c 100644 --- a/pandas/tests/frame/methods/test_isin.py +++ b/pandas/tests/frame/methods/test_isin.py @@ -60,10 +60,12 @@ 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): @@ -92,7 +94,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 = "cannot compute isin with a duplicate axis." + with pytest.raises(ValueError, match=msg): df1.isin(df2) # just index duped @@ -101,12 +104,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): diff --git a/pandas/tests/frame/methods/test_quantile.py b/pandas/tests/frame/methods/test_quantile.py index 64461c08d34f4..9c52e8ec5620f 100644 --- a/pandas/tests/frame/methods/test_quantile.py +++ b/pandas/tests/frame/methods/test_quantile.py @@ -75,7 +75,8 @@ def test_quantile_axis_mixed(self): tm.assert_series_equal(result, expected) # must raise - with pytest.raises(TypeError): + msg = "'<' not supported between instances of 'Timestamp' and 'float'" + with pytest.raises(TypeError, match=msg): df.quantile(0.5, axis=1, numeric_only=False) def test_quantile_axis_parameter(self): diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 92b74c4409d7d..57f000a45a619 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1308,7 +1308,9 @@ def test_categorical_replace_with_dict(self, replace_dict, final_data): expected["b"] = expected["b"].cat.set_categories([1, 2, 3]) result = df.replace(replace_dict, 3) tm.assert_frame_equal(result, expected) - with pytest.raises(AssertionError): + msg = r"Attributes of DataFrame.iloc\[:, 0\]" + r"\(column name=\"a\"\) are different.*" + with pytest.raises(AssertionError, match=msg): # ensure non-inplace call does not affect original tm.assert_frame_equal(df, expected) df.replace(replace_dict, 3, inplace=True) diff --git a/pandas/tests/frame/methods/test_round.py b/pandas/tests/frame/methods/test_round.py index 0865e03cedc50..6dcdf49e93729 100644 --- a/pandas/tests/frame/methods/test_round.py +++ b/pandas/tests/frame/methods/test_round.py @@ -34,7 +34,8 @@ def test_round(self): # Round with a list round_list = [1, 2] - with pytest.raises(TypeError): + msg = "decimals must be an integer, a dict-like or a Series" + with pytest.raises(TypeError, match=msg): df.round(round_list) # Round with a dictionary @@ -57,34 +58,37 @@ def test_round(self): # float input to `decimals` non_int_round_dict = {"col1": 1, "col2": 0.5} - with pytest.raises(TypeError): + msg = "integer argument expected, got float" + with pytest.raises(TypeError, match=msg): df.round(non_int_round_dict) # String input non_int_round_dict = {"col1": 1, "col2": "foo"} - with pytest.raises(TypeError): + msg = r"an integer is required \(got type str\)" + with pytest.raises(TypeError, match=msg): df.round(non_int_round_dict) non_int_round_Series = Series(non_int_round_dict) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): df.round(non_int_round_Series) # List input non_int_round_dict = {"col1": 1, "col2": [1, 2]} - with pytest.raises(TypeError): + msg = r"an integer is required \(got type list\)" + with pytest.raises(TypeError, match=msg): df.round(non_int_round_dict) non_int_round_Series = Series(non_int_round_dict) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): df.round(non_int_round_Series) # Non integer Series inputs non_int_round_Series = Series(non_int_round_dict) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): df.round(non_int_round_Series) non_int_round_Series = Series(non_int_round_dict) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): df.round(non_int_round_Series) # Negative numbers @@ -103,7 +107,8 @@ def test_round(self): {"col1": [1.123, 2.123, 3.123], "col2": [1.2, 2.2, 3.2]} ) - with pytest.raises(TypeError): + msg = "integer argument expected, got float" + with pytest.raises(TypeError, match=msg): df.round(nan_round_Series) # Make sure this doesn't break existing Series.round diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index 96f4d6ed90d6b..5a25d1c2c0894 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -458,7 +458,7 @@ def test_sort_values_na_position_with_categories_raises(self): } ) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="invalid na_position: bad_position"): df.sort_values(by="c", ascending=False, na_position="bad_position") @pytest.mark.parametrize("inplace", [True, False]) diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index 40393721c4ac6..71373c1fd180f 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -132,7 +132,11 @@ def test_to_dict(self, mapping): def test_to_dict_errors(self, mapping): # GH#16122 df = DataFrame(np.random.randn(3, 3)) - with pytest.raises(TypeError): + msg = ( + r"(:?unsupported type: )" + r"|(:?to_dict\(\) only accepts initialized defaultdicts)" + ) + with pytest.raises(TypeError, match=msg): df.to_dict(into=mapping) def test_to_dict_not_unique_warning(self): From 26b3cdf2c0c301cb341309a1ee13cc32ef41f39d Mon Sep 17 00:00:00 2001 From: Raisa Dzhamtyrova Date: Mon, 10 Feb 2020 17:14:48 +0000 Subject: [PATCH 2/9] changes to test_boolean.py --- pandas/tests/extension/test_boolean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/extension/test_boolean.py b/pandas/tests/extension/test_boolean.py index 26795b9d23eff..00ee95f30eb85 100644 --- a/pandas/tests/extension/test_boolean.py +++ b/pandas/tests/extension/test_boolean.py @@ -112,7 +112,7 @@ 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") - msg = r"numpy boolean subtract, the \`-\` operator, is not supported.*" + msg = r"numpy boolean subtract, the \`-\` operator.*" with pytest.raises(TypeError, match=msg): op(s, other) From 5e811bda9fbbac45a5f1a6ea7b917206e8af439a Mon Sep 17 00:00:00 2001 From: Raisa Dzhamtyrova Date: Mon, 10 Feb 2020 21:40:53 +0000 Subject: [PATCH 3/9] split error messages --- .../tests/extension/decimal/test_decimal.py | 2 +- .../tests/frame/indexing/test_categorical.py | 49 ++++++++++--------- pandas/tests/frame/methods/test_explode.py | 5 +- pandas/tests/frame/methods/test_isin.py | 6 ++- pandas/tests/frame/methods/test_replace.py | 6 ++- 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/pandas/tests/extension/decimal/test_decimal.py b/pandas/tests/extension/decimal/test_decimal.py index fa69f025bd0fd..f4ffcb8d0f109 100644 --- a/pandas/tests/extension/decimal/test_decimal.py +++ b/pandas/tests/extension/decimal/test_decimal.py @@ -146,8 +146,8 @@ class TestMissing(BaseDecimal, base.BaseMissingTests): class Reduce: def check_reduce(self, s, op_name, skipna): + if op_name in ["median", "skew", "kurt"]: - # breakpoint() msg = r"decimal does not support the .* operation" with pytest.raises(NotImplementedError, match=msg): getattr(s, op_name)(skipna=skipna) diff --git a/pandas/tests/frame/indexing/test_categorical.py b/pandas/tests/frame/indexing/test_categorical.py index e2a7b11eb7a13..f5b3f980cc534 100644 --- a/pandas/tests/frame/indexing/test_categorical.py +++ b/pandas/tests/frame/indexing/test_categorical.py @@ -115,11 +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 - msg = ( - r"(:?Cannot setitem on a Categorical with a new category.*)" - r"|(:?Cannot set a Categorical with another, without identical categories)" + msg1 = ( + "Cannot setitem on a Categorical with a new category, " + "set the categories first" ) - with pytest.raises(ValueError, match=msg): + msg2 = "Cannot set a Categorical with another, without identical categories" + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.iloc[2, 0] = "c" @@ -129,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.iloc[2, :] = ["c", 2] @@ -138,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.iloc[2:4, :] = [["c", 2], ["c", 2]] @@ -148,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, match=msg): + 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, match=msg): + with pytest.raises(ValueError, match=msg2): # different values df = orig.copy() df.iloc[2:4, 0] = Categorical(list("cc"), categories=list("abc")) @@ -164,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, match=msg): + with pytest.raises(ValueError, match=msg1): df.iloc[2:4, 0] = ["c", "c"] # loc @@ -179,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.loc["j", "cats"] = "c" @@ -189,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.loc["j", :] = ["c", 2] @@ -198,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.loc["j":"k", :] = [["c", 2], ["c", 2]] @@ -208,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, match=msg): + 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, match=msg): + with pytest.raises(ValueError, match=msg2): # different values df = orig.copy() df.loc["j":"k", "cats"] = Categorical( @@ -228,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, match=msg): + with pytest.raises(ValueError, match=msg1): df.loc["j":"k", "cats"] = ["c", "c"] # loc @@ -243,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.loc["j", df.columns[0]] = "c" @@ -253,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.loc["j", :] = ["c", 2] @@ -262,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.loc["j":"k", :] = [["c", 2], ["c", 2]] @@ -272,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, match=msg): + 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, match=msg): + with pytest.raises(ValueError, match=msg2): # different values df = orig.copy() df.loc["j":"k", df.columns[0]] = Categorical( @@ -292,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, match=msg): + with pytest.raises(ValueError, match=msg1): df.loc["j":"k", df.columns[0]] = ["c", "c"] # iat @@ -301,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.iat[2, 0] = "c" @@ -312,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.at["j", "cats"] = "c" @@ -336,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, match=msg): + with pytest.raises(ValueError, match=msg1): df = orig.copy() df.at["j", "cats"] = "c" diff --git a/pandas/tests/frame/methods/test_explode.py b/pandas/tests/frame/methods/test_explode.py index f4ce766c57d5b..bad8349ec977b 100644 --- a/pandas/tests/frame/methods/test_explode.py +++ b/pandas/tests/frame/methods/test_explode.py @@ -9,12 +9,11 @@ def test_error(): df = pd.DataFrame( {"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1} ) - msg = r"(:?columns must be unique)|(:?column must be a scalar)" - with pytest.raises(ValueError, match=msg): + with pytest.raises(ValueError, match="column must be a scalar"): df.explode(list("AA")) df.columns = list("AA") - with pytest.raises(ValueError, match=msg): + with pytest.raises(ValueError, match="columns must be unique"): df.explode("A") diff --git a/pandas/tests/frame/methods/test_isin.py b/pandas/tests/frame/methods/test_isin.py index 7900e82505e6c..f7d3f95d7bbf6 100644 --- a/pandas/tests/frame/methods/test_isin.py +++ b/pandas/tests/frame/methods/test_isin.py @@ -60,8 +60,10 @@ def test_isin_with_string_scalar(self): }, index=["foo", "bar", "baz", "qux"], ) - msg = r"only list-like or dict-like objects are allowed" - r"to be passed to DataFrame.isin\(\), you passed a 'str'" + 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") diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 57f000a45a619..f4cdbb519b872 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1308,8 +1308,10 @@ def test_categorical_replace_with_dict(self, replace_dict, final_data): expected["b"] = expected["b"].cat.set_categories([1, 2, 3]) result = df.replace(replace_dict, 3) tm.assert_frame_equal(result, expected) - msg = r"Attributes of DataFrame.iloc\[:, 0\]" - r"\(column name=\"a\"\) are different.*" + msg = ( + r"Attributes of DataFrame.iloc\[:, 0\] " + r"\(column name=\"a\"\) are different.*" + ) with pytest.raises(AssertionError, match=msg): # ensure non-inplace call does not affect original tm.assert_frame_equal(df, expected) From 18b55813f58d470bf5acb074853ab1a5031c8f65 Mon Sep 17 00:00:00 2001 From: Raisa Dzhamtyrova Date: Mon, 10 Feb 2020 22:13:09 +0000 Subject: [PATCH 4/9] change to test_isin.py --- pandas/tests/frame/methods/test_isin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/methods/test_isin.py b/pandas/tests/frame/methods/test_isin.py index f7d3f95d7bbf6..65ad76382e5c6 100644 --- a/pandas/tests/frame/methods/test_isin.py +++ b/pandas/tests/frame/methods/test_isin.py @@ -61,7 +61,7 @@ def test_isin_with_string_scalar(self): index=["foo", "bar", "baz", "qux"], ) msg = ( - r"only list-like or dict-like objects are allowed" + 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): From 965946dec87aa858c5b762ca06031850f5dd82de Mon Sep 17 00:00:00 2001 From: Raisa Dzhamtyrova Date: Tue, 11 Feb 2020 11:11:25 +0000 Subject: [PATCH 5/9] changes to test_boolean.py and test_indexing.py --- pandas/tests/extension/test_boolean.py | 3 +-- pandas/tests/frame/indexing/test_indexing.py | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/tests/extension/test_boolean.py b/pandas/tests/extension/test_boolean.py index 00ee95f30eb85..04f49a335292b 100644 --- a/pandas/tests/extension/test_boolean.py +++ b/pandas/tests/extension/test_boolean.py @@ -115,7 +115,6 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError): msg = r"numpy boolean subtract, the \`-\` operator.*" with pytest.raises(TypeError, match=msg): op(s, other) - return result = op(s, other) @@ -140,7 +139,7 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError): expected[result.isna()] = np.nan self.assert_series_equal(result, expected) else: - with pytest.raises(exc, match=msg): + with pytest.raises(exc): op(s, other) def _check_divmod_op(self, s, op, other, exc=None): diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 6d7f1e465dfa3..fcf0a41e0f74e 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -481,7 +481,7 @@ def test_setitem(self, float_frame): # so raise/warn smaller = float_frame[:2] - msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame.*" + 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"] @@ -866,7 +866,7 @@ def test_fancy_getitem_slice_mixed(self, float_frame, float_string_frame): # setting it triggers setting with copy sliced = float_frame.iloc[:, -3:] - msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame.*" + 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 @@ -1472,7 +1472,7 @@ def test_iloc_row(self): # verify slice is view # setting it makes it raise/warn - msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame.*" + 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 @@ -1504,7 +1504,7 @@ def test_iloc_col(self): # verify slice is view # and that we are setting a copy - msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame.*" + 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 From c22044c69daa0cd6311e731f1b781ac2478e4e78 Mon Sep 17 00:00:00 2001 From: Raisa Dzhamtyrova Date: Tue, 11 Feb 2020 15:43:10 +0000 Subject: [PATCH 6/9] revert changes to test_diff.py --- pandas/tests/frame/methods/test_diff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/methods/test_diff.py b/pandas/tests/frame/methods/test_diff.py index bc52ab8a20bef..ffdb6d41ebda5 100644 --- a/pandas/tests/frame/methods/test_diff.py +++ b/pandas/tests/frame/methods/test_diff.py @@ -74,7 +74,7 @@ def test_diff_datetime_axis1(self, tz): ) tm.assert_frame_equal(result, expected) else: - with pytest.raises(NotImplementedError, match=""): + with pytest.raises(NotImplementedError): result = df.diff(axis=1) def test_diff_timedelta(self): From f1686fce2791ba5a393362017f9f1f6a8ecfd753 Mon Sep 17 00:00:00 2001 From: Raisa Dzhamtyrova Date: Tue, 11 Feb 2020 16:46:34 +0000 Subject: [PATCH 7/9] cleanups to the code --- pandas/tests/extension/json/test_json.py | 2 +- pandas/tests/extension/test_boolean.py | 2 +- pandas/tests/frame/methods/test_isin.py | 2 +- pandas/tests/frame/methods/test_replace.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/extension/json/test_json.py b/pandas/tests/extension/json/test_json.py index 6a85c4efe29d4..d086896fb09c3 100644 --- a/pandas/tests/extension/json/test_json.py +++ b/pandas/tests/extension/json/test_json.py @@ -136,7 +136,7 @@ def test_custom_asserts(self): self.assert_frame_equal(a.to_frame(), a.to_frame()) b = pd.Series(data.take([0, 0, 1])) - msg = r"ExtensionArray are different.*" + msg = r"ExtensionArray are different" with pytest.raises(AssertionError, match=msg): self.assert_series_equal(a, b) diff --git a/pandas/tests/extension/test_boolean.py b/pandas/tests/extension/test_boolean.py index 04f49a335292b..a354075809fba 100644 --- a/pandas/tests/extension/test_boolean.py +++ b/pandas/tests/extension/test_boolean.py @@ -112,7 +112,7 @@ 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") - msg = r"numpy boolean subtract, the \`-\` operator.*" + msg = r"numpy boolean subtract, the \`-\` operator" with pytest.raises(TypeError, match=msg): op(s, other) return diff --git a/pandas/tests/frame/methods/test_isin.py b/pandas/tests/frame/methods/test_isin.py index 65ad76382e5c6..6307738021f68 100644 --- a/pandas/tests/frame/methods/test_isin.py +++ b/pandas/tests/frame/methods/test_isin.py @@ -96,7 +96,7 @@ 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"]) - msg = "cannot compute isin with a duplicate axis." + msg = r"cannot compute isin with a duplicate axis\." with pytest.raises(ValueError, match=msg): df1.isin(df2) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index f4cdbb519b872..d1ba0c0a6ea8f 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1310,7 +1310,7 @@ def test_categorical_replace_with_dict(self, replace_dict, final_data): tm.assert_frame_equal(result, expected) msg = ( r"Attributes of DataFrame.iloc\[:, 0\] " - r"\(column name=\"a\"\) are different.*" + r"\(column name=\"a\"\) are different" ) with pytest.raises(AssertionError, match=msg): # ensure non-inplace call does not affect original From dda42be7a2ee8dd0c556cd79923f4ee7f16f6683 Mon Sep 17 00:00:00 2001 From: Raisa Dzhamtyrova Date: Wed, 12 Feb 2020 15:30:58 +0000 Subject: [PATCH 8/9] changes to test_boolean.py and test_replace.py --- pandas/tests/extension/test_boolean.py | 2 +- pandas/tests/frame/methods/test_replace.py | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/pandas/tests/extension/test_boolean.py b/pandas/tests/extension/test_boolean.py index a354075809fba..e2331b69916fb 100644 --- a/pandas/tests/extension/test_boolean.py +++ b/pandas/tests/extension/test_boolean.py @@ -112,7 +112,7 @@ 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") - msg = r"numpy boolean subtract, the \`-\` operator" + msg = r"numpy boolean subtract" with pytest.raises(TypeError, match=msg): op(s, other) return diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index d1ba0c0a6ea8f..92b74c4409d7d 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1308,11 +1308,7 @@ def test_categorical_replace_with_dict(self, replace_dict, final_data): expected["b"] = expected["b"].cat.set_categories([1, 2, 3]) result = df.replace(replace_dict, 3) tm.assert_frame_equal(result, expected) - msg = ( - r"Attributes of DataFrame.iloc\[:, 0\] " - r"\(column name=\"a\"\) are different" - ) - with pytest.raises(AssertionError, match=msg): + with pytest.raises(AssertionError): # ensure non-inplace call does not affect original tm.assert_frame_equal(df, expected) df.replace(replace_dict, 3, inplace=True) From f54510d8c7b1adb6e57978df526d139533bfbd1d Mon Sep 17 00:00:00 2001 From: Raisa Dzhamtyrova Date: Tue, 18 Feb 2020 09:12:46 +0000 Subject: [PATCH 9/9] change error message in test_to_dict.py --- pandas/tests/frame/methods/test_to_dict.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index 71373c1fd180f..cd9bd169322fd 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -132,9 +132,11 @@ def test_to_dict(self, mapping): def test_to_dict_errors(self, mapping): # GH#16122 df = DataFrame(np.random.randn(3, 3)) - msg = ( - r"(:?unsupported type: )" - r"|(:?to_dict\(\) only accepts initialized defaultdicts)" + msg = "|".join( + [ + "unsupported type: ", + r"to_dict\(\) only accepts initialized defaultdicts", + ] ) with pytest.raises(TypeError, match=msg): df.to_dict(into=mapping)