Skip to content

Commit c790639

Browse files
committed
add match messages
1 parent a7923bd commit c790639

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

pandas/core/arrays/categorical.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
339339
msg = "Unknown `dtype` {dtype}"
340340
raise ValueError(msg.format(dtype=dtype))
341341
elif categories is not None or ordered is not None:
342-
raise ValueError("Cannot specify both `dtype` and `categories`"
343-
" or `ordered`.")
342+
raise ValueError("Cannot specify `categories` or `ordered` "
343+
"together with `dtype`.")
344344

345345
categories = dtype.categories
346346

@@ -673,8 +673,8 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
673673
"""
674674
if dtype is not None:
675675
if categories is not None or ordered is not None:
676-
raise ValueError("Cannot specify both `dtype` and `categories`"
677-
" or `ordered`.")
676+
raise ValueError("Cannot specify `categories` or `ordered` "
677+
"together with `dtype`.")
678678
else:
679679
dtype = CategoricalDtype(categories, ordered)
680680

pandas/tests/arrays/categorical/test_constructors.py

+39-23
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def test_constructor_unsortable(self):
7777
assert not factor.ordered
7878

7979
# this however will raise as cannot be sorted
80-
with pytest.raises(TypeError):
80+
msg = ("'values' is not ordered, please explicitly specify the "
81+
"categories order by passing in a categories argument.")
82+
with pytest.raises(TypeError, match=msg):
8183
Categorical(arr, ordered=True)
8284

8385
def test_constructor_interval(self):
@@ -99,10 +101,11 @@ def test_constructor(self):
99101
tm.assert_numpy_array_equal(c2.__array__(), exp_arr)
100102

101103
# categories must be unique
102-
with pytest.raises(ValueError):
104+
msg = "Categorical categories must be unique"
105+
with pytest.raises(ValueError, match=msg):
103106
Categorical([1, 2], [1, 2, 2])
104107

105-
with pytest.raises(ValueError):
108+
with pytest.raises(ValueError, match=msg):
106109
Categorical(["a", "b"], ["a", "b", "b"])
107110

108111
# The default should be unordered
@@ -211,21 +214,23 @@ def test_constructor(self):
211214

212215
def test_constructor_not_sequence(self):
213216
# https://github.com/pandas-dev/pandas/issues/16022
214-
with pytest.raises(TypeError):
217+
msg = r"^Index\(\.\.\.\) must be called with a collection of some kind"
218+
with pytest.raises(TypeError, match=msg):
215219
Categorical(['a', 'b'], categories='a')
216220

217221
def test_constructor_with_null(self):
218222

219223
# Cannot have NaN in categories
220-
with pytest.raises(ValueError):
224+
msg = "Categorial categories cannot be null"
225+
with pytest.raises(ValueError, match=msg):
221226
Categorical([np.nan, "a", "b", "c"],
222227
categories=[np.nan, "a", "b", "c"])
223228

224-
with pytest.raises(ValueError):
229+
with pytest.raises(ValueError, match=msg):
225230
Categorical([None, "a", "b", "c"],
226231
categories=[None, "a", "b", "c"])
227232

228-
with pytest.raises(ValueError):
233+
with pytest.raises(ValueError, match=msg):
229234
Categorical(DatetimeIndex(['nat', '20160101']),
230235
categories=[NaT, Timestamp('20160101')])
231236

@@ -347,13 +352,14 @@ def test_constructor_with_dtype(self, ordered):
347352

348353
def test_constructor_dtype_and_others_raises(self):
349354
dtype = CategoricalDtype(['a', 'b'], ordered=True)
350-
with pytest.raises(ValueError, match="Cannot"):
355+
msg = "Cannot specify `categories` or `ordered` together with `dtype`."
356+
with pytest.raises(ValueError, match=msg):
351357
Categorical(['a', 'b'], categories=['a', 'b'], dtype=dtype)
352358

353-
with pytest.raises(ValueError, match="Cannot"):
359+
with pytest.raises(ValueError, match=msg):
354360
Categorical(['a', 'b'], ordered=True, dtype=dtype)
355361

356-
with pytest.raises(ValueError, match="Cannot"):
362+
with pytest.raises(ValueError, match=msg):
357363
Categorical(['a', 'b'], ordered=False, dtype=dtype)
358364

359365
@pytest.mark.parametrize('categories', [
@@ -418,30 +424,35 @@ def test_from_codes(self):
418424

419425
# too few categories
420426
dtype = CategoricalDtype(categories=[1, 2])
421-
with pytest.raises(ValueError):
427+
msg = "codes need to be between "
428+
with pytest.raises(ValueError, match=msg):
422429
Categorical.from_codes([1, 2], categories=dtype.categories)
423-
with pytest.raises(ValueError):
430+
with pytest.raises(ValueError, match=msg):
424431
Categorical.from_codes([1, 2], dtype=dtype)
425432

426433
# no int codes
427-
with pytest.raises(ValueError):
434+
msg = "codes need to be array-like integers"
435+
with pytest.raises(ValueError, match=msg):
428436
Categorical.from_codes(["a"], categories=dtype.categories)
429-
with pytest.raises(ValueError):
437+
with pytest.raises(ValueError, match=msg):
430438
Categorical.from_codes(["a"], dtype=dtype)
431439

432440
# no unique categories
433-
with pytest.raises(ValueError):
441+
with pytest.raises(ValueError,
442+
match="Categorical categories must be unique"):
434443
Categorical.from_codes([0, 1, 2], categories=["a", "a", "b"])
435444

436445
# NaN categories included
437-
with pytest.raises(ValueError):
446+
with pytest.raises(ValueError,
447+
match="Categorial categories cannot be null"):
438448
Categorical.from_codes([0, 1, 2], categories=["a", "b", np.nan])
439449

440450
# too negative
441451
dtype = CategoricalDtype(categories=["a", "b", "c"])
442-
with pytest.raises(ValueError):
452+
msg = r"codes need to be between -1 and len\(categories\)-1"
453+
with pytest.raises(ValueError, match=msg):
443454
Categorical.from_codes([-2, 1, 2], categories=dtype.categories)
444-
with pytest.raises(ValueError):
455+
with pytest.raises(ValueError, match=msg):
445456
Categorical.from_codes([-2, 1, 2], dtype=dtype)
446457

447458
exp = Categorical(["a", "b", "c"], ordered=False)
@@ -469,16 +480,19 @@ def test_from_codes_with_categorical_categories(self):
469480
tm.assert_categorical_equal(result, expected)
470481

471482
# non-unique Categorical still raises
472-
with pytest.raises(ValueError):
483+
with pytest.raises(ValueError,
484+
match="Categorical categories must be unique"):
473485
Categorical.from_codes([0, 1], Categorical(['a', 'b', 'a']))
474486

475487
def test_from_codes_with_nan_code(self):
476488
# GH21767
477489
codes = [1, 2, np.nan]
478490
dtype = CategoricalDtype(categories=['a', 'b', 'c'])
479-
with pytest.raises(ValueError):
491+
with pytest.raises(ValueError,
492+
match="codes need to be array-like integers"):
480493
Categorical.from_codes(codes, categories=dtype.categories)
481-
with pytest.raises(ValueError):
494+
with pytest.raises(ValueError,
495+
match="codes need to be array-like integers"):
482496
Categorical.from_codes(codes, dtype=dtype)
483497

484498
def test_from_codes_with_float(self):
@@ -495,9 +509,11 @@ def test_from_codes_with_float(self):
495509
tm.assert_numpy_array_equal(cat.codes, np.array([1, 2, 0], dtype='i1'))
496510

497511
codes = [1.1, 2.0, 0] # non-integer
498-
with pytest.raises(ValueError):
512+
with pytest.raises(ValueError,
513+
match="codes need to be array-like integers"):
499514
Categorical.from_codes(codes, dtype.categories)
500-
with pytest.raises(ValueError):
515+
with pytest.raises(ValueError,
516+
match="codes need to be array-like integers"):
501517
Categorical.from_codes(codes, dtype=dtype)
502518

503519
@pytest.mark.parametrize('dtype', [None, 'category'])

pandas/tests/indexes/test_category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_construction_with_categorical_dtype(self):
156156
tm.assert_index_equal(result, expected, exact=True)
157157

158158
# error when combining categories/ordered and dtype kwargs
159-
msg = 'Cannot specify both `dtype` and `categories` or `ordered`.'
159+
msg = "Cannot specify `categories` or `ordered` together with `dtype`."
160160
with pytest.raises(ValueError, match=msg):
161161
CategoricalIndex(data, categories=cats, dtype=dtype)
162162

0 commit comments

Comments
 (0)