Skip to content

TST: Assert msg with pytest raises in pandas/tests/extension/base #38232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pandas/tests/extension/base/getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@ def test_getitem_mask(self, data):

def test_getitem_mask_raises(self, data):
mask = np.array([True, False])
with pytest.raises(IndexError):
msg = f"Boolean index has wrong length: 2 instead of {len(data)}"
with pytest.raises(IndexError, match=msg):
data[mask]

mask = pd.array(mask, dtype="boolean")
with pytest.raises(IndexError):
with pytest.raises(IndexError, match=msg):
data[mask]

def test_getitem_boolean_array_mask(self, data):
Expand Down Expand Up @@ -305,7 +306,9 @@ def test_take_empty(self, data, na_value, na_cmp):
result = empty.take([-1], allow_fill=True)
assert na_cmp(result[0], na_value)

with pytest.raises(IndexError):
msg = "cannot do a non-empty take from an empty axes|out of bounds"

with pytest.raises(IndexError, match=msg):
empty.take([-1])

with pytest.raises(IndexError, match="cannot do a non-empty take"):
Expand All @@ -330,13 +333,14 @@ def test_take_non_na_fill_value(self, data_missing):
self.assert_extension_array_equal(result, expected)

def test_take_pandas_style_negative_raises(self, data, na_value):
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=""):
data.take([0, -2], fill_value=na_value, allow_fill=True)

@pytest.mark.parametrize("allow_fill", [True, False])
def test_take_out_of_bounds_raises(self, data, allow_fill):
arr = data[:3]
with pytest.raises(IndexError):

with pytest.raises(IndexError, match="out of bounds|out-of-bounds"):
arr.take(np.asarray([0, 3]), allow_fill=allow_fill)

def test_take_series(self, data):
Expand Down
14 changes: 12 additions & 2 deletions pandas/tests/extension/base/reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna):
op_name = all_numeric_reductions
s = pd.Series(data)

with pytest.raises(TypeError):
msg = (
"[Cc]annot perform|Categorical is not ordered for operation|"
"'Categorical' does not implement reduction|"
)

with pytest.raises(TypeError, match=msg):
getattr(s, op_name)(skipna=skipna)

@pytest.mark.parametrize("skipna", [True, False])
def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna):
op_name = all_boolean_reductions
s = pd.Series(data)

with pytest.raises(TypeError):
msg = (
"[Cc]annot perform|Categorical is not ordered for operation|"
"'Categorical' does not implement reduction|"
)

with pytest.raises(TypeError, match=msg):
getattr(s, op_name)(skipna=skipna)


Expand Down