Skip to content

Commit 888e56a

Browse files
simonjayhawkinsjreback
authored andcommitted
TST: Add message checks to tests/arrays/sparse/ (#30244)
1 parent e883fb9 commit 888e56a

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

pandas/tests/arrays/sparse/test_array.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,12 @@ def test_take_filling(self):
307307
with pytest.raises(ValueError, match=msg):
308308
sparse.take(np.array([1, 0, -5]), allow_fill=True)
309309

310-
with pytest.raises(IndexError):
310+
msg = "out of bounds value in 'indices'"
311+
with pytest.raises(IndexError, match=msg):
311312
sparse.take(np.array([1, -6]))
312-
with pytest.raises(IndexError):
313+
with pytest.raises(IndexError, match=msg):
313314
sparse.take(np.array([1, 5]))
314-
with pytest.raises(IndexError):
315+
with pytest.raises(IndexError, match=msg):
315316
sparse.take(np.array([1, 5]), allow_fill=True)
316317

317318
def test_take_filling_fill_value(self):
@@ -340,11 +341,12 @@ def test_take_filling_fill_value(self):
340341
with pytest.raises(ValueError, match=msg):
341342
sparse.take(np.array([1, 0, -5]), allow_fill=True)
342343

343-
with pytest.raises(IndexError):
344+
msg = "out of bounds value in 'indices'"
345+
with pytest.raises(IndexError, match=msg):
344346
sparse.take(np.array([1, -6]))
345-
with pytest.raises(IndexError):
347+
with pytest.raises(IndexError, match=msg):
346348
sparse.take(np.array([1, 5]))
347-
with pytest.raises(IndexError):
349+
with pytest.raises(IndexError, match=msg):
348350
sparse.take(np.array([1, 5]), fill_value=True)
349351

350352
def test_take_filling_all_nan(self):
@@ -358,11 +360,12 @@ def test_take_filling_all_nan(self):
358360
expected = SparseArray([np.nan, np.nan, np.nan], kind="block")
359361
tm.assert_sp_array_equal(result, expected)
360362

361-
with pytest.raises(IndexError):
363+
msg = "out of bounds value in 'indices'"
364+
with pytest.raises(IndexError, match=msg):
362365
sparse.take(np.array([1, -6]))
363-
with pytest.raises(IndexError):
366+
with pytest.raises(IndexError, match=msg):
364367
sparse.take(np.array([1, 5]))
365-
with pytest.raises(IndexError):
368+
with pytest.raises(IndexError, match=msg):
366369
sparse.take(np.array([1, 5]), fill_value=True)
367370

368371
def test_set_item(self):
@@ -670,10 +673,11 @@ def test_getslice_tuple(self):
670673
exp = SparseArray(dense[4:,], fill_value=0) # noqa: E231
671674
tm.assert_sp_array_equal(res, exp)
672675

673-
with pytest.raises(IndexError):
676+
msg = "too many indices for array"
677+
with pytest.raises(IndexError, match=msg):
674678
sparse[4:, :]
675679

676-
with pytest.raises(IndexError):
680+
with pytest.raises(IndexError, match=msg):
677681
# check numpy compat
678682
dense[4:, :]
679683

pandas/tests/arrays/sparse/test_dtype.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -80,7 +82,9 @@ def test_not_equal(a, b):
8082

8183

8284
def test_construct_from_string_raises():
83-
with pytest.raises(TypeError):
85+
with pytest.raises(
86+
TypeError, match="Could not construct SparseDtype from 'not a dtype'"
87+
):
8488
SparseDtype.construct_from_string("not a dtype")
8589

8690

@@ -175,9 +179,20 @@ def test_update_dtype(original, dtype, expected):
175179

176180

177181
@pytest.mark.parametrize(
178-
"original, dtype",
179-
[(SparseDtype(float, np.nan), int), (SparseDtype(str, "abc"), int)],
182+
"original, dtype, expected_error_msg",
183+
[
184+
(
185+
SparseDtype(float, np.nan),
186+
int,
187+
re.escape("Cannot convert non-finite values (NA or inf) to integer"),
188+
),
189+
(
190+
SparseDtype(str, "abc"),
191+
int,
192+
re.escape("invalid literal for int() with base 10: 'abc'"),
193+
),
194+
],
180195
)
181-
def test_update_dtype_raises(original, dtype):
182-
with pytest.raises(ValueError):
196+
def test_update_dtype_raises(original, dtype, expected_error_msg):
197+
with pytest.raises(ValueError, match=expected_error_msg):
183198
original.update_dtype(dtype)

0 commit comments

Comments
 (0)