@@ -53,23 +53,20 @@ def test_loc_scalar(self):
5353 assert_frame_equal (df , expected )
5454
5555 # 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' ]
5758
58- def f ():
59+ msg = "cannot append a non-category item to a CategoricalIndex"
60+ with pytest .raises (TypeError , match = msg ):
5961 df .loc ['d' ] = 10
6062
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 ):
6466 df .loc ['d' , 'A' ] = 10
65-
66- pytest .raises (TypeError , f )
67-
68- def f ():
67+ with pytest .raises (TypeError , match = msg ):
6968 df .loc ['d' , 'C' ] = 10
7069
71- pytest .raises (TypeError , f )
72-
7370 def test_getitem_scalar (self ):
7471
7572 cats = Categorical ([Timestamp ('12-31-1999' ),
@@ -318,7 +315,8 @@ def test_loc_listlike(self):
318315 assert_frame_equal (result , expected , check_index_type = True )
319316
320317 # 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' ]
322320
323321 # assign is ok
324322 df = self .df2 .copy ()
@@ -616,22 +614,29 @@ def test_reindexing(self):
616614 assert_frame_equal (result , expected , check_index_type = True )
617615
618616 # 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' ])
620620
621621 # 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 )
628629
629630 def test_loc_slice (self ):
630631 # slicing
631632 # not implemented ATM
632633 # GH9748
633634
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 ]
635640
636641 # result = df.loc[1:5]
637642 # expected = df.iloc[[1,2,3,4]]
@@ -679,8 +684,11 @@ def test_boolean_selection(self):
679684 # categories=[3, 2, 1],
680685 # ordered=False,
681686 # 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 ]
684692
685693 def test_indexing_with_category (self ):
686694
0 commit comments