@@ -417,33 +417,44 @@ def test_constructor_with_categorical_categories(self):
417
417
def test_from_codes (self ):
418
418
419
419
# too few categories
420
+ dtype = CategoricalDtype (categories = [1 , 2 ])
420
421
with pytest .raises (ValueError ):
421
- Categorical .from_codes ([1 , 2 ], [1 , 2 ])
422
+ Categorical .from_codes ([1 , 2 ], categories = dtype .categories )
423
+ with pytest .raises (ValueError ):
424
+ Categorical .from_codes ([1 , 2 ], dtype = dtype )
422
425
423
426
# no int codes
424
427
with pytest .raises (ValueError ):
425
- Categorical .from_codes (["a" ], [1 , 2 ])
428
+ Categorical .from_codes (["a" ], categories = dtype .categories )
429
+ with pytest .raises (ValueError ):
430
+ Categorical .from_codes (["a" ], dtype = dtype )
426
431
427
432
# no unique categories
428
433
with pytest .raises (ValueError ):
429
- Categorical .from_codes ([0 , 1 , 2 ], ["a" , "a" , "b" ])
434
+ Categorical .from_codes ([0 , 1 , 2 ], categories = ["a" , "a" , "b" ])
430
435
431
436
# NaN categories included
432
437
with pytest .raises (ValueError ):
433
- Categorical .from_codes ([0 , 1 , 2 ], ["a" , "b" , np .nan ])
438
+ Categorical .from_codes ([0 , 1 , 2 ], categories = ["a" , "b" , np .nan ])
434
439
435
440
# too negative
441
+ dtype = CategoricalDtype (categories = ["a" , "b" , "c" ])
442
+ with pytest .raises (ValueError ):
443
+ Categorical .from_codes ([- 2 , 1 , 2 ], categories = dtype .categories )
436
444
with pytest .raises (ValueError ):
437
- Categorical .from_codes ([- 2 , 1 , 2 ], [ "a" , "b" , "c" ] )
445
+ Categorical .from_codes ([- 2 , 1 , 2 ], dtype = dtype )
438
446
439
447
exp = Categorical (["a" , "b" , "c" ], ordered = False )
440
- res = Categorical .from_codes ([0 , 1 , 2 ], ["a" , "b" , "c" ])
448
+ res = Categorical .from_codes ([0 , 1 , 2 ], categories = dtype .categories )
449
+ tm .assert_categorical_equal (exp , res )
450
+
451
+ res = Categorical .from_codes ([0 , 1 , 2 ], dtype = dtype )
441
452
tm .assert_categorical_equal (exp , res )
442
453
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" ] )
454
+ codes = np . random . choice ([ 0 , 1 ], 5 , p = [ 0.9 , 0.1 ])
455
+ dtype = CategoricalDtype ( categories = [ "train" , "test" ])
456
+ Categorical . from_codes ( codes , categories = dtype . categories )
457
+ Categorical .from_codes (codes , dtype = dtype )
447
458
448
459
def test_from_codes_with_categorical_categories (self ):
449
460
# GH17884
@@ -464,22 +475,30 @@ def test_from_codes_with_categorical_categories(self):
464
475
def test_from_codes_with_nan_code (self ):
465
476
# GH21767
466
477
codes = [1 , 2 , np .nan ]
467
- categories = ['a' , 'b' , 'c' ]
478
+ dtype = CategoricalDtype ( categories = ['a' , 'b' , 'c' ])
468
479
with pytest .raises (ValueError ):
469
- Categorical .from_codes (codes , categories )
480
+ Categorical .from_codes (codes , categories = dtype .categories )
481
+ with pytest .raises (ValueError ):
482
+ Categorical .from_codes (codes , dtype = dtype )
470
483
471
484
def test_from_codes_with_float (self ):
472
485
# GH21767
473
486
codes = [1.0 , 2.0 , 0 ] # integer, but in float dtype
474
- categories = ['a' , 'b' , 'c' ]
487
+ dtype = CategoricalDtype ( categories = ['a' , 'b' , 'c' ])
475
488
476
489
with tm .assert_produces_warning (FutureWarning ):
477
- cat = Categorical .from_codes (codes , categories )
490
+ cat = Categorical .from_codes (codes , dtype .categories )
491
+ tm .assert_numpy_array_equal (cat .codes , np .array ([1 , 2 , 0 ], dtype = 'i1' ))
492
+
493
+ with tm .assert_produces_warning (FutureWarning ):
494
+ cat = Categorical .from_codes (codes , dtype = dtype )
478
495
tm .assert_numpy_array_equal (cat .codes , np .array ([1 , 2 , 0 ], dtype = 'i1' ))
479
496
480
497
codes = [1.1 , 2.0 , 0 ] # non-integer
481
498
with pytest .raises (ValueError ):
482
- Categorical .from_codes (codes , categories )
499
+ Categorical .from_codes (codes , dtype .categories )
500
+ with pytest .raises (ValueError ):
501
+ Categorical .from_codes (codes , dtype = dtype )
483
502
484
503
@pytest .mark .parametrize ('dtype' , [None , 'category' ])
485
504
def test_from_inferred_categories (self , dtype ):
@@ -515,14 +534,11 @@ def test_from_inferred_categories_coerces(self):
515
534
expected = Categorical ([1 , 1 , 2 , np .nan ])
516
535
tm .assert_categorical_equal (result , expected )
517
536
518
- def test_construction_with_ordered (self ):
537
+ @pytest .mark .parametrize ('ordered' , [None , True , False ])
538
+ def test_construction_with_ordered (self , ordered ):
519
539
# 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
540
+ cat = Categorical ([0 , 1 , 2 ], ordered = ordered )
541
+ assert cat .ordered == bool (ordered )
526
542
527
543
@pytest .mark .xfail (reason = "Imaginary values not supported in Categorical" )
528
544
def test_constructor_imaginary (self ):
0 commit comments