@@ -21,18 +21,13 @@ class TestCategoricalConstructors(object):
21
21
def test_validate_ordered (self ):
22
22
# see gh-14058
23
23
exp_msg = "'ordered' must either be 'True' or 'False'"
24
- exp_err = TypeError
25
24
26
- # This should be a boolean.
25
+ # This should be a boolean or None .
27
26
ordered = np .array ([0 , 1 , 2 ])
28
27
29
- with pytest .raises (exp_err , match = exp_msg ):
28
+ with pytest .raises (TypeError , match = exp_msg ):
30
29
Categorical ([1 , 2 , 3 ], ordered = ordered )
31
30
32
- with pytest .raises (exp_err , match = exp_msg ):
33
- Categorical .from_codes ([0 , 0 , 1 ], categories = ['a' , 'b' , 'c' ],
34
- ordered = ordered )
35
-
36
31
def test_constructor_empty (self ):
37
32
# GH 17248
38
33
c = Categorical ([])
@@ -421,76 +416,41 @@ def test_constructor_with_categorical_categories(self):
421
416
tm .assert_categorical_equal (result , expected )
422
417
423
418
def test_from_codes (self ):
419
+ dtype = CategoricalDtype (categories = [1 , 2 ])
420
+
421
+ # no dtype or categories
422
+ msg = "Must specify `categories` or `dtype`."
423
+ with pytest .raises (ValueError , match = msg ):
424
+ Categorical .from_codes ([1 , 2 ])
424
425
425
426
# too few categories
426
- dtype = CategoricalDtype (categories = [1 , 2 ])
427
427
msg = "codes need to be between "
428
- with pytest .raises (ValueError , match = msg ):
429
- Categorical .from_codes ([1 , 2 ], categories = dtype .categories )
430
428
with pytest .raises (ValueError , match = msg ):
431
429
Categorical .from_codes ([1 , 2 ], dtype = dtype )
432
430
433
431
# no int codes
434
432
msg = "codes need to be array-like integers"
435
- with pytest .raises (ValueError , match = msg ):
436
- Categorical .from_codes (["a" ], categories = dtype .categories )
437
433
with pytest .raises (ValueError , match = msg ):
438
434
Categorical .from_codes (["a" ], dtype = dtype )
439
435
440
- # no unique categories
441
- with pytest .raises (ValueError ,
442
- match = "Categorical categories must be unique" ):
443
- Categorical .from_codes ([0 , 1 , 2 ], categories = ["a" , "a" , "b" ])
444
-
445
- # NaN categories included
446
- with pytest .raises (ValueError ,
447
- match = "Categorial categories cannot be null" ):
448
- Categorical .from_codes ([0 , 1 , 2 ], categories = ["a" , "b" , np .nan ])
449
-
450
436
# too negative
451
437
dtype = CategoricalDtype (categories = ["a" , "b" , "c" ])
452
438
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
439
with pytest .raises (ValueError , match = msg ):
456
440
Categorical .from_codes ([- 2 , 1 , 2 ], dtype = dtype )
457
441
458
442
exp = Categorical (["a" , "b" , "c" ], ordered = False )
459
- res = Categorical .from_codes ([0 , 1 , 2 ], categories = dtype .categories )
460
- tm .assert_categorical_equal (exp , res )
461
-
462
443
res = Categorical .from_codes ([0 , 1 , 2 ], dtype = dtype )
463
444
tm .assert_categorical_equal (exp , res )
464
445
465
446
codes = np .random .choice ([0 , 1 ], 5 , p = [0.9 , 0.1 ])
466
447
dtype = CategoricalDtype (categories = ["train" , "test" ])
467
- Categorical .from_codes (codes , categories = dtype .categories )
468
448
Categorical .from_codes (codes , dtype = dtype )
469
449
470
- def test_from_codes_with_categorical_categories (self ):
471
- # GH17884
472
- expected = Categorical (['a' , 'b' ], categories = ['a' , 'b' , 'c' ])
473
-
474
- result = Categorical .from_codes (
475
- [0 , 1 ], categories = Categorical (['a' , 'b' , 'c' ]))
476
- tm .assert_categorical_equal (result , expected )
477
-
478
- result = Categorical .from_codes (
479
- [0 , 1 ], categories = CategoricalIndex (['a' , 'b' , 'c' ]))
480
- tm .assert_categorical_equal (result , expected )
481
-
482
- # non-unique Categorical still raises
483
- with pytest .raises (ValueError ,
484
- match = "Categorical categories must be unique" ):
485
- Categorical .from_codes ([0 , 1 ], Categorical (['a' , 'b' , 'a' ]))
486
-
487
450
def test_from_codes_with_nan_code (self ):
488
451
# GH21767
489
452
codes = [1 , 2 , np .nan ]
490
453
dtype = CategoricalDtype (categories = ['a' , 'b' , 'c' ])
491
- with pytest .raises (ValueError ,
492
- match = "codes need to be array-like integers" ):
493
- Categorical .from_codes (codes , categories = dtype .categories )
494
454
with pytest .raises (ValueError ,
495
455
match = "codes need to be array-like integers" ):
496
456
Categorical .from_codes (codes , dtype = dtype )
@@ -500,36 +460,41 @@ def test_from_codes_with_float(self):
500
460
codes = [1.0 , 2.0 , 0 ] # integer, but in float dtype
501
461
dtype = CategoricalDtype (categories = ['a' , 'b' , 'c' ])
502
462
503
- with tm .assert_produces_warning (FutureWarning ):
504
- cat = Categorical .from_codes (codes , dtype .categories )
505
- tm .assert_numpy_array_equal (cat .codes , np .array ([1 , 2 , 0 ], dtype = 'i1' ))
506
-
507
- with tm .assert_produces_warning (FutureWarning ):
463
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
508
464
cat = Categorical .from_codes (codes , dtype = dtype )
509
465
tm .assert_numpy_array_equal (cat .codes , np .array ([1 , 2 , 0 ], dtype = 'i1' ))
510
466
511
467
codes = [1.1 , 2.0 , 0 ] # non-integer
512
- with pytest .raises (ValueError ,
513
- match = "codes need to be array-like integers" ):
514
- Categorical .from_codes (codes , dtype .categories )
515
468
with pytest .raises (ValueError ,
516
469
match = "codes need to be array-like integers" ):
517
470
Categorical .from_codes (codes , dtype = dtype )
518
471
472
+ def test_from_codes_deprecated (self ):
473
+ with tm .assert_produces_warning (FutureWarning ):
474
+ Categorical .from_codes ([0 , 1 ], categories = ['a' , 'b' ])
475
+
476
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
477
+ Categorical .from_codes ([0 , 1 ], categories = ['a' , 'b' ], ordered = True )
478
+
479
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
480
+ Categorical .from_codes ([0 , 1 ], categories = ['a' , 'b' ], ordered = False )
481
+
519
482
@pytest .mark .parametrize ('dtype' , [None , 'category' ])
520
483
def test_from_inferred_categories (self , dtype ):
521
484
cats = ['a' , 'b' ]
522
485
codes = np .array ([0 , 0 , 1 , 1 ], dtype = 'i8' )
523
486
result = Categorical ._from_inferred_categories (cats , codes , dtype )
524
- expected = Categorical .from_codes (codes , cats )
487
+ expected = Categorical .from_codes (codes ,
488
+ dtype = CategoricalDtype (cats ))
525
489
tm .assert_categorical_equal (result , expected )
526
490
527
491
@pytest .mark .parametrize ('dtype' , [None , 'category' ])
528
492
def test_from_inferred_categories_sorts (self , dtype ):
529
493
cats = ['b' , 'a' ]
530
494
codes = np .array ([0 , 1 , 1 , 1 ], dtype = 'i8' )
531
495
result = Categorical ._from_inferred_categories (cats , codes , dtype )
532
- expected = Categorical .from_codes ([1 , 0 , 0 , 0 ], ['a' , 'b' ])
496
+ expected = Categorical .from_codes ([1 , 0 , 0 , 0 ],
497
+ dtype = CategoricalDtype (['a' , 'b' ]))
533
498
tm .assert_categorical_equal (result , expected )
534
499
535
500
def test_from_inferred_categories_dtype (self ):
0 commit comments