diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 75a4f683e92d2..0aaf294378bf7 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -307,11 +307,12 @@ def test_take_filling(self): with pytest.raises(ValueError, match=msg): sparse.take(np.array([1, 0, -5]), allow_fill=True) - with pytest.raises(IndexError): + msg = "out of bounds value in 'indices'" + with pytest.raises(IndexError, match=msg): sparse.take(np.array([1, -6])) - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): sparse.take(np.array([1, 5])) - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): sparse.take(np.array([1, 5]), allow_fill=True) def test_take_filling_fill_value(self): @@ -340,11 +341,12 @@ def test_take_filling_fill_value(self): with pytest.raises(ValueError, match=msg): sparse.take(np.array([1, 0, -5]), allow_fill=True) - with pytest.raises(IndexError): + msg = "out of bounds value in 'indices'" + with pytest.raises(IndexError, match=msg): sparse.take(np.array([1, -6])) - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): sparse.take(np.array([1, 5])) - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): sparse.take(np.array([1, 5]), fill_value=True) def test_take_filling_all_nan(self): @@ -358,11 +360,12 @@ def test_take_filling_all_nan(self): expected = SparseArray([np.nan, np.nan, np.nan], kind="block") tm.assert_sp_array_equal(result, expected) - with pytest.raises(IndexError): + msg = "out of bounds value in 'indices'" + with pytest.raises(IndexError, match=msg): sparse.take(np.array([1, -6])) - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): sparse.take(np.array([1, 5])) - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): sparse.take(np.array([1, 5]), fill_value=True) def test_set_item(self): @@ -670,10 +673,11 @@ def test_getslice_tuple(self): exp = SparseArray(dense[4:,], fill_value=0) # noqa: E231 tm.assert_sp_array_equal(res, exp) - with pytest.raises(IndexError): + msg = "too many indices for array" + with pytest.raises(IndexError, match=msg): sparse[4:, :] - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): # check numpy compat dense[4:, :] diff --git a/pandas/tests/arrays/sparse/test_dtype.py b/pandas/tests/arrays/sparse/test_dtype.py index aa8d2afca11e6..3194498daf825 100644 --- a/pandas/tests/arrays/sparse/test_dtype.py +++ b/pandas/tests/arrays/sparse/test_dtype.py @@ -1,3 +1,5 @@ +import re + import numpy as np import pytest @@ -80,7 +82,9 @@ def test_not_equal(a, b): def test_construct_from_string_raises(): - with pytest.raises(TypeError): + with pytest.raises( + TypeError, match="Could not construct SparseDtype from 'not a dtype'" + ): SparseDtype.construct_from_string("not a dtype") @@ -175,9 +179,20 @@ def test_update_dtype(original, dtype, expected): @pytest.mark.parametrize( - "original, dtype", - [(SparseDtype(float, np.nan), int), (SparseDtype(str, "abc"), int)], + "original, dtype, expected_error_msg", + [ + ( + SparseDtype(float, np.nan), + int, + re.escape("Cannot convert non-finite values (NA or inf) to integer"), + ), + ( + SparseDtype(str, "abc"), + int, + re.escape("invalid literal for int() with base 10: 'abc'"), + ), + ], ) -def test_update_dtype_raises(original, dtype): - with pytest.raises(ValueError): +def test_update_dtype_raises(original, dtype, expected_error_msg): + with pytest.raises(ValueError, match=expected_error_msg): original.update_dtype(dtype)