@@ -888,6 +888,60 @@ def test_multi_assign(self):
888888 df2 .ix [mask , cols ]= dft .ix [mask , cols ].values
889889 assert_frame_equal (df2 ,expected )
890890
891+ def test_iloc_mask (self ):
892+
893+ # GH 3631, iloc with a mask (of a series) should raise
894+ df = DataFrame (range (5 ), list ('ABCDE' ), columns = ['a' ])
895+ mask = (df .a % 2 == 0 )
896+ self .assertRaises (ValueError , df .iloc .__getitem__ , tuple ([mask ]))
897+ mask .index = range (len (mask ))
898+ self .assertRaises (NotImplementedError , df .iloc .__getitem__ , tuple ([mask ]))
899+
900+ # ndarray ok
901+ result = df .iloc [np .array ([True ] * len (mask ),dtype = bool )]
902+ assert_frame_equal (result ,df )
903+
904+ # the possibilities
905+ locs = np .arange (4 )
906+ nums = 2 ** locs
907+ reps = map (bin , nums )
908+ df = DataFrame ({'locs' :locs , 'nums' :nums }, reps )
909+
910+ expected = {
911+ (None ,'' ) : '0b1100' ,
912+ (None ,'.loc' ) : '0b1100' ,
913+ (None ,'.iloc' ) : '0b1100' ,
914+ ('index' ,'' ) : '0b11' ,
915+ ('index' ,'.loc' ) : '0b11' ,
916+ ('index' ,'.iloc' ) : 'iLocation based boolean indexing cannot use an indexable as a mask' ,
917+ ('locs' ,'' ) : 'Unalignable boolean Series key provided' ,
918+ ('locs' ,'.loc' ) : 'Unalignable boolean Series key provided' ,
919+ ('locs' ,'.iloc' ) : 'iLocation based boolean indexing on an integer type is not available' ,
920+ }
921+
922+ import warnings
923+ warnings .filterwarnings (action = 'ignore' , category = UserWarning )
924+ result = dict ()
925+ for idx in [None , 'index' , 'locs' ]:
926+ mask = (df .nums > 2 ).values
927+ if idx :
928+ mask = Series (mask , list (reversed (getattr (df , idx ))))
929+ for method in ['' , '.loc' , '.iloc' ]:
930+ try :
931+ if method :
932+ accessor = getattr (df , method [1 :])
933+ else :
934+ accessor = df
935+ ans = str (bin (accessor [mask ]['nums' ].sum ()))
936+ except Exception , e :
937+ ans = str (e )
938+
939+ key = tuple ([idx ,method ])
940+ r = expected .get (key )
941+ if r != ans :
942+ raise AssertionError ("[%s] does not match [%s], received [%s]" %
943+ (key ,ans ,r ))
944+ warnings .filterwarnings (action = 'always' , category = UserWarning )
891945
892946if __name__ == '__main__' :
893947 import nose
0 commit comments