diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 88662a4fabed8..356c54e80f0ca 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -685,7 +685,7 @@ def get_median(x): values = values.astype("f8") except ValueError as err: # e.g. "could not convert string to float: 'a'" - raise TypeError from err + raise TypeError(str(err)) from err if mask is not None: values[mask] = np.nan diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 8c2297699807d..473e836be8e50 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -526,9 +526,17 @@ def test_numpy_minmax_period(self): def test_min_max_categorical(self): ci = pd.CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False) - with pytest.raises(TypeError): + msg = ( + r"Categorical is not ordered for operation min\n" + r"you can use .as_ordered\(\) to change the Categorical to an ordered one\n" + ) + with pytest.raises(TypeError, match=msg): ci.min() - with pytest.raises(TypeError): + msg = ( + r"Categorical is not ordered for operation max\n" + r"you can use .as_ordered\(\) to change the Categorical to an ordered one\n" + ) + with pytest.raises(TypeError, match=msg): ci.max() ci = pd.CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=True) @@ -880,16 +888,20 @@ def test_all_any_params(self): tm.assert_series_equal(s.all(level=0), Series([False, True, False])) tm.assert_series_equal(s.any(level=0), Series([False, True, True])) - # bool_only is not implemented with level option. - with pytest.raises(NotImplementedError): + msg = "Option bool_only is not implemented with option level" + with pytest.raises(NotImplementedError, match=msg): s.any(bool_only=True, level=0) - with pytest.raises(NotImplementedError): + with pytest.raises(NotImplementedError, match=msg): s.all(bool_only=True, level=0) # bool_only is not implemented alone. - with pytest.raises(NotImplementedError): + # TODO GH38810 change this error message to: + # "Series.any does not implement bool_only" + msg = "Series.any does not implement numeric_only" + with pytest.raises(NotImplementedError, match=msg): s.any(bool_only=True) - with pytest.raises(NotImplementedError): + msg = "Series.all does not implement numeric_only." + with pytest.raises(NotImplementedError, match=msg): s.all(bool_only=True) def test_all_any_boolean(self): @@ -980,13 +992,21 @@ def test_assert_idxminmax_raises(self, test_input, error_type): """ Cases where ``Series.argmax`` and related should raise an exception """ - with pytest.raises(error_type): + msg = ( + "reduction operation 'argmin' not allowed for this dtype|" + "attempt to get argmin of an empty sequence" + ) + with pytest.raises(error_type, match=msg): test_input.idxmin() - with pytest.raises(error_type): + with pytest.raises(error_type, match=msg): test_input.idxmin(skipna=False) - with pytest.raises(error_type): + msg = ( + "reduction operation 'argmax' not allowed for this dtype|" + "attempt to get argmax of an empty sequence" + ) + with pytest.raises(error_type, match=msg): test_input.idxmax() - with pytest.raises(error_type): + with pytest.raises(error_type, match=msg): test_input.idxmax(skipna=False) def test_idxminmax_with_inf(self): diff --git a/pandas/tests/reductions/test_stat_reductions.py b/pandas/tests/reductions/test_stat_reductions.py index 67e871f8b67c2..ab13893901104 100644 --- a/pandas/tests/reductions/test_stat_reductions.py +++ b/pandas/tests/reductions/test_stat_reductions.py @@ -98,7 +98,8 @@ def _check_stat_op( # mean, idxmax, idxmin, min, and max are valid for dates if name not in ["max", "min", "mean", "median", "std"]: ds = Series(pd.date_range("1/1/2001", periods=10)) - with pytest.raises(TypeError): + msg = f"'DatetimeArray' does not implement reduction '{name}'" + with pytest.raises(TypeError, match=msg): f(ds) # skipna or no @@ -134,11 +135,12 @@ def _check_stat_op( # check on string data if name not in ["sum", "min", "max"]: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=None): f(Series(list("abc"))) # Invalid axis. - with pytest.raises(ValueError): + msg = "No axis named 1 for object type Series" + with pytest.raises(ValueError, match=msg): f(string_series_, axis=1) # Unimplemented numeric_only parameter.