@@ -53,23 +53,20 @@ def test_loc_scalar(self):
53
53
assert_frame_equal (df , expected )
54
54
55
55
# value not in the categories
56
- pytest .raises (KeyError , lambda : df .loc ['d' ])
56
+ with pytest .raises (KeyError , match = r"^'d'$" ):
57
+ df .loc ['d' ]
57
58
58
- def f ():
59
+ msg = "cannot append a non-category item to a CategoricalIndex"
60
+ with pytest .raises (TypeError , match = msg ):
59
61
df .loc ['d' ] = 10
60
62
61
- pytest . raises ( TypeError , f )
62
-
63
- def f ( ):
63
+ msg = ( "cannot insert an item into a CategoricalIndex that is not"
64
+ " already an existing category" )
65
+ with pytest . raises ( TypeError , match = msg ):
64
66
df .loc ['d' , 'A' ] = 10
65
-
66
- pytest .raises (TypeError , f )
67
-
68
- def f ():
67
+ with pytest .raises (TypeError , match = msg ):
69
68
df .loc ['d' , 'C' ] = 10
70
69
71
- pytest .raises (TypeError , f )
72
-
73
70
def test_getitem_scalar (self ):
74
71
75
72
cats = Categorical ([Timestamp ('12-31-1999' ),
@@ -318,7 +315,8 @@ def test_loc_listlike(self):
318
315
assert_frame_equal (result , expected , check_index_type = True )
319
316
320
317
# element in the categories but not in the values
321
- pytest .raises (KeyError , lambda : self .df2 .loc ['e' ])
318
+ with pytest .raises (KeyError , match = r"^'e'$" ):
319
+ self .df2 .loc ['e' ]
322
320
323
321
# assign is ok
324
322
df = self .df2 .copy ()
@@ -616,22 +614,29 @@ def test_reindexing(self):
616
614
assert_frame_equal (result , expected , check_index_type = True )
617
615
618
616
# passed duplicate indexers are not allowed
619
- pytest .raises (ValueError , lambda : self .df2 .reindex (['a' , 'a' ]))
617
+ msg = "cannot reindex with a non-unique indexer"
618
+ with pytest .raises (ValueError , match = msg ):
619
+ self .df2 .reindex (['a' , 'a' ])
620
620
621
621
# args NotImplemented ATM
622
- pytest .raises (NotImplementedError ,
623
- lambda : self .df2 .reindex (['a' ], method = 'ffill' ))
624
- pytest .raises (NotImplementedError ,
625
- lambda : self .df2 .reindex (['a' ], level = 1 ))
626
- pytest .raises (NotImplementedError ,
627
- lambda : self .df2 .reindex (['a' ], limit = 2 ))
622
+ msg = r"argument {} is not implemented for CategoricalIndex\.reindex"
623
+ with pytest .raises (NotImplementedError , match = msg .format ('method' )):
624
+ self .df2 .reindex (['a' ], method = 'ffill' )
625
+ with pytest .raises (NotImplementedError , match = msg .format ('level' )):
626
+ self .df2 .reindex (['a' ], level = 1 )
627
+ with pytest .raises (NotImplementedError , match = msg .format ('limit' )):
628
+ self .df2 .reindex (['a' ], limit = 2 )
628
629
629
630
def test_loc_slice (self ):
630
631
# slicing
631
632
# not implemented ATM
632
633
# GH9748
633
634
634
- pytest .raises (TypeError , lambda : self .df .loc [1 :5 ])
635
+ msg = ("cannot do slice indexing on {klass} with these "
636
+ r"indexers \[1\] of {kind}" .format (
637
+ klass = str (CategoricalIndex ), kind = str (int )))
638
+ with pytest .raises (TypeError , match = msg ):
639
+ self .df .loc [1 :5 ]
635
640
636
641
# result = df.loc[1:5]
637
642
# expected = df.iloc[[1,2,3,4]]
@@ -679,8 +684,11 @@ def test_boolean_selection(self):
679
684
# categories=[3, 2, 1],
680
685
# ordered=False,
681
686
# name=u'B')
682
- pytest .raises (TypeError , lambda : df4 [df4 .index < 2 ])
683
- pytest .raises (TypeError , lambda : df4 [df4 .index > 1 ])
687
+ msg = "Unordered Categoricals can only compare equality or not"
688
+ with pytest .raises (TypeError , match = msg ):
689
+ df4 [df4 .index < 2 ]
690
+ with pytest .raises (TypeError , match = msg ):
691
+ df4 [df4 .index > 1 ]
684
692
685
693
def test_indexing_with_category (self ):
686
694
0 commit comments