Skip to content

Commit 24bb514

Browse files
authored
Check error message for raised exception (#33103)
1 parent 39e3412 commit 24bb514

File tree

7 files changed

+19
-13
lines changed

7 files changed

+19
-13
lines changed

pandas/core/arrays/period.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ def __init__(self, values, freq=None, dtype=None, copy=False):
166166
@classmethod
167167
def _simple_new(cls, values: np.ndarray, freq=None, **kwargs) -> "PeriodArray":
168168
# alias for PeriodArray.__init__
169-
assert isinstance(values, np.ndarray) and values.dtype == "i8"
169+
assertion_msg = "Should be numpy array of type i8"
170+
assert isinstance(values, np.ndarray) and values.dtype == "i8", assertion_msg
170171
return cls(values, freq=freq, **kwargs)
171172

172173
@classmethod

pandas/tests/indexes/categorical/test_category.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def test_delete(self):
192192
expected = CategoricalIndex(list("aabbc"), categories=categories)
193193
tm.assert_index_equal(result, expected, exact=True)
194194

195-
with pytest.raises((IndexError, ValueError)):
195+
with tm.external_error_raised((IndexError, ValueError)):
196196
# Either depending on NumPy version
197197
ci.delete(10)
198198

pandas/tests/indexes/period/test_constructors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ def test_constructor_simple_new(self):
327327
result = idx._simple_new(idx._data, name="p")
328328
tm.assert_index_equal(result, idx)
329329

330-
with pytest.raises(AssertionError):
330+
msg = "Should be numpy array of type i8"
331+
with pytest.raises(AssertionError, match=msg):
331332
# Need ndarray, not Int64Index
332333
type(idx._data)._simple_new(idx.astype("i8"), freq=idx.freq)
333334

pandas/tests/indexes/period/test_join.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ def test_join_does_not_recur(self):
3939
def test_join_mismatched_freq_raises(self):
4040
index = period_range("1/1/2000", "1/20/2000", freq="D")
4141
index3 = period_range("1/1/2000", "1/20/2000", freq="2D")
42-
with pytest.raises(IncompatibleFrequency):
42+
msg = r".*Input has different freq=2D from PeriodIndex\(freq=D\)"
43+
with pytest.raises(IncompatibleFrequency, match=msg):
4344
index.join(index3)

pandas/tests/indexes/period/test_partial_slicing.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def test_range_slice_day(self):
5959
didx = pd.date_range(start="2013/01/01", freq="D", periods=400)
6060
pidx = period_range(start="2013/01/01", freq="D", periods=400)
6161

62+
msg = "slice indices must be integers or None or have an __index__ method"
6263
for idx in [didx, pidx]:
6364
# slices against index should raise IndexError
6465
values = [
@@ -69,7 +70,7 @@ def test_range_slice_day(self):
6970
"2013/02/01 09:00",
7071
]
7172
for v in values:
72-
with pytest.raises(TypeError):
73+
with pytest.raises(TypeError, match=msg):
7374
idx[v:]
7475

7576
s = Series(np.random.rand(len(idx)), index=idx)
@@ -81,13 +82,14 @@ def test_range_slice_day(self):
8182

8283
invalid = ["2013/02/01 9H", "2013/02/01 09:00"]
8384
for v in invalid:
84-
with pytest.raises(TypeError):
85+
with pytest.raises(TypeError, match=msg):
8586
idx[v:]
8687

8788
def test_range_slice_seconds(self):
8889
# GH#6716
8990
didx = pd.date_range(start="2013/01/01 09:00:00", freq="S", periods=4000)
9091
pidx = period_range(start="2013/01/01 09:00:00", freq="S", periods=4000)
92+
msg = "slice indices must be integers or None or have an __index__ method"
9193

9294
for idx in [didx, pidx]:
9395
# slices against index should raise IndexError
@@ -99,7 +101,7 @@ def test_range_slice_seconds(self):
99101
"2013/02/01 09:00",
100102
]
101103
for v in values:
102-
with pytest.raises(TypeError):
104+
with pytest.raises(TypeError, match=msg):
103105
idx[v:]
104106

105107
s = Series(np.random.rand(len(idx)), index=idx)

pandas/tests/indexes/period/test_setops.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ def test_union_misc(self, sort):
148148
# raise if different frequencies
149149
index = period_range("1/1/2000", "1/20/2000", freq="D")
150150
index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED")
151-
with pytest.raises(IncompatibleFrequency):
151+
msg = r"Input has different freq=W-WED from PeriodIndex\(freq=D\)"
152+
with pytest.raises(IncompatibleFrequency, match=msg):
152153
index.union(index2, sort=sort)
153154

154155
# TODO: belongs elsewhere
@@ -180,11 +181,13 @@ def test_intersection(self, sort):
180181
# raise if different frequencies
181182
index = period_range("1/1/2000", "1/20/2000", freq="D")
182183
index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED")
183-
with pytest.raises(IncompatibleFrequency):
184+
msg = r"Input has different freq=W-WED from PeriodIndex\(freq=D\)"
185+
with pytest.raises(IncompatibleFrequency, match=msg):
184186
index.intersection(index2, sort=sort)
185187

186188
index3 = period_range("1/1/2000", "1/20/2000", freq="2D")
187-
with pytest.raises(IncompatibleFrequency):
189+
msg = r"Input has different freq=2D from PeriodIndex\(freq=D\)"
190+
with pytest.raises(IncompatibleFrequency, match=msg):
188191
index.intersection(index3, sort=sort)
189192

190193
def test_intersection_cases(self, sort):

pandas/tests/indexes/timedeltas/test_delete.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import pytest
2-
31
from pandas import TimedeltaIndex, timedelta_range
42
import pandas._testing as tm
53

@@ -30,7 +28,7 @@ def test_delete(self):
3028
assert result.name == expected.name
3129
assert result.freq == expected.freq
3230

33-
with pytest.raises((IndexError, ValueError)):
31+
with tm.external_error_raised((IndexError, ValueError)):
3432
# either depending on numpy version
3533
idx.delete(5)
3634

0 commit comments

Comments
 (0)