@@ -77,7 +77,9 @@ def test_constructor_unsortable(self):
77
77
assert not factor .ordered
78
78
79
79
# 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 ):
81
83
Categorical (arr , ordered = True )
82
84
83
85
def test_constructor_interval (self ):
@@ -99,10 +101,11 @@ def test_constructor(self):
99
101
tm .assert_numpy_array_equal (c2 .__array__ (), exp_arr )
100
102
101
103
# categories must be unique
102
- with pytest .raises (ValueError ):
104
+ msg = "Categorical categories must be unique"
105
+ with pytest .raises (ValueError , match = msg ):
103
106
Categorical ([1 , 2 ], [1 , 2 , 2 ])
104
107
105
- with pytest .raises (ValueError ):
108
+ with pytest .raises (ValueError , match = msg ):
106
109
Categorical (["a" , "b" ], ["a" , "b" , "b" ])
107
110
108
111
# The default should be unordered
@@ -211,21 +214,23 @@ def test_constructor(self):
211
214
212
215
def test_constructor_not_sequence (self ):
213
216
# 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 ):
215
219
Categorical (['a' , 'b' ], categories = 'a' )
216
220
217
221
def test_constructor_with_null (self ):
218
222
219
223
# Cannot have NaN in categories
220
- with pytest .raises (ValueError ):
224
+ msg = "Categorial categories cannot be null"
225
+ with pytest .raises (ValueError , match = msg ):
221
226
Categorical ([np .nan , "a" , "b" , "c" ],
222
227
categories = [np .nan , "a" , "b" , "c" ])
223
228
224
- with pytest .raises (ValueError ):
229
+ with pytest .raises (ValueError , match = msg ):
225
230
Categorical ([None , "a" , "b" , "c" ],
226
231
categories = [None , "a" , "b" , "c" ])
227
232
228
- with pytest .raises (ValueError ):
233
+ with pytest .raises (ValueError , match = msg ):
229
234
Categorical (DatetimeIndex (['nat' , '20160101' ]),
230
235
categories = [NaT , Timestamp ('20160101' )])
231
236
@@ -347,13 +352,14 @@ def test_constructor_with_dtype(self, ordered):
347
352
348
353
def test_constructor_dtype_and_others_raises (self ):
349
354
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 ):
351
357
Categorical (['a' , 'b' ], categories = ['a' , 'b' ], dtype = dtype )
352
358
353
- with pytest .raises (ValueError , match = "Cannot" ):
359
+ with pytest .raises (ValueError , match = msg ):
354
360
Categorical (['a' , 'b' ], ordered = True , dtype = dtype )
355
361
356
- with pytest .raises (ValueError , match = "Cannot" ):
362
+ with pytest .raises (ValueError , match = msg ):
357
363
Categorical (['a' , 'b' ], ordered = False , dtype = dtype )
358
364
359
365
@pytest .mark .parametrize ('categories' , [
@@ -418,30 +424,35 @@ def test_from_codes(self):
418
424
419
425
# too few categories
420
426
dtype = CategoricalDtype (categories = [1 , 2 ])
421
- with pytest .raises (ValueError ):
427
+ msg = "codes need to be between "
428
+ with pytest .raises (ValueError , match = msg ):
422
429
Categorical .from_codes ([1 , 2 ], categories = dtype .categories )
423
- with pytest .raises (ValueError ):
430
+ with pytest .raises (ValueError , match = msg ):
424
431
Categorical .from_codes ([1 , 2 ], dtype = dtype )
425
432
426
433
# no int codes
427
- with pytest .raises (ValueError ):
434
+ msg = "codes need to be array-like integers"
435
+ with pytest .raises (ValueError , match = msg ):
428
436
Categorical .from_codes (["a" ], categories = dtype .categories )
429
- with pytest .raises (ValueError ):
437
+ with pytest .raises (ValueError , match = msg ):
430
438
Categorical .from_codes (["a" ], dtype = dtype )
431
439
432
440
# no unique categories
433
- with pytest .raises (ValueError ):
441
+ with pytest .raises (ValueError ,
442
+ match = "Categorical categories must be unique" ):
434
443
Categorical .from_codes ([0 , 1 , 2 ], categories = ["a" , "a" , "b" ])
435
444
436
445
# NaN categories included
437
- with pytest .raises (ValueError ):
446
+ with pytest .raises (ValueError ,
447
+ match = "Categorial categories cannot be null" ):
438
448
Categorical .from_codes ([0 , 1 , 2 ], categories = ["a" , "b" , np .nan ])
439
449
440
450
# too negative
441
451
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 ):
443
454
Categorical .from_codes ([- 2 , 1 , 2 ], categories = dtype .categories )
444
- with pytest .raises (ValueError ):
455
+ with pytest .raises (ValueError , match = msg ):
445
456
Categorical .from_codes ([- 2 , 1 , 2 ], dtype = dtype )
446
457
447
458
exp = Categorical (["a" , "b" , "c" ], ordered = False )
@@ -469,16 +480,19 @@ def test_from_codes_with_categorical_categories(self):
469
480
tm .assert_categorical_equal (result , expected )
470
481
471
482
# non-unique Categorical still raises
472
- with pytest .raises (ValueError ):
483
+ with pytest .raises (ValueError ,
484
+ match = "Categorical categories must be unique" ):
473
485
Categorical .from_codes ([0 , 1 ], Categorical (['a' , 'b' , 'a' ]))
474
486
475
487
def test_from_codes_with_nan_code (self ):
476
488
# GH21767
477
489
codes = [1 , 2 , np .nan ]
478
490
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" ):
480
493
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" ):
482
496
Categorical .from_codes (codes , dtype = dtype )
483
497
484
498
def test_from_codes_with_float (self ):
@@ -495,9 +509,11 @@ def test_from_codes_with_float(self):
495
509
tm .assert_numpy_array_equal (cat .codes , np .array ([1 , 2 , 0 ], dtype = 'i1' ))
496
510
497
511
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" ):
499
514
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" ):
501
517
Categorical .from_codes (codes , dtype = dtype )
502
518
503
519
@pytest .mark .parametrize ('dtype' , [None , 'category' ])
0 commit comments