From 3f8b82a89282b8334ef1cd29e112d1d97dfa90c8 Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Thu, 30 Apr 2020 15:51:15 -0400 Subject: [PATCH 01/11] fixed tests/series/methods/test_asof.py --- pandas/tests/series/methods/test_asof.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/series/methods/test_asof.py b/pandas/tests/series/methods/test_asof.py index ad5a2de6eabac..19caf4eccf748 100644 --- a/pandas/tests/series/methods/test_asof.py +++ b/pandas/tests/series/methods/test_asof.py @@ -148,14 +148,14 @@ def test_errors(self): # non-monotonic assert not s.index.is_monotonic - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="requires a sorted index"): s.asof(s.index[0]) # subset with Series N = 10 rng = date_range("1/1/1990", periods=N, freq="53s") s = Series(np.random.randn(N), index=rng) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="not valid for Series"): s.asof(s.index[0], subset="foo") def test_all_nans(self): From 4e025a80c06a531056d6f8e2d729e5434d2f2107 Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Thu, 30 Apr 2020 15:55:49 -0400 Subject: [PATCH 02/11] fixed tests/series/methods/test_droplevel.py --- pandas/tests/series/methods/test_droplevel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/methods/test_droplevel.py b/pandas/tests/series/methods/test_droplevel.py index 435eb5751de4b..6bee3561cb4e3 100644 --- a/pandas/tests/series/methods/test_droplevel.py +++ b/pandas/tests/series/methods/test_droplevel.py @@ -15,5 +15,5 @@ def test_droplevel(self): result = ser.droplevel("b", axis="index") tm.assert_series_equal(result, expected) # test that droplevel raises ValueError on axis != 0 - with pytest.raises(ValueError): + with pytest.raises(ValueError,match="No axis named columns"): ser.droplevel(1, axis="columns") From 96b8b304eb068ec87b453906c43e9c882bd4e36c Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Thu, 30 Apr 2020 16:24:01 -0400 Subject: [PATCH 03/11] fixed tests/series/methods/test_tz_localize.py --- pandas/tests/series/methods/test_tz_localize.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/series/methods/test_tz_localize.py b/pandas/tests/series/methods/test_tz_localize.py index 44c55edf77c0a..532b8d16f0d5c 100644 --- a/pandas/tests/series/methods/test_tz_localize.py +++ b/pandas/tests/series/methods/test_tz_localize.py @@ -35,7 +35,7 @@ def test_series_tz_localize_ambiguous_bool(self): expected0 = Series([expected0]) expected1 = Series([expected1]) - with pytest.raises(pytz.AmbiguousTimeError): + with tm.external_error_raised(pytz.AmbiguousTimeError): ser.dt.tz_localize("US/Central") result = ser.dt.tz_localize("US/Central", ambiguous=True) @@ -66,10 +66,10 @@ def test_series_tz_localize_nonexistent(self, tz, method, exp): dti = date_range(start="2015-03-29 02:00:00", periods=n, freq="min") s = Series(1, dti) if method == "raise": - with pytest.raises(pytz.NonExistentTimeError): + with tm.external_error_raised(pytz.NonExistentTimeError): s.tz_localize(tz, nonexistent=method) elif exp == "invalid": - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="argument must be one of"): dti.tz_localize(tz, nonexistent=method) else: result = s.tz_localize(tz, nonexistent=method) From 6d6cdb19c2191bc7351b3d601520c5ca13deffc6 Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Thu, 30 Apr 2020 17:04:21 -0400 Subject: [PATCH 04/11] fixed tests/series/test_apply.py --- pandas/tests/series/test_apply.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/tests/series/test_apply.py b/pandas/tests/series/test_apply.py index 0661828814888..54cf307a6ee66 100644 --- a/pandas/tests/series/test_apply.py +++ b/pandas/tests/series/test_apply.py @@ -248,18 +248,21 @@ def test_transform(self, string_series): def test_transform_and_agg_error(self, string_series): # we are trying to transform with an aggregator - with pytest.raises(ValueError): + msg = "transforms cannot produce aggregated results" + with pytest.raises(ValueError, match=msg): string_series.transform(["min", "max"]) - with pytest.raises(ValueError): + msg = "cannot combine transform and aggregation" + with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): string_series.agg(["sqrt", "max"]) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): string_series.transform(["sqrt", "max"]) - with pytest.raises(ValueError): + msg = "cannot perform both aggregation and transformation" + with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): string_series.agg({"foo": np.sqrt, "bar": "sum"}) From 7aac1ad94a2db4655235d7a27c1dd4f25ba13beb Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Thu, 30 Apr 2020 18:40:40 -0400 Subject: [PATCH 05/11] fixed tests/series/test_arithmetic.py --- pandas/tests/series/test_arithmetic.py | 33 +++++++++++++++----------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index c7a04843b8296..5c8a0d224c4f9 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -335,12 +335,13 @@ class TestSeriesComparison: def test_comparison_different_length(self): a = Series(["a", "b", "c"]) b = Series(["b", "a"]) - with pytest.raises(ValueError): + msg = "only compare identically-labeled Series" + with pytest.raises(ValueError, match=msg): a < b a = Series([1, 2]) b = Series([2, 3, 4]) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): a == b @pytest.mark.parametrize("opname", ["eq", "ne", "gt", "lt", "ge", "le"]) @@ -474,23 +475,25 @@ def test_categorical_comparisons(self): assert (~(f == a) == (f != a)).all() # non-equality is not comparable - with pytest.raises(TypeError): + msg = "can only compare equality or not" + with pytest.raises(TypeError, match=msg): a < b - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): b < a - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): a > b - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): b > a def test_unequal_categorical_comparison_raises_type_error(self): # unequal comparison should raise for unordered cats cat = Series(Categorical(list("abc"))) - with pytest.raises(TypeError): + msg = "can only compare equality or not" + with pytest.raises(TypeError, match=msg): cat > "b" cat = Series(Categorical(list("abc"), ordered=False)) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): cat > "b" # https://github.com/pandas-dev/pandas/issues/9836#issuecomment-92123057 @@ -498,13 +501,14 @@ def test_unequal_categorical_comparison_raises_type_error(self): # for unequal comps, but not for equal/not equal cat = Series(Categorical(list("abc"), ordered=True)) - with pytest.raises(TypeError): + msg = "Cannot compare a Categorical for op.+with a scalar" + with pytest.raises(TypeError, match=msg): cat < "d" - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): cat > "d" - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): "d" < cat - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): "d" > cat tm.assert_series_equal(cat == "d", Series([False, False, False])) @@ -668,10 +672,11 @@ def test_series_add_aware_naive_raises(self): ser_utc = ser.tz_localize("utc") - with pytest.raises(Exception): + msg = "Cannot join tz-naive with tz-aware DatetimeIndex" + with pytest.raises(Exception, match=msg): ser + ser_utc - with pytest.raises(Exception): + with pytest.raises(Exception, match=msg): ser_utc + ser def test_datetime_understood(self): From d5ac9412458e67c98aa32c123bb936dc126b66b4 Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Thu, 30 Apr 2020 19:03:23 -0400 Subject: [PATCH 06/11] fixed tests/series/test_datetime_values.py --- pandas/tests/series/test_datetime_values.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index 0d7fd0529dc1f..16c3376b1a8ca 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -244,8 +244,9 @@ def get_dir(s): s.dt.hour = 5 # trying to set a copy + msg = "modifications to a property of a datetimelike.+not supported.+" with pd.option_context("chained_assignment", "raise"): - with pytest.raises(com.SettingWithCopyError): + with pytest.raises(com.SettingWithCopyError, match=msg): s.dt.hour[0] = 5 @pytest.mark.parametrize( @@ -311,7 +312,7 @@ def test_dt_round_tz_ambiguous(self, method): tm.assert_series_equal(result, expected) # raise - with pytest.raises(pytz.AmbiguousTimeError): + with tm.external_error_raised(pytz.AmbiguousTimeError): getattr(df1.date.dt, method)("H", ambiguous="raise") @pytest.mark.parametrize( From abf8dedc1b029e659fd186b19779752478a90f8c Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Thu, 30 Apr 2020 19:32:33 -0400 Subject: [PATCH 07/11] fixed test/series/test_operators.py --- pandas/tests/series/test_operators.py | 29 ++++++++++++++++----------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index ba1b3e9d0ca8e..8bafad7572bae 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -121,23 +121,25 @@ def test_logical_operators_int_dtype_with_float(self): # GH#9016: support bitwise op for integer types s_0123 = Series(range(4), dtype="int64") - with pytest.raises(TypeError): + msg = "Cannot perform.+with a dtyped.+array and scalar of type.+" + with pytest.raises(TypeError, match=msg): s_0123 & np.NaN - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): s_0123 & 3.14 - with pytest.raises(TypeError): + msg = "unsupported operand type.+for &:.+" + with pytest.raises(TypeError, match=msg): s_0123 & [0.1, 4, 3.14, 2] - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): s_0123 & np.array([0.1, 4, 3.14, 2]) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): s_0123 & Series([0.1, 4, -3.14, 2]) def test_logical_operators_int_dtype_with_str(self): s_1111 = Series([1] * 4, dtype="int8") - - with pytest.raises(TypeError): + msg = "Cannot perform 'and_' with a dtyped.+array and scalar of type.+" + with pytest.raises(TypeError, match=msg): s_1111 & "a" - with pytest.raises(TypeError): + with pytest.raises(TypeError, match="unsupported operand.+for &.+"): s_1111 & ["a", "b", "c", "d"] def test_logical_operators_int_dtype_with_bool(self): @@ -254,8 +256,9 @@ def test_logical_operators_int_dtype_with_bool_dtype_and_reindex(self): def test_scalar_na_logical_ops_corners(self): s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10]) - - with pytest.raises(TypeError): + + msg = "Cannot perform.+with a dtyped.+array and scalar of type.+" + with pytest.raises(TypeError, match=msg): s & datetime(2005, 1, 1) s = Series([2, 3, 4, 5, 6, 7, 8, 9, datetime(2005, 1, 1)]) @@ -451,8 +454,9 @@ def test_logical_ops_label_based(self): expected = Series([True, True, True], index=index) tm.assert_series_equal(result, expected) + msg = "Cannot perform.+with a dtyped.+array and scalar of type.+" for v in [np.nan, "foo"]: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): t | v for v in [False, 0]: @@ -469,8 +473,9 @@ def test_logical_ops_label_based(self): result = Series([True, False, True], index=index) & v expected = Series([False, False, False], index=index) tm.assert_series_equal(result, expected) + msg = "Cannot perform.+with a dtyped.+array and scalar of type.+" for v in [np.nan]: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): t & v def test_logical_ops_df_compat(self): From f7fcf1434080b97e13c88157cac43db65d6658ed Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Thu, 30 Apr 2020 21:38:41 -0400 Subject: [PATCH 08/11] fixed tests/tools/test_to_time.py --- pandas/tests/tools/test_to_time.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/tools/test_to_time.py b/pandas/tests/tools/test_to_time.py index 17ab492aca725..937570d89fb77 100644 --- a/pandas/tests/tools/test_to_time.py +++ b/pandas/tests/tools/test_to_time.py @@ -46,7 +46,8 @@ def test_parsers_time(self): res = to_time(arg, format="%I:%M%p", errors="ignore") tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_)) - with pytest.raises(ValueError): + msg = "Cannot convert.+to a time with given format" + with pytest.raises(ValueError, match=msg): to_time(arg, format="%I:%M%p", errors="raise") tm.assert_series_equal( From 58a783a72517b00981ed1e30a8c917991c5d1c73 Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Fri, 1 May 2020 11:27:40 -0400 Subject: [PATCH 09/11] final commit before style checks --- pandas/tests/series/methods/test_droplevel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/methods/test_droplevel.py b/pandas/tests/series/methods/test_droplevel.py index 6bee3561cb4e3..449ddd1cd0e49 100644 --- a/pandas/tests/series/methods/test_droplevel.py +++ b/pandas/tests/series/methods/test_droplevel.py @@ -15,5 +15,5 @@ def test_droplevel(self): result = ser.droplevel("b", axis="index") tm.assert_series_equal(result, expected) # test that droplevel raises ValueError on axis != 0 - with pytest.raises(ValueError,match="No axis named columns"): + with pytest.raises(ValueError, match="No axis named columns"): ser.droplevel(1, axis="columns") From 8339fc8fd7964a2ad612b48a509f0875aad8bc35 Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Fri, 1 May 2020 11:43:07 -0400 Subject: [PATCH 10/11] TST: final commit fixing bare pytest raises --- pandas/tests/series/test_operators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index 8bafad7572bae..7c1384d66b1f8 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -256,8 +256,8 @@ def test_logical_operators_int_dtype_with_bool_dtype_and_reindex(self): def test_scalar_na_logical_ops_corners(self): s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10]) - - msg = "Cannot perform.+with a dtyped.+array and scalar of type.+" + + msg = "Cannot perform.+with a dtyped.+array and scalar of type" with pytest.raises(TypeError, match=msg): s & datetime(2005, 1, 1) From 0076be4badfa458904199ac9d169bcfdf81fea37 Mon Sep 17 00:00:00 2001 From: Andres McNeill Date: Fri, 1 May 2020 11:53:15 -0400 Subject: [PATCH 11/11] TST: fixed bare pytest raises --- pandas/tests/series/test_datetime_values.py | 2 +- pandas/tests/series/test_operators.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index 16c3376b1a8ca..8b1f58414e175 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -244,7 +244,7 @@ def get_dir(s): s.dt.hour = 5 # trying to set a copy - msg = "modifications to a property of a datetimelike.+not supported.+" + msg = "modifications to a property of a datetimelike.+not supported" with pd.option_context("chained_assignment", "raise"): with pytest.raises(com.SettingWithCopyError, match=msg): s.dt.hour[0] = 5 diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index 7c1384d66b1f8..e1c9682329271 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -121,12 +121,12 @@ def test_logical_operators_int_dtype_with_float(self): # GH#9016: support bitwise op for integer types s_0123 = Series(range(4), dtype="int64") - msg = "Cannot perform.+with a dtyped.+array and scalar of type.+" + msg = "Cannot perform.+with a dtyped.+array and scalar of type" with pytest.raises(TypeError, match=msg): s_0123 & np.NaN with pytest.raises(TypeError, match=msg): s_0123 & 3.14 - msg = "unsupported operand type.+for &:.+" + msg = "unsupported operand type.+for &:" with pytest.raises(TypeError, match=msg): s_0123 & [0.1, 4, 3.14, 2] with pytest.raises(TypeError, match=msg): @@ -136,10 +136,10 @@ def test_logical_operators_int_dtype_with_float(self): def test_logical_operators_int_dtype_with_str(self): s_1111 = Series([1] * 4, dtype="int8") - msg = "Cannot perform 'and_' with a dtyped.+array and scalar of type.+" + msg = "Cannot perform 'and_' with a dtyped.+array and scalar of type" with pytest.raises(TypeError, match=msg): s_1111 & "a" - with pytest.raises(TypeError, match="unsupported operand.+for &.+"): + with pytest.raises(TypeError, match="unsupported operand.+for &"): s_1111 & ["a", "b", "c", "d"] def test_logical_operators_int_dtype_with_bool(self): @@ -454,7 +454,7 @@ def test_logical_ops_label_based(self): expected = Series([True, True, True], index=index) tm.assert_series_equal(result, expected) - msg = "Cannot perform.+with a dtyped.+array and scalar of type.+" + msg = "Cannot perform.+with a dtyped.+array and scalar of type" for v in [np.nan, "foo"]: with pytest.raises(TypeError, match=msg): t | v @@ -473,7 +473,7 @@ def test_logical_ops_label_based(self): result = Series([True, False, True], index=index) & v expected = Series([False, False, False], index=index) tm.assert_series_equal(result, expected) - msg = "Cannot perform.+with a dtyped.+array and scalar of type.+" + msg = "Cannot perform.+with a dtyped.+array and scalar of type" for v in [np.nan]: with pytest.raises(TypeError, match=msg): t & v