Skip to content

Avoid bare pytest.raises in indexes/categorical/test_indexing.py #32797

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

2 changes: 1 addition & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def validate_categories(categories, fastpath: bool = False):
if not fastpath:

if categories.hasnans:
raise ValueError("Categorial categories cannot be null")
raise ValueError("Categorical categories cannot be null")

if not categories.is_unique:
raise ValueError("Categorical categories must be unique")
Expand Down
22 changes: 13 additions & 9 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def test_hash_vs_equality(self, dtype):
assert hash(dtype) == hash(dtype3)

def test_construction(self):
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Invalid frequency: xx"):
PeriodDtype("xx")

for s in ["period[D]", "Period[D]", "D"]:
Expand Down Expand Up @@ -414,16 +414,17 @@ def test_construction_from_string(self, dtype):
assert is_dtype_equal(dtype, result)
result = PeriodDtype.construct_from_string("period[D]")
assert is_dtype_equal(dtype, result)
with pytest.raises(TypeError):
msg = "Cannot construct a 'PeriodDtype' from "
with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("foo")
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("period[foo]")
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("foo[D]")

with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("datetime64[ns]")
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]")

with pytest.raises(TypeError, match="list"):
Expand Down Expand Up @@ -475,7 +476,8 @@ def test_basic(self, dtype):

def test_empty(self):
dt = PeriodDtype()
with pytest.raises(AttributeError):
msg = "object has no attribute 'freqstr'"
with pytest.raises(AttributeError, match=msg):
str(dt)

def test_not_string(self):
Expand Down Expand Up @@ -764,11 +766,13 @@ def test_order_hashes_different(self, v1, v2):
assert c1 is not c3

def test_nan_invalid(self):
with pytest.raises(ValueError):
msg = "Categorical categories cannot be null"
with pytest.raises(ValueError, match=msg):
CategoricalDtype([1, 2, np.nan])

def test_non_unique_invalid(self):
with pytest.raises(ValueError):
msg = "Categorical categories must be unique"
with pytest.raises(ValueError, match=msg):
CategoricalDtype([1, 2, 1])

def test_same_categories_different_order(self):
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/categorical/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def test_take_fill_value(self):
with pytest.raises(ValueError, match=msg):
idx.take(np.array([1, 0, -5]), fill_value=True)

with pytest.raises(IndexError):
msg = "index -5 is out of bounds for size 3"
with pytest.raises(IndexError, match=msg):
idx.take(np.array([1, -5]))

def test_take_fill_value_datetime(self):
Expand Down Expand Up @@ -104,7 +105,8 @@ def test_take_fill_value_datetime(self):
with pytest.raises(ValueError, match=msg):
idx.take(np.array([1, 0, -5]), fill_value=True)

with pytest.raises(IndexError):
msg = "index -5 is out of bounds for size 3"
with pytest.raises(IndexError, match=msg):
idx.take(np.array([1, -5]))

def test_take_invalid_kwargs(self):
Expand Down