@@ -1036,20 +1036,43 @@ def check_slice(in_slice, expected):
1036
1036
def test_drop (self ):
1037
1037
n = len (self .strIndex )
1038
1038
1039
- dropped = self .strIndex .drop (self .strIndex [lrange (5 , 10 )])
1039
+ drop = self .strIndex [lrange (5 , 10 )]
1040
+ dropped = self .strIndex .drop (drop )
1040
1041
expected = self .strIndex [lrange (5 ) + lrange (10 , n )]
1041
1042
self .assertTrue (dropped .equals (expected ))
1042
1043
1043
1044
self .assertRaises (ValueError , self .strIndex .drop , ['foo' , 'bar' ])
1045
+ self .assertRaises (ValueError , self .strIndex .drop , ['1' , 'bar' ])
1046
+
1047
+ # errors='ignore'
1048
+ mixed = drop .tolist () + ['foo' ]
1049
+ dropped = self .strIndex .drop (mixed , errors = 'ignore' )
1050
+ expected = self .strIndex [lrange (5 ) + lrange (10 , n )]
1051
+ self .assert_index_equal (dropped , expected )
1052
+
1053
+ dropped = self .strIndex .drop (['foo' , 'bar' ], errors = 'ignore' )
1054
+ expected = self .strIndex [lrange (n )]
1055
+ self .assert_index_equal (dropped , expected )
1044
1056
1045
1057
dropped = self .strIndex .drop (self .strIndex [0 ])
1046
1058
expected = self .strIndex [1 :]
1047
- self .assertTrue (dropped . equals ( expected ) )
1059
+ self .assert_index_equal (dropped , expected )
1048
1060
1049
1061
ser = Index ([1 , 2 , 3 ])
1050
1062
dropped = ser .drop (1 )
1051
1063
expected = Index ([2 , 3 ])
1052
- self .assertTrue (dropped .equals (expected ))
1064
+ self .assert_index_equal (dropped , expected )
1065
+
1066
+ # errors='ignore'
1067
+ self .assertRaises (ValueError , ser .drop , [3 , 4 ])
1068
+
1069
+ dropped = ser .drop (4 , errors = 'ignore' )
1070
+ expected = Index ([1 , 2 , 3 ])
1071
+ self .assert_index_equal (dropped , expected )
1072
+
1073
+ dropped = ser .drop ([3 , 4 , 5 ], errors = 'ignore' )
1074
+ expected = Index ([1 , 2 ])
1075
+ self .assert_index_equal (dropped , expected )
1053
1076
1054
1077
def test_tuple_union_bug (self ):
1055
1078
import pandas
@@ -3529,21 +3552,50 @@ def test_drop(self):
3529
3552
dropped2 = self .index .drop (index )
3530
3553
3531
3554
expected = self .index [[0 , 2 , 3 , 5 ]]
3532
- self .assertTrue (dropped . equals ( expected ) )
3533
- self .assertTrue (dropped2 . equals ( expected ) )
3555
+ self .assert_index_equal (dropped , expected )
3556
+ self .assert_index_equal (dropped2 , expected )
3534
3557
3535
3558
dropped = self .index .drop (['bar' ])
3536
3559
expected = self .index [[0 , 1 , 3 , 4 , 5 ]]
3537
- self .assertTrue (dropped .equals (expected ))
3560
+ self .assert_index_equal (dropped , expected )
3561
+
3562
+ dropped = self .index .drop ('foo' )
3563
+ expected = self .index [[2 , 3 , 4 , 5 ]]
3564
+ self .assert_index_equal (dropped , expected )
3538
3565
3539
3566
index = MultiIndex .from_tuples ([('bar' , 'two' )])
3540
3567
self .assertRaises (KeyError , self .index .drop , [('bar' , 'two' )])
3541
3568
self .assertRaises (KeyError , self .index .drop , index )
3569
+ self .assertRaises (KeyError , self .index .drop , ['foo' , 'two' ])
3570
+
3571
+ # partially correct argument
3572
+ mixed_index = MultiIndex .from_tuples ([('qux' , 'one' ), ('bar' , 'two' )])
3573
+ self .assertRaises (KeyError , self .index .drop , mixed_index )
3574
+
3575
+ # error='ignore'
3576
+ dropped = self .index .drop (index , errors = 'ignore' )
3577
+ expected = self .index [[0 , 1 , 2 , 3 , 4 , 5 ]]
3578
+ self .assert_index_equal (dropped , expected )
3579
+
3580
+ dropped = self .index .drop (mixed_index , errors = 'ignore' )
3581
+ expected = self .index [[0 , 1 , 2 , 3 , 5 ]]
3582
+ self .assert_index_equal (dropped , expected )
3583
+
3584
+ dropped = self .index .drop (['foo' , 'two' ], errors = 'ignore' )
3585
+ expected = self .index [[2 , 3 , 4 , 5 ]]
3586
+ self .assert_index_equal (dropped , expected )
3542
3587
3543
3588
# mixed partial / full drop
3544
3589
dropped = self .index .drop (['foo' , ('qux' , 'one' )])
3545
3590
expected = self .index [[2 , 3 , 5 ]]
3546
- self .assertTrue (dropped .equals (expected ))
3591
+ self .assert_index_equal (dropped , expected )
3592
+
3593
+ # mixed partial / full drop / error='ignore'
3594
+ mixed_index = ['foo' , ('qux' , 'one' ), 'two' ]
3595
+ self .assertRaises (KeyError , self .index .drop , mixed_index )
3596
+ dropped = self .index .drop (mixed_index , errors = 'ignore' )
3597
+ expected = self .index [[2 , 3 , 5 ]]
3598
+ self .assert_index_equal (dropped , expected )
3547
3599
3548
3600
def test_droplevel_with_names (self ):
3549
3601
index = self .index [self .index .get_loc ('foo' )]
0 commit comments