@@ -77,8 +77,8 @@ def test_constructor_unsortable(self):
77
77
assert not factor .ordered
78
78
79
79
# this however will raise as cannot be sorted
80
- pytest .raises (
81
- TypeError , lambda : Categorical (arr , ordered = True ) )
80
+ with pytest .raises (TypeError ):
81
+ Categorical (arr , ordered = True )
82
82
83
83
def test_constructor_interval (self ):
84
84
result = Categorical ([Interval (1 , 2 ), Interval (2 , 3 ), Interval (3 , 6 )],
@@ -99,16 +99,12 @@ def test_constructor(self):
99
99
tm .assert_numpy_array_equal (c2 .__array__ (), exp_arr )
100
100
101
101
# categories must be unique
102
- def f ( ):
102
+ with pytest . raises ( ValueError ):
103
103
Categorical ([1 , 2 ], [1 , 2 , 2 ])
104
104
105
- pytest .raises (ValueError , f )
106
-
107
- def f ():
105
+ with pytest .raises (ValueError ):
108
106
Categorical (["a" , "b" ], ["a" , "b" , "b" ])
109
107
110
- pytest .raises (ValueError , f )
111
-
112
108
# The default should be unordered
113
109
c1 = Categorical (["a" , "b" , "c" , "a" ])
114
110
assert not c1 .ordered
@@ -421,35 +417,25 @@ def test_constructor_with_categorical_categories(self):
421
417
def test_from_codes (self ):
422
418
423
419
# too few categories
424
- def f ( ):
420
+ with pytest . raises ( ValueError ):
425
421
Categorical .from_codes ([1 , 2 ], [1 , 2 ])
426
422
427
- pytest .raises (ValueError , f )
428
-
429
423
# no int codes
430
- def f ( ):
424
+ with pytest . raises ( ValueError ):
431
425
Categorical .from_codes (["a" ], [1 , 2 ])
432
426
433
- pytest .raises (ValueError , f )
434
-
435
427
# no unique categories
436
- def f ( ):
428
+ with pytest . raises ( ValueError ):
437
429
Categorical .from_codes ([0 , 1 , 2 ], ["a" , "a" , "b" ])
438
430
439
- pytest .raises (ValueError , f )
440
-
441
431
# NaN categories included
442
- def f ( ):
432
+ with pytest . raises ( ValueError ):
443
433
Categorical .from_codes ([0 , 1 , 2 ], ["a" , "b" , np .nan ])
444
434
445
- pytest .raises (ValueError , f )
446
-
447
435
# too negative
448
- def f ( ):
436
+ with pytest . raises ( ValueError ):
449
437
Categorical .from_codes ([- 2 , 1 , 2 ], ["a" , "b" , "c" ])
450
438
451
- pytest .raises (ValueError , f )
452
-
453
439
exp = Categorical (["a" , "b" , "c" ], ordered = False )
454
440
res = Categorical .from_codes ([0 , 1 , 2 ], ["a" , "b" , "c" ])
455
441
tm .assert_categorical_equal (exp , res )
0 commit comments