From 1786b2dc5cba48c6c90f00ac36b034e7d99ae038 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 10 Mar 2020 21:05:51 -0400 Subject: [PATCH 01/19] Adding message check to pytest.raises for test_dtypes.py Found a type-o in one of the error messages and corrected it. --- pandas/core/dtypes/dtypes.py | 2 +- pandas/tests/dtypes/test_dtypes.py | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index d00b46700981c..c47806513baca 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -541,7 +541,7 @@ def validate_categories(categories, fastpath: bool = False): if not fastpath: if categories.hasnans: - raise ValueError("Categorial categories cannot be null") + raise ValueError("Categorical categories cannot be null") if not categories.is_unique: raise ValueError("Categorical categories must be unique") diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index a599a086ae92b..07347e2841c54 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -344,7 +344,7 @@ def test_hash_vs_equality(self): assert hash(dtype) == hash(dtype3) def test_construction(self): - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid frequency: xx"): PeriodDtype("xx") for s in ["period[D]", "Period[D]", "D"]: @@ -397,16 +397,17 @@ def test_construction_from_string(self): assert is_dtype_equal(self.dtype, result) result = PeriodDtype.construct_from_string("period[D]") assert is_dtype_equal(self.dtype, result) - with pytest.raises(TypeError): + msg = "Cannot construct a 'PeriodDtype' from " + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("foo") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("period[foo]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("foo[D]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("datetime64[ns]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]") with pytest.raises(TypeError, match="list"): @@ -458,7 +459,8 @@ def test_basic(self): def test_empty(self): dt = PeriodDtype() - with pytest.raises(AttributeError): + msg = "object has no attribute 'freqstr'" + with pytest.raises(AttributeError, match=msg): str(dt) def test_not_string(self): @@ -744,11 +746,13 @@ def test_order_hashes_different(self, v1, v2): assert c1 is not c3 def test_nan_invalid(self): - with pytest.raises(ValueError): + msg = "Categorial categories cannot be null" + with pytest.raises(ValueError, match=msg): CategoricalDtype([1, 2, np.nan]) def test_non_unique_invalid(self): - with pytest.raises(ValueError): + msg = "Categorical categories must be unique" + with pytest.raises(ValueError, match=msg): CategoricalDtype([1, 2, 1]) def test_same_categories_different_order(self): From bf777e83d1708b5810657273912422d2dd971feb Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 10 Mar 2020 21:16:25 -0400 Subject: [PATCH 02/19] Readding message check to pytest.raises for test_dtypes.py They got undone due to new commits to pandas. --- pandas/tests/dtypes/test_dtypes.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 55b1ac819049d..e27a6b2102052 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -361,7 +361,7 @@ def test_hash_vs_equality(self, dtype): assert hash(dtype) == hash(dtype3) def test_construction(self): - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid frequency: xx"): PeriodDtype("xx") for s in ["period[D]", "Period[D]", "D"]: @@ -414,16 +414,17 @@ def test_construction_from_string(self, dtype): assert is_dtype_equal(dtype, result) result = PeriodDtype.construct_from_string("period[D]") assert is_dtype_equal(dtype, result) - with pytest.raises(TypeError): + msg = "Cannot construct a 'PeriodDtype' from " + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("foo") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("period[foo]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("foo[D]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("datetime64[ns]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]") with pytest.raises(TypeError, match="list"): @@ -475,7 +476,8 @@ def test_basic(self, dtype): def test_empty(self): dt = PeriodDtype() - with pytest.raises(AttributeError): + msg = "object has no attribute 'freqstr'" + with pytest.raises(AttributeError, match=msg): str(dt) def test_not_string(self): @@ -764,11 +766,13 @@ def test_order_hashes_different(self, v1, v2): assert c1 is not c3 def test_nan_invalid(self): - with pytest.raises(ValueError): + msg = "Categorical categories cannot be null" + with pytest.raises(ValueError, match=msg): CategoricalDtype([1, 2, np.nan]) def test_non_unique_invalid(self): - with pytest.raises(ValueError): + msg = "Categorical categories must be unique" + with pytest.raises(ValueError, match=msg): CategoricalDtype([1, 2, 1]) def test_same_categories_different_order(self): From 20094d9ee6b27c29541bfa3fe6726c0412489217 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 20:47:07 -0400 Subject: [PATCH 03/19] Fixed bare pytest raises in test_indexing.py. --- pandas/tests/indexes/categorical/test_indexing.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/categorical/test_indexing.py b/pandas/tests/indexes/categorical/test_indexing.py index 507e38d9acac2..d6ccfe50059c6 100644 --- a/pandas/tests/indexes/categorical/test_indexing.py +++ b/pandas/tests/indexes/categorical/test_indexing.py @@ -65,7 +65,8 @@ def test_take_fill_value(self): with pytest.raises(ValueError, match=msg): idx.take(np.array([1, 0, -5]), fill_value=True) - with pytest.raises(IndexError): + msg = "index -5 is out of bounds for size 3" + with pytest.raises(IndexError, match=msg): idx.take(np.array([1, -5])) def test_take_fill_value_datetime(self): @@ -104,7 +105,8 @@ def test_take_fill_value_datetime(self): with pytest.raises(ValueError, match=msg): idx.take(np.array([1, 0, -5]), fill_value=True) - with pytest.raises(IndexError): + msg = "index -5 is out of bounds for size 3" + with pytest.raises(IndexError, match=msg): idx.take(np.array([1, -5])) def test_take_invalid_kwargs(self): From 617f03c704d5431b29d8c1bc32615f28e7086eb4 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 21:51:57 -0400 Subject: [PATCH 04/19] Fixed bare pytest raises in test_replace.py --- pandas/tests/frame/methods/test_replace.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index ee89562261b19..1f63ded5536c1 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1308,7 +1308,11 @@ 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 = ( + 'Attributes of DataFrame.iloc[:, 0] ' + '(column name="a") are different' + ) + with pytest.raises(AssertionError, match=re.escape(msg)): # ensure non-inplace call does not affect original tm.assert_frame_equal(df, expected) df.replace(replace_dict, 3, inplace=True) From 1991131837284d59a76f1bb2b3771a5da52ea384 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 22:46:28 -0400 Subject: [PATCH 05/19] Fixed bare pytest raises in test_apply.py --- pandas/tests/frame/test_apply.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index ee3cd59c27b44..214577c02106e 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -49,7 +49,11 @@ def test_apply(self, float_frame): # invalid axis df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "a", "c"]) - with pytest.raises(ValueError): + msg = ( + "No axis named 2 for object type " + "" + ) + with pytest.raises(ValueError, match=msg): df.apply(lambda x: x, 2) # GH 9573 @@ -221,7 +225,8 @@ def test_apply_broadcast_error(self, int_frame_const_col): df = int_frame_const_col # > 1 ndim - with pytest.raises(ValueError): + msg = "too many dims to broadcast" + with pytest.raises(ValueError, match=msg): df.apply( lambda x: np.array([1, 2]).reshape(-1, 2), axis=1, @@ -229,10 +234,11 @@ def test_apply_broadcast_error(self, int_frame_const_col): ) # cannot broadcast - with pytest.raises(ValueError): + msg = "cannot broadcast result" + with pytest.raises(ValueError, match=msg): df.apply(lambda x: [1, 2], axis=1, result_type="broadcast") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.apply(lambda x: Series([1, 2]), axis=1, result_type="broadcast") def test_apply_raw(self, float_frame, mixed_type_frame): @@ -950,7 +956,11 @@ def test_result_type_error(self, result_type, int_frame_const_col): # allowed result_type df = int_frame_const_col - with pytest.raises(ValueError): + msg = ( + "invalid value for result_type, must be one of " + "{None, 'reduce', 'broadcast', 'expand'}" + ) + with pytest.raises(ValueError, match=msg): df.apply(lambda x: [1, 2, 3], axis=1, result_type=result_type) @pytest.mark.parametrize( @@ -1046,14 +1056,16 @@ def test_agg_transform(self, axis, float_frame): def test_transform_and_agg_err(self, axis, float_frame): # cannot both transform and agg - with pytest.raises(ValueError): + msg = "transforms cannot produce aggregated results" + with pytest.raises(ValueError, match=msg): float_frame.transform(["max", "min"], axis=axis) - with pytest.raises(ValueError): + msg = "cannot combine transform and aggregation operations" + with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): float_frame.agg(["max", "sqrt"], axis=axis) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): float_frame.transform(["max", "sqrt"], axis=axis) @@ -1387,7 +1399,8 @@ def test_agg_cython_table_transform(self, df, func, expected, axis): ) def test_agg_cython_table_raises(self, df, func, expected, axis): # GH 21224 - with pytest.raises(expected): + msg = "can't multiply sequence by non-int of type 'str'" + with pytest.raises(expected, match=msg): df.agg(func, axis=axis) @pytest.mark.parametrize("num_cols", [2, 3, 5]) From f88064fecab553b818d792b0e2343ffe7474f49a Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 23:07:01 -0400 Subject: [PATCH 06/19] Fixed bare pytest raises in test_arithmetic.py --- pandas/tests/frame/test_arithmetic.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index b39c58b9931ab..3cf9fa8798baa 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -1,6 +1,7 @@ from collections import deque from datetime import datetime import operator +import re import numpy as np import pytest @@ -46,13 +47,17 @@ def check(df, df2): ) tm.assert_frame_equal(result, expected) - with pytest.raises(TypeError): + msg = re.escape( + "Invalid comparison between " + "dtype=datetime64[ns] and ndarray" + ) + with pytest.raises(TypeError, match=msg): x >= y - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): x > y - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): x < y - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): x <= y # GH4968 @@ -98,9 +103,13 @@ def test_timestamp_compare(self): result = right_f(pd.Timestamp("20010109"), df) tm.assert_frame_equal(result, expected) else: - with pytest.raises(TypeError): + msg = ( + "'(<|>)=?' not supported between " + "instances of 'Timestamp' and 'float'" + ) + with pytest.raises(TypeError, match=msg): left_f(df, pd.Timestamp("20010109")) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): right_f(pd.Timestamp("20010109"), df) # nats expected = left_f(df, pd.Timestamp("nat")) From 74476aec14392aca3ef360cda6957f4ebc6b68bf Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 23:13:13 -0400 Subject: [PATCH 07/19] Fixed bare pytest raises in test_axis_select_reindex.py --- pandas/tests/frame/test_axis_select_reindex.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index 7effa98fd8213..60f5360aac448 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -173,13 +173,15 @@ def test_drop_api_equivalence(self): res2 = df.drop(index=["a"], columns=["d"]) tm.assert_frame_equal(res1, res2) - with pytest.raises(ValueError): + msg = "Cannot specify both 'labels' and 'index'/'columns'" + with pytest.raises(ValueError, match=msg): df.drop(labels="a", index="b") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.drop(labels="a", columns="b") - with pytest.raises(ValueError): + msg = "Need to specify at least one of 'labels', 'index' or 'columns'" + with pytest.raises(ValueError, match=msg): df.drop(axis=1) def test_merge_join_different_levels(self): From fa46b4a11af8820ef021e4591a50a286b7ccc84b Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 23:26:09 -0400 Subject: [PATCH 08/19] Fixed bare pytest raises in test_axis_select_reindex.py --- pandas/tests/frame/test_axis_select_reindex.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index 60f5360aac448..cc2008bf01e64 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -618,7 +618,11 @@ def test_align_float(self, float_frame): tm.assert_index_equal(bf.index, Index([])) # Try to align DataFrame to Series along bad axis - with pytest.raises(ValueError): + msg = ( + "No axis named 2 for object type " + "" + ) + with pytest.raises(ValueError, match=msg): float_frame.align(af.iloc[0, :3], join="inner", axis=2) # align dataframe to series with broadcast or not From cf714b01af9460dde767a38f2c5016e9cb13219c Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 23:43:19 -0400 Subject: [PATCH 09/19] Fixed bare pytest raises in test_constructors.py --- pandas/tests/frame/test_constructors.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 95f812a99c579..7149ea23e2f3c 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2,6 +2,7 @@ from datetime import date, datetime, timedelta import functools import itertools +import re import numpy as np import numpy.ma as ma @@ -1401,7 +1402,8 @@ def test_constructor_list_of_dataclasses_error_thrown(self): Point = make_dataclass("Point", [("x", int), ("y", int)]) # expect TypeError - with pytest.raises(TypeError): + msg ="asdict() should be called on dataclass instances" + with pytest.raises(TypeError, match=re.escape(msg)): DataFrame([Point(0, 0), {"x": 1, "y": 0}]) def test_constructor_list_of_dict_order(self): From 9443bce0401b38176c218628ca69df5ba2530a33 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 23:49:33 -0400 Subject: [PATCH 10/19] Fixed bare pytest raises in test_dtypes.py.py --- pandas/tests/frame/test_dtypes.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index d1a7917bd127b..323a13a940ac3 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -1,5 +1,6 @@ from collections import OrderedDict from datetime import timedelta +import re import numpy as np import pytest @@ -636,7 +637,11 @@ def test_arg_for_errors_in_astype(self): df = DataFrame([1, 2, 3]) - with pytest.raises(ValueError): + msg = ( + "Expected value of kwarg 'errors' to be one of " + "['raise', 'ignore']. Supplied value is 'True'" + ) + with pytest.raises(ValueError, match=re.escape(msg)): df.astype(np.float64, errors=True) df.astype(np.int8, errors="ignore") From 5a06dd18c6687f57f23ba530f0dd28b1f34ba4d4 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Wed, 18 Mar 2020 21:02:37 -0400 Subject: [PATCH 11/19] Fixed bare pytest raises in test_operators.py --- pandas/tests/frame/test_operators.py | 31 ++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/pandas/tests/frame/test_operators.py b/pandas/tests/frame/test_operators.py index 542d9835bb5d3..71f3f0f0acc87 100644 --- a/pandas/tests/frame/test_operators.py +++ b/pandas/tests/frame/test_operators.py @@ -1,5 +1,6 @@ from decimal import Decimal import operator +import re import numpy as np import pytest @@ -51,9 +52,14 @@ def test_neg_object(self, df, expected): ], ) def test_neg_raises(self, df): - with pytest.raises(TypeError): + msg = ( + "bad operand type for unary -: 'str'|" + + "Unary negative expects numeric " + + re.escape("dtype, not datetime64[ns]") + ) + with pytest.raises(TypeError, match=msg): (-df) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): (-df["a"]) def test_invert(self, float_frame): @@ -116,9 +122,10 @@ def test_pos_object(self, df): "df", [pd.DataFrame({"a": pd.to_datetime(["2017-01-22", "1970-01-01"])})] ) def test_pos_raises(self, df): - with pytest.raises(TypeError): + msg = re.escape("Unary plus expects numeric dtype, not datetime64[ns]") + with pytest.raises(TypeError, match=msg): (+df) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): (+df["a"]) @@ -173,12 +180,14 @@ def test_logical_ops_invalid(self): df1 = DataFrame(1.0, index=[1], columns=["A"]) df2 = DataFrame(True, index=[1], columns=["A"]) - with pytest.raises(TypeError): + msg = re.escape("unsupported operand type(s) for |: 'float' and 'bool'") + with pytest.raises(TypeError, match=msg): df1 | df2 df1 = DataFrame("foo", index=[1], columns=["A"]) df2 = DataFrame(True, index=[1], columns=["A"]) - with pytest.raises(TypeError): + msg = re.escape("unsupported operand type(s) for |: 'str' and 'bool'") + with pytest.raises(TypeError, match=msg): df1 | df2 def test_logical_operators(self): @@ -594,7 +603,8 @@ def test_strings_to_numbers_comparisons_raises(self, compare_operators_no_eq_ne) ) f = getattr(operator, compare_operators_no_eq_ne) - with pytest.raises(TypeError): + msg = "'[<>]=?' not supported between instances of 'str' and 'int'" + with pytest.raises(TypeError, match=msg): f(df, 0) def test_comparison_protected_from_errstate(self): @@ -881,9 +891,12 @@ def test_alignment_non_pandas(self): align(df, val, "columns") val = np.zeros((3, 3, 3)) - with pytest.raises(ValueError): + msg = re.escape( + "Unable to coerce to Series/DataFrame, dim must be <= 2: (3, 3, 3)" + ) + with pytest.raises(ValueError, match=msg): align(df, val, "index") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): align(df, val, "columns") def test_no_warning(self, all_arithmetic_operators): From 058404b9cbd525fec167d8bcbfc6c6becdcdbda7 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Wed, 18 Mar 2020 21:39:58 -0400 Subject: [PATCH 12/19] Fixed bare pytest raises in test_reshape.py --- pandas/tests/frame/test_reshape.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_reshape.py b/pandas/tests/frame/test_reshape.py index 46a4a0a2af4ba..4f039baa5c7bd 100644 --- a/pandas/tests/frame/test_reshape.py +++ b/pandas/tests/frame/test_reshape.py @@ -695,10 +695,11 @@ def test_unstack_dtypes(self): def test_unstack_non_unique_index_names(self): idx = MultiIndex.from_tuples([("a", "b"), ("c", "d")], names=["c1", "c1"]) df = DataFrame([1, 2], index=idx) - with pytest.raises(ValueError): + msg = "The name c1 occurs multiple times, use a level number" + with pytest.raises(ValueError, match=msg): df.unstack("c1") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.T.stack("c1") def test_unstack_unused_levels(self): From 2faa42a309461b7a7987ec78bcfffa3db39de7f7 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Wed, 18 Mar 2020 21:43:03 -0400 Subject: [PATCH 13/19] Blacked the files before creating pull request --- pandas/tests/frame/methods/test_replace.py | 5 +---- pandas/tests/frame/test_apply.py | 9 +++------ pandas/tests/frame/test_arithmetic.py | 3 +-- pandas/tests/frame/test_axis_select_reindex.py | 5 +---- pandas/tests/frame/test_constructors.py | 2 +- pandas/tests/frame/test_operators.py | 6 +++--- 6 files changed, 10 insertions(+), 20 deletions(-) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 1f63ded5536c1..ce7c48ec41548 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1308,10 +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 = ( - 'Attributes of DataFrame.iloc[:, 0] ' - '(column name="a") are different' - ) + msg = "Attributes of DataFrame.iloc[:, 0] " '(column name="a") are different' with pytest.raises(AssertionError, match=re.escape(msg)): # ensure non-inplace call does not affect original tm.assert_frame_equal(df, expected) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 214577c02106e..c340533bf2d62 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -49,10 +49,7 @@ def test_apply(self, float_frame): # invalid axis df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "a", "c"]) - msg = ( - "No axis named 2 for object type " - "" - ) + msg = "No axis named 2 for object type " "" with pytest.raises(ValueError, match=msg): df.apply(lambda x: x, 2) @@ -957,8 +954,8 @@ def test_result_type_error(self, result_type, int_frame_const_col): df = int_frame_const_col msg = ( - "invalid value for result_type, must be one of " - "{None, 'reduce', 'broadcast', 'expand'}" + "invalid value for result_type, must be one of " + "{None, 'reduce', 'broadcast', 'expand'}" ) with pytest.raises(ValueError, match=msg): df.apply(lambda x: [1, 2, 3], axis=1, result_type=result_type) diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index 3cf9fa8798baa..544c5775d39ce 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -48,8 +48,7 @@ def check(df, df2): tm.assert_frame_equal(result, expected) msg = re.escape( - "Invalid comparison between " - "dtype=datetime64[ns] and ndarray" + "Invalid comparison between " "dtype=datetime64[ns] and ndarray" ) with pytest.raises(TypeError, match=msg): x >= y diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index cc2008bf01e64..335c8620c6ab5 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -618,10 +618,7 @@ def test_align_float(self, float_frame): tm.assert_index_equal(bf.index, Index([])) # Try to align DataFrame to Series along bad axis - msg = ( - "No axis named 2 for object type " - "" - ) + msg = "No axis named 2 for object type " "" with pytest.raises(ValueError, match=msg): float_frame.align(af.iloc[0, :3], join="inner", axis=2) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 7149ea23e2f3c..9f40e8c6931c8 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1402,7 +1402,7 @@ def test_constructor_list_of_dataclasses_error_thrown(self): Point = make_dataclass("Point", [("x", int), ("y", int)]) # expect TypeError - msg ="asdict() should be called on dataclass instances" + msg = "asdict() should be called on dataclass instances" with pytest.raises(TypeError, match=re.escape(msg)): DataFrame([Point(0, 0), {"x": 1, "y": 0}]) diff --git a/pandas/tests/frame/test_operators.py b/pandas/tests/frame/test_operators.py index 71f3f0f0acc87..2d910ae9529a5 100644 --- a/pandas/tests/frame/test_operators.py +++ b/pandas/tests/frame/test_operators.py @@ -53,9 +53,9 @@ def test_neg_object(self, df, expected): ) def test_neg_raises(self, df): msg = ( - "bad operand type for unary -: 'str'|" + - "Unary negative expects numeric " + - re.escape("dtype, not datetime64[ns]") + "bad operand type for unary -: 'str'|" + + "Unary negative expects numeric " + + re.escape("dtype, not datetime64[ns]") ) with pytest.raises(TypeError, match=msg): (-df) From f0d6e58e350a13f1cf8bd1f3d4e0243e527be2df Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Wed, 18 Mar 2020 23:59:40 -0400 Subject: [PATCH 14/19] Revert "Fixed bare pytest raises in test_indexing.py." This reverts commit 20094d9e This is handled in another file and is awaiting approval. --- pandas/tests/indexes/categorical/test_indexing.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/categorical/test_indexing.py b/pandas/tests/indexes/categorical/test_indexing.py index d6ccfe50059c6..507e38d9acac2 100644 --- a/pandas/tests/indexes/categorical/test_indexing.py +++ b/pandas/tests/indexes/categorical/test_indexing.py @@ -65,8 +65,7 @@ def test_take_fill_value(self): with pytest.raises(ValueError, match=msg): idx.take(np.array([1, 0, -5]), fill_value=True) - msg = "index -5 is out of bounds for size 3" - with pytest.raises(IndexError, match=msg): + with pytest.raises(IndexError): idx.take(np.array([1, -5])) def test_take_fill_value_datetime(self): @@ -105,8 +104,7 @@ def test_take_fill_value_datetime(self): with pytest.raises(ValueError, match=msg): idx.take(np.array([1, 0, -5]), fill_value=True) - msg = "index -5 is out of bounds for size 3" - with pytest.raises(IndexError, match=msg): + with pytest.raises(IndexError): idx.take(np.array([1, -5])) def test_take_invalid_kwargs(self): From 5546949d3e9f3fe32357782d935506a1aad6695f Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Thu, 19 Mar 2020 00:00:15 -0400 Subject: [PATCH 15/19] Fixed the issues with Black that were brought up by the linter test. --- pandas/tests/frame/methods/test_replace.py | 2 +- pandas/tests/frame/test_apply.py | 2 +- pandas/tests/frame/test_arithmetic.py | 2 +- pandas/tests/frame/test_axis_select_reindex.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index ce7c48ec41548..34940b50a7f22 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1308,7 +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 = "Attributes of DataFrame.iloc[:, 0] " '(column name="a") are different' + msg = 'Attributes of DataFrame.iloc[:, 0] (column name="a") are different' with pytest.raises(AssertionError, match=re.escape(msg)): # ensure non-inplace call does not affect original tm.assert_frame_equal(df, expected) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index c340533bf2d62..8a2e7a2c5882e 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -49,7 +49,7 @@ def test_apply(self, float_frame): # invalid axis df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "a", "c"]) - msg = "No axis named 2 for object type " "" + msg = "No axis named 2 for object type " with pytest.raises(ValueError, match=msg): df.apply(lambda x: x, 2) diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index 544c5775d39ce..2150e1da9e8ad 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -48,7 +48,7 @@ def check(df, df2): tm.assert_frame_equal(result, expected) msg = re.escape( - "Invalid comparison between " "dtype=datetime64[ns] and ndarray" + "Invalid comparison between dtype=datetime64[ns] and ndarray" ) with pytest.raises(TypeError, match=msg): x >= y diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index 335c8620c6ab5..4070984c84fec 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -618,7 +618,7 @@ def test_align_float(self, float_frame): tm.assert_index_equal(bf.index, Index([])) # Try to align DataFrame to Series along bad axis - msg = "No axis named 2 for object type " "" + msg = "No axis named 2 for object type " with pytest.raises(ValueError, match=msg): float_frame.align(af.iloc[0, :3], join="inner", axis=2) From e9645fecb2ae6b45262d66577cd4bf37baa1f4e7 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Thu, 19 Mar 2020 09:15:06 -0400 Subject: [PATCH 16/19] Update pandas/tests/frame/test_operators.py Co-Authored-By: Simon Hawkins --- pandas/tests/frame/test_operators.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_operators.py b/pandas/tests/frame/test_operators.py index 2d910ae9529a5..a84898c659a02 100644 --- a/pandas/tests/frame/test_operators.py +++ b/pandas/tests/frame/test_operators.py @@ -54,8 +54,7 @@ def test_neg_object(self, df, expected): def test_neg_raises(self, df): msg = ( "bad operand type for unary -: 'str'|" - + "Unary negative expects numeric " - + re.escape("dtype, not datetime64[ns]") + r"Unary negative expects numeric dtype, not datetime64\[ns\]" ) with pytest.raises(TypeError, match=msg): (-df) From d90454a9d588464f07c842669238cdbb6123e771 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Fri, 20 Mar 2020 00:23:45 -0400 Subject: [PATCH 17/19] Made error messages more human readable based on feedback received on pull #32816 by jorisvandenbossche. --- pandas/core/generic.py | 2 +- pandas/core/ops/__init__.py | 3 ++- pandas/tests/frame/test_apply.py | 2 +- pandas/tests/frame/test_operators.py | 8 ++++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 6b0f7de11a3e7..e694b1452083f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -354,7 +354,7 @@ def _get_axis_number(cls, axis): return cls._AXIS_NUMBERS[axis] except KeyError: pass - raise ValueError(f"No axis named {axis} for object type {cls}") + raise ValueError(f"No axis named {axis} for object type {cls.__name__}") @classmethod def _get_axis_name(cls, axis): diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 3153a9ac28c10..954f99cda4211 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -685,7 +685,8 @@ def to_series(right): elif right.ndim > 2: raise ValueError( - f"Unable to coerce to Series/DataFrame, dim must be <= 2: {right.shape}" + "Unable to coerce to Series/DataFrame, " + f"dimension must be <= 2: {right.shape}" ) elif is_list_like(right) and not isinstance(right, (ABCSeries, ABCDataFrame)): diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 8a2e7a2c5882e..6dee4424f1cec 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -49,7 +49,7 @@ def test_apply(self, float_frame): # invalid axis df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "a", "c"]) - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.apply(lambda x: x, 2) diff --git a/pandas/tests/frame/test_operators.py b/pandas/tests/frame/test_operators.py index 2d910ae9529a5..7549023ce6d2a 100644 --- a/pandas/tests/frame/test_operators.py +++ b/pandas/tests/frame/test_operators.py @@ -574,7 +574,11 @@ def test_comp(func): result = func(df1, df2) tm.assert_numpy_array_equal(result.values, func(df1.values, df2.values)) - with pytest.raises(ValueError, match="dim must be <= 2"): + msg = ( + "Unable to coerce to Series/DataFrame, " + "dimension must be <= 2: (30, 4, 1, 1, 1)" + ) + with pytest.raises(ValueError, match=re.escape(msg)): func(df1, ndim_5) result2 = func(simple_frame, row) @@ -892,7 +896,7 @@ def test_alignment_non_pandas(self): val = np.zeros((3, 3, 3)) msg = re.escape( - "Unable to coerce to Series/DataFrame, dim must be <= 2: (3, 3, 3)" + "Unable to coerce to Series/DataFrame, dimension must be <= 2: (3, 3, 3)" ) with pytest.raises(ValueError, match=msg): align(df, val, "index") From 483009258f1aef0061d04b4e9c42addc4ddd7686 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Fri, 20 Mar 2020 01:07:01 -0400 Subject: [PATCH 18/19] Fixed the assertions in other tests that relied on the previous wording of the error messages. --- pandas/tests/frame/methods/test_quantile.py | 7 ++----- pandas/tests/frame/methods/test_sort_values.py | 2 +- pandas/tests/frame/test_analytics.py | 4 ++-- pandas/tests/frame/test_api.py | 5 +---- pandas/tests/frame/test_axis_select_reindex.py | 2 +- pandas/tests/frame/test_missing.py | 2 +- pandas/tests/generic/test_generic.py | 4 ++-- pandas/tests/series/methods/test_between_time.py | 2 +- pandas/tests/series/methods/test_rank.py | 4 +--- pandas/tests/series/methods/test_sort_index.py | 2 +- pandas/tests/series/test_missing.py | 2 +- 11 files changed, 14 insertions(+), 22 deletions(-) diff --git a/pandas/tests/frame/methods/test_quantile.py b/pandas/tests/frame/methods/test_quantile.py index 9c52e8ec5620f..0eec30cbc5c67 100644 --- a/pandas/tests/frame/methods/test_quantile.py +++ b/pandas/tests/frame/methods/test_quantile.py @@ -100,13 +100,10 @@ def test_quantile_axis_parameter(self): result = df.quantile(0.5, axis="columns") tm.assert_series_equal(result, expected) - msg = "No axis named -1 for object type " + msg = "No axis named -1 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.quantile(0.1, axis=-1) - msg = ( - "No axis named column for object type " - "" - ) + msg = "No axis named column for object type DataFrame" with pytest.raises(ValueError, match=msg): df.quantile(0.1, axis="column") diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index 5a25d1c2c0894..3d3bb98f80ac5 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -43,7 +43,7 @@ def test_sort_values(self): sorted_df = frame.sort_values(by=["B", "A"], ascending=[True, False]) tm.assert_frame_equal(sorted_df, expected) - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): frame.sort_values(by=["A", "B"], axis=2, inplace=True) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 3964e790c7c12..3a7df29ae9091 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -919,7 +919,7 @@ def test_idxmin(self, float_frame, int_frame): expected = df.apply(Series.idxmin, axis=axis, skipna=skipna) tm.assert_series_equal(result, expected) - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): frame.idxmin(axis=2) @@ -934,7 +934,7 @@ def test_idxmax(self, float_frame, int_frame): expected = df.apply(Series.idxmax, axis=axis, skipna=skipna) tm.assert_series_equal(result, expected) - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): frame.idxmax(axis=2) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 940a76601b75e..91627b46c2fee 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -371,10 +371,7 @@ def test_swapaxes(self): tm.assert_frame_equal(df.T, df.swapaxes(0, 1)) tm.assert_frame_equal(df.T, df.swapaxes(1, 0)) tm.assert_frame_equal(df, df.swapaxes(0, 0)) - msg = ( - "No axis named 2 for object type " - r"" - ) + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.swapaxes(2, 5) diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index 4070984c84fec..ea21359c2f75c 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -618,7 +618,7 @@ def test_align_float(self, float_frame): tm.assert_index_equal(bf.index, Index([])) # Try to align DataFrame to Series along bad axis - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): float_frame.align(af.iloc[0, :3], join="inner", axis=2) diff --git a/pandas/tests/frame/test_missing.py b/pandas/tests/frame/test_missing.py index 196df8ba00476..470da25a922a1 100644 --- a/pandas/tests/frame/test_missing.py +++ b/pandas/tests/frame/test_missing.py @@ -114,7 +114,7 @@ def test_dropna(self): tm.assert_frame_equal(dropped, expected) # bad input - msg = "No axis named 3 for object type " + msg = "No axis named 3 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.dropna(axis=3) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 6999dea6adfa3..f6005a0f839a3 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -692,10 +692,10 @@ def test_squeeze(self): tm.assert_series_equal(df.squeeze(axis=1), df.iloc[:, 0]) tm.assert_series_equal(df.squeeze(axis="columns"), df.iloc[:, 0]) assert df.squeeze() == df.iloc[0, 0] - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.squeeze(axis=2) - msg = "No axis named x for object type " + msg = "No axis named x for object type DataFrame" with pytest.raises(ValueError, match=msg): df.squeeze(axis="x") diff --git a/pandas/tests/series/methods/test_between_time.py b/pandas/tests/series/methods/test_between_time.py index 3fa26afe77a1d..e9d2f8e6f1637 100644 --- a/pandas/tests/series/methods/test_between_time.py +++ b/pandas/tests/series/methods/test_between_time.py @@ -139,6 +139,6 @@ def test_between_time_axis(self): assert len(ts.between_time(stime, etime)) == expected_length assert len(ts.between_time(stime, etime, axis=0)) == expected_length - msg = "No axis named 1 for object type " + msg = "No axis named 1 for object type Series" with pytest.raises(ValueError, match=msg): ts.between_time(stime, etime, axis=1) diff --git a/pandas/tests/series/methods/test_rank.py b/pandas/tests/series/methods/test_rank.py index 3d4688c8274f9..caaffb7d5b61f 100644 --- a/pandas/tests/series/methods/test_rank.py +++ b/pandas/tests/series/methods/test_rank.py @@ -202,9 +202,7 @@ def test_rank_categorical(self): def test_rank_signature(self): s = Series([0, 1]) s.rank(method="average") - msg = ( - "No axis named average for object type " - ) + msg = "No axis named average for object type Series" with pytest.raises(ValueError, match=msg): s.rank("average") diff --git a/pandas/tests/series/methods/test_sort_index.py b/pandas/tests/series/methods/test_sort_index.py index 6fa4eeaee34c0..d4ebc9062a0c9 100644 --- a/pandas/tests/series/methods/test_sort_index.py +++ b/pandas/tests/series/methods/test_sort_index.py @@ -30,7 +30,7 @@ def test_sort_index(self, datetime_series): sorted_series = random_order.sort_index(axis=0) tm.assert_series_equal(sorted_series, datetime_series) - msg = "No axis named 1 for object type " + msg = "No axis named 1 for object type Series" with pytest.raises(ValueError, match=msg): random_order.sort_values(axis=1) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index bac005465034f..15f1bc8941d47 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -823,7 +823,7 @@ def test_dropna_empty(self): assert len(s) == 0 # invalid axis - msg = "No axis named 1 for object type " + msg = "No axis named 1 for object type Series" with pytest.raises(ValueError, match=msg): s.dropna(axis=1) From 531ae1ca3c48e032583b3908a95201452e47dc2e Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Fri, 20 Mar 2020 20:12:21 -0400 Subject: [PATCH 19/19] Removed error message check for test_categorical_replace_with_dict in test_replace.py based on feedback received from jorisvandenbossche. --- pandas/tests/frame/methods/test_replace.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 34940b50a7f22..ee89562261b19 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1308,8 +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 = 'Attributes of DataFrame.iloc[:, 0] (column name="a") are different' - with pytest.raises(AssertionError, match=re.escape(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)