diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index c24b0b5fa64b8..be9cc53d33d6f 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -166,7 +166,8 @@ def __init__(self, values, freq=None, dtype=None, copy=False): @classmethod def _simple_new(cls, values: np.ndarray, freq=None, **kwargs) -> "PeriodArray": # alias for PeriodArray.__init__ - assert isinstance(values, np.ndarray) and values.dtype == "i8" + assertion_msg = "Should be numpy array of type i8" + assert isinstance(values, np.ndarray) and values.dtype == "i8", assertion_msg return cls(values, freq=freq, **kwargs) @classmethod diff --git a/pandas/tests/indexes/categorical/test_category.py b/pandas/tests/indexes/categorical/test_category.py index 6e8e81230b2bb..954601ad423bf 100644 --- a/pandas/tests/indexes/categorical/test_category.py +++ b/pandas/tests/indexes/categorical/test_category.py @@ -192,7 +192,7 @@ def test_delete(self): expected = CategoricalIndex(list("aabbc"), categories=categories) tm.assert_index_equal(result, expected, exact=True) - with pytest.raises((IndexError, ValueError)): + with tm.external_error_raised((IndexError, ValueError)): # Either depending on NumPy version ci.delete(10) diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index cb2140d0b4025..4ec7ef64e2272 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -327,7 +327,8 @@ def test_constructor_simple_new(self): result = idx._simple_new(idx._data, name="p") tm.assert_index_equal(result, idx) - with pytest.raises(AssertionError): + msg = "Should be numpy array of type i8" + with pytest.raises(AssertionError, match=msg): # Need ndarray, not Int64Index type(idx._data)._simple_new(idx.astype("i8"), freq=idx.freq) diff --git a/pandas/tests/indexes/period/test_join.py b/pandas/tests/indexes/period/test_join.py index 9e3df0c32d6d5..8a68561dd5819 100644 --- a/pandas/tests/indexes/period/test_join.py +++ b/pandas/tests/indexes/period/test_join.py @@ -39,5 +39,6 @@ def test_join_does_not_recur(self): def test_join_mismatched_freq_raises(self): index = period_range("1/1/2000", "1/20/2000", freq="D") index3 = period_range("1/1/2000", "1/20/2000", freq="2D") - with pytest.raises(IncompatibleFrequency): + msg = r".*Input has different freq=2D from PeriodIndex\(freq=D\)" + with pytest.raises(IncompatibleFrequency, match=msg): index.join(index3) diff --git a/pandas/tests/indexes/period/test_partial_slicing.py b/pandas/tests/indexes/period/test_partial_slicing.py index 833901ea7ba22..c0597180184a6 100644 --- a/pandas/tests/indexes/period/test_partial_slicing.py +++ b/pandas/tests/indexes/period/test_partial_slicing.py @@ -59,6 +59,7 @@ def test_range_slice_day(self): didx = pd.date_range(start="2013/01/01", freq="D", periods=400) pidx = period_range(start="2013/01/01", freq="D", periods=400) + msg = "slice indices must be integers or None or have an __index__ method" for idx in [didx, pidx]: # slices against index should raise IndexError values = [ @@ -69,7 +70,7 @@ def test_range_slice_day(self): "2013/02/01 09:00", ] for v in values: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): idx[v:] s = Series(np.random.rand(len(idx)), index=idx) @@ -81,13 +82,14 @@ def test_range_slice_day(self): invalid = ["2013/02/01 9H", "2013/02/01 09:00"] for v in invalid: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): idx[v:] def test_range_slice_seconds(self): # GH#6716 didx = pd.date_range(start="2013/01/01 09:00:00", freq="S", periods=4000) pidx = period_range(start="2013/01/01 09:00:00", freq="S", periods=4000) + msg = "slice indices must be integers or None or have an __index__ method" for idx in [didx, pidx]: # slices against index should raise IndexError @@ -99,7 +101,7 @@ def test_range_slice_seconds(self): "2013/02/01 09:00", ] for v in values: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): idx[v:] s = Series(np.random.rand(len(idx)), index=idx) diff --git a/pandas/tests/indexes/period/test_setops.py b/pandas/tests/indexes/period/test_setops.py index 88f1687b8bb10..71b827d83b836 100644 --- a/pandas/tests/indexes/period/test_setops.py +++ b/pandas/tests/indexes/period/test_setops.py @@ -148,7 +148,8 @@ def test_union_misc(self, sort): # raise if different frequencies index = period_range("1/1/2000", "1/20/2000", freq="D") index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED") - with pytest.raises(IncompatibleFrequency): + msg = r"Input has different freq=W-WED from PeriodIndex\(freq=D\)" + with pytest.raises(IncompatibleFrequency, match=msg): index.union(index2, sort=sort) # TODO: belongs elsewhere @@ -180,11 +181,13 @@ def test_intersection(self, sort): # raise if different frequencies index = period_range("1/1/2000", "1/20/2000", freq="D") index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED") - with pytest.raises(IncompatibleFrequency): + msg = r"Input has different freq=W-WED from PeriodIndex\(freq=D\)" + with pytest.raises(IncompatibleFrequency, match=msg): index.intersection(index2, sort=sort) index3 = period_range("1/1/2000", "1/20/2000", freq="2D") - with pytest.raises(IncompatibleFrequency): + msg = r"Input has different freq=2D from PeriodIndex\(freq=D\)" + with pytest.raises(IncompatibleFrequency, match=msg): index.intersection(index3, sort=sort) def test_intersection_cases(self, sort): diff --git a/pandas/tests/indexes/timedeltas/test_delete.py b/pandas/tests/indexes/timedeltas/test_delete.py index 593ed7bb0a1ac..63f2b450aa818 100644 --- a/pandas/tests/indexes/timedeltas/test_delete.py +++ b/pandas/tests/indexes/timedeltas/test_delete.py @@ -1,5 +1,3 @@ -import pytest - from pandas import TimedeltaIndex, timedelta_range import pandas._testing as tm @@ -30,7 +28,7 @@ def test_delete(self): assert result.name == expected.name assert result.freq == expected.freq - with pytest.raises((IndexError, ValueError)): + with tm.external_error_raised((IndexError, ValueError)): # either depending on numpy version idx.delete(5)