Skip to content

Commit 81669e3

Browse files
authored
Test messages test integer (#31831)
1 parent d7996b9 commit 81669e3

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

pandas/tests/arrays/test_integer.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,26 +330,37 @@ def test_error(self, data, all_arithmetic_operators):
330330
opa = getattr(data, op)
331331

332332
# invalid scalars
333-
with pytest.raises(TypeError):
333+
msg = (
334+
r"(:?can only perform ops with numeric values)"
335+
r"|(:?IntegerArray cannot perform the operation mod)"
336+
)
337+
with pytest.raises(TypeError, match=msg):
334338
ops("foo")
335-
with pytest.raises(TypeError):
339+
with pytest.raises(TypeError, match=msg):
336340
ops(pd.Timestamp("20180101"))
337341

338342
# invalid array-likes
339-
with pytest.raises(TypeError):
343+
with pytest.raises(TypeError, match=msg):
340344
ops(pd.Series("foo", index=s.index))
341345

342346
if op != "__rpow__":
343347
# TODO(extension)
344348
# rpow with a datetimelike coerces the integer array incorrectly
345-
with pytest.raises(TypeError):
349+
msg = (
350+
r"(:?can only perform ops with numeric values)"
351+
r"|(:?cannot perform .* with this index type: DatetimeArray)"
352+
r"|(:?Addition/subtraction of integers and integer-arrays"
353+
r" with DatetimeArray is no longer supported. *)"
354+
)
355+
with pytest.raises(TypeError, match=msg):
346356
ops(pd.Series(pd.date_range("20180101", periods=len(s))))
347357

348358
# 2d
349359
result = opa(pd.DataFrame({"A": s}))
350360
assert result is NotImplemented
351361

352-
with pytest.raises(NotImplementedError):
362+
msg = r"can only perform ops with 1-d structures"
363+
with pytest.raises(NotImplementedError, match=msg):
353364
opa(np.arange(len(s)).reshape(-1, len(s)))
354365

355366
@pytest.mark.parametrize("zero, negative", [(0, False), (0.0, False), (-0.0, True)])
@@ -589,7 +600,8 @@ def test_astype(self, all_data):
589600

590601
# coerce to same numpy_dtype - mixed
591602
s = pd.Series(mixed)
592-
with pytest.raises(ValueError):
603+
msg = r"cannot convert to .*-dtype NumPy array with missing values.*"
604+
with pytest.raises(ValueError, match=msg):
593605
s.astype(all_data.dtype.numpy_dtype)
594606

595607
# coerce to object
@@ -730,16 +742,17 @@ def test_integer_array_constructor():
730742
expected = integer_array([1, 2, 3, np.nan], dtype="int64")
731743
tm.assert_extension_array_equal(result, expected)
732744

733-
with pytest.raises(TypeError):
745+
msg = r".* should be .* numpy array. Use the 'integer_array' function instead"
746+
with pytest.raises(TypeError, match=msg):
734747
IntegerArray(values.tolist(), mask)
735748

736-
with pytest.raises(TypeError):
749+
with pytest.raises(TypeError, match=msg):
737750
IntegerArray(values, mask.tolist())
738751

739-
with pytest.raises(TypeError):
752+
with pytest.raises(TypeError, match=msg):
740753
IntegerArray(values.astype(float), mask)
741-
742-
with pytest.raises(TypeError):
754+
msg = r"__init__\(\) missing 1 required positional argument: 'mask'"
755+
with pytest.raises(TypeError, match=msg):
743756
IntegerArray(values)
744757

745758

@@ -787,7 +800,11 @@ def test_integer_array_constructor_copy():
787800
)
788801
def test_to_integer_array_error(values):
789802
# error in converting existing arrays to IntegerArrays
790-
with pytest.raises(TypeError):
803+
msg = (
804+
r"(:?.* cannot be converted to an IntegerDtype)"
805+
r"|(:?values must be a 1D list-like)"
806+
)
807+
with pytest.raises(TypeError, match=msg):
791808
integer_array(values)
792809

793810

@@ -1002,7 +1019,8 @@ def test_ufuncs_binary_int(ufunc):
10021019
@pytest.mark.parametrize("values", [[0, 1], [0, None]])
10031020
def test_ufunc_reduce_raises(values):
10041021
a = integer_array(values)
1005-
with pytest.raises(NotImplementedError):
1022+
msg = r"The 'reduce' method is not supported."
1023+
with pytest.raises(NotImplementedError, match=msg):
10061024
np.add.reduce(a)
10071025

10081026

pandas/tests/arrays/test_period.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ def test_arrow_array(data, freq):
371371
assert result.equals(expected)
372372

373373
# unsupported conversions
374-
with pytest.raises(TypeError):
374+
msg = "Not supported to convert PeriodArray to 'double' type"
375+
with pytest.raises(TypeError, match=msg):
375376
pa.array(periods, type="float64")
376377

377378
with pytest.raises(TypeError, match="different 'freq'"):

0 commit comments

Comments
 (0)