@@ -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"^Parameter 'categories' must be list-like, was"
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' , [
@@ -417,33 +423,44 @@ def test_constructor_with_categorical_categories(self):
417
423
def test_from_codes (self ):
418
424
419
425
# too few categories
420
- with pytest .raises (ValueError ):
421
- Categorical .from_codes ([1 , 2 ], [1 , 2 ])
426
+ dtype = CategoricalDtype (categories = [1 , 2 ])
427
+ msg = "codes need to be between "
428
+ with pytest .raises (ValueError , match = msg ):
429
+ Categorical .from_codes ([1 , 2 ], categories = dtype .categories )
430
+ with pytest .raises (ValueError , match = msg ):
431
+ Categorical .from_codes ([1 , 2 ], dtype = dtype )
422
432
423
433
# no int codes
424
- with pytest .raises (ValueError ):
425
- Categorical .from_codes (["a" ], [1 , 2 ])
434
+ msg = "codes need to be array-like integers"
435
+ with pytest .raises (ValueError , match = msg ):
436
+ Categorical .from_codes (["a" ], categories = dtype .categories )
437
+ with pytest .raises (ValueError , match = msg ):
438
+ Categorical .from_codes (["a" ], dtype = dtype )
426
439
427
440
# no unique categories
428
- with pytest .raises (ValueError ):
429
- Categorical .from_codes ([0 , 1 , 2 ], ["a" , "a" , "b" ])
441
+ with pytest .raises (ValueError ,
442
+ match = "Categorical categories must be unique" ):
443
+ Categorical .from_codes ([0 , 1 , 2 ], categories = ["a" , "a" , "b" ])
430
444
431
445
# NaN categories included
432
- with pytest .raises (ValueError ):
433
- Categorical .from_codes ([0 , 1 , 2 ], ["a" , "b" , np .nan ])
446
+ with pytest .raises (ValueError ,
447
+ match = "Categorial categories cannot be null" ):
448
+ Categorical .from_codes ([0 , 1 , 2 ], categories = ["a" , "b" , np .nan ])
434
449
435
450
# too negative
436
- with pytest .raises (ValueError ):
437
- Categorical .from_codes ([- 2 , 1 , 2 ], ["a" , "b" , "c" ])
451
+ dtype = CategoricalDtype (categories = ["a" , "b" , "c" ])
452
+ msg = r"codes need to be between -1 and len\(categories\)-1"
453
+ with pytest .raises (ValueError , match = msg ):
454
+ Categorical .from_codes ([- 2 , 1 , 2 ], categories = dtype .categories )
455
+ with pytest .raises (ValueError , match = msg ):
456
+ Categorical .from_codes ([- 2 , 1 , 2 ], dtype = dtype )
438
457
439
458
exp = Categorical (["a" , "b" , "c" ], ordered = False )
440
- res = Categorical .from_codes ([0 , 1 , 2 ], [ "a" , "b" , "c" ] )
459
+ res = Categorical .from_codes ([0 , 1 , 2 ], categories = dtype . categories )
441
460
tm .assert_categorical_equal (exp , res )
442
461
443
- # Not available in earlier numpy versions
444
- if hasattr (np .random , "choice" ):
445
- codes = np .random .choice ([0 , 1 ], 5 , p = [0.9 , 0.1 ])
446
- Categorical .from_codes (codes , categories = ["train" , "test" ])
462
+ res = Categorical .from_codes ([0 , 1 , 2 ], dtype = dtype )
463
+ tm .assert_categorical_equal (exp , res )
447
464
448
465
def test_from_codes_with_categorical_categories (self ):
449
466
# GH17884
@@ -458,28 +475,56 @@ def test_from_codes_with_categorical_categories(self):
458
475
tm .assert_categorical_equal (result , expected )
459
476
460
477
# non-unique Categorical still raises
461
- with pytest .raises (ValueError ):
478
+ with pytest .raises (ValueError ,
479
+ match = "Categorical categories must be unique" ):
462
480
Categorical .from_codes ([0 , 1 ], Categorical (['a' , 'b' , 'a' ]))
463
481
464
482
def test_from_codes_with_nan_code (self ):
465
483
# GH21767
466
484
codes = [1 , 2 , np .nan ]
467
- categories = ['a' , 'b' , 'c' ]
468
- with pytest .raises (ValueError ):
469
- Categorical .from_codes (codes , categories )
485
+ dtype = CategoricalDtype (categories = ['a' , 'b' , 'c' ])
486
+ with pytest .raises (ValueError ,
487
+ match = "codes need to be array-like integers" ):
488
+ Categorical .from_codes (codes , categories = dtype .categories )
489
+ with pytest .raises (ValueError ,
490
+ match = "codes need to be array-like integers" ):
491
+ Categorical .from_codes (codes , dtype = dtype )
470
492
471
493
def test_from_codes_with_float (self ):
472
494
# GH21767
473
495
codes = [1.0 , 2.0 , 0 ] # integer, but in float dtype
474
- categories = ['a' , 'b' , 'c' ]
496
+ dtype = CategoricalDtype (categories = ['a' , 'b' , 'c' ])
497
+
498
+ with tm .assert_produces_warning (FutureWarning ):
499
+ cat = Categorical .from_codes (codes , dtype .categories )
500
+ tm .assert_numpy_array_equal (cat .codes , np .array ([1 , 2 , 0 ], dtype = 'i1' ))
475
501
476
502
with tm .assert_produces_warning (FutureWarning ):
477
- cat = Categorical .from_codes (codes , categories )
503
+ cat = Categorical .from_codes (codes , dtype = dtype )
478
504
tm .assert_numpy_array_equal (cat .codes , np .array ([1 , 2 , 0 ], dtype = 'i1' ))
479
505
480
506
codes = [1.1 , 2.0 , 0 ] # non-integer
481
- with pytest .raises (ValueError ):
482
- Categorical .from_codes (codes , categories )
507
+ with pytest .raises (ValueError ,
508
+ match = "codes need to be array-like integers" ):
509
+ Categorical .from_codes (codes , dtype .categories )
510
+ with pytest .raises (ValueError ,
511
+ match = "codes need to be array-like integers" ):
512
+ Categorical .from_codes (codes , dtype = dtype )
513
+
514
+ def test_from_codes_with_dtype_raises (self ):
515
+ msg = 'Cannot specify'
516
+ with pytest .raises (ValueError , match = msg ):
517
+ Categorical .from_codes ([0 , 1 ], categories = ['a' , 'b' ],
518
+ dtype = CategoricalDtype (['a' , 'b' ]))
519
+
520
+ with pytest .raises (ValueError , match = msg ):
521
+ Categorical .from_codes ([0 , 1 ], ordered = True ,
522
+ dtype = CategoricalDtype (['a' , 'b' ]))
523
+
524
+ def test_from_codes_neither (self ):
525
+ msg = "Both were None"
526
+ with pytest .raises (ValueError , match = msg ):
527
+ Categorical .from_codes ([0 , 1 ])
483
528
484
529
@pytest .mark .parametrize ('dtype' , [None , 'category' ])
485
530
def test_from_inferred_categories (self , dtype ):
@@ -515,14 +560,11 @@ def test_from_inferred_categories_coerces(self):
515
560
expected = Categorical ([1 , 1 , 2 , np .nan ])
516
561
tm .assert_categorical_equal (result , expected )
517
562
518
- def test_construction_with_ordered (self ):
563
+ @pytest .mark .parametrize ('ordered' , [None , True , False ])
564
+ def test_construction_with_ordered (self , ordered ):
519
565
# GH 9347, 9190
520
- cat = Categorical ([0 , 1 , 2 ])
521
- assert not cat .ordered
522
- cat = Categorical ([0 , 1 , 2 ], ordered = False )
523
- assert not cat .ordered
524
- cat = Categorical ([0 , 1 , 2 ], ordered = True )
525
- assert cat .ordered
566
+ cat = Categorical ([0 , 1 , 2 ], ordered = ordered )
567
+ assert cat .ordered == bool (ordered )
526
568
527
569
@pytest .mark .xfail (reason = "Imaginary values not supported in Categorical" )
528
570
def test_constructor_imaginary (self ):
0 commit comments