@@ -742,26 +742,6 @@ def test_iloc_mask(self):
742
742
result = df .iloc [np .array ([True ] * len (mask ), dtype = bool )]
743
743
tm .assert_frame_equal (result , df )
744
744
745
- result = df .iloc [np .array ([True , False , True , False , True ], dtype = bool )]
746
- tm .assert_frame_equal (
747
- result , DataFrame ({"a" : [0 , 2 , 4 ]}, index = ["A" , "C" , "E" ])
748
- )
749
-
750
- # series (index does not match)
751
- msg = "Unalignable boolean Series provided as indexer"
752
- with pytest .raises (IndexingError , match = msg ):
753
- df .iloc [Series ([True ] * len (mask ), dtype = bool )]
754
-
755
- df = DataFrame (list (range (5 )), columns = ["a" ])
756
-
757
- result = df .iloc [Series ([True ] * len (mask ), dtype = bool )]
758
- tm .assert_frame_equal (result , df )
759
-
760
- result = df .iloc [Series ([True , False , True , False , True ], dtype = bool )]
761
- tm .assert_frame_equal (result , DataFrame ({"a" : [0 , 2 , 4 ]}, index = [0 , 2 , 4 ]))
762
-
763
- df = DataFrame (list (range (5 )), index = list ("ABCDE" ), columns = ["a" ])
764
-
765
745
# the possibilities
766
746
locs = np .arange (4 )
767
747
nums = 2 ** locs
@@ -817,6 +797,32 @@ def test_iloc_mask(self):
817
797
# For error messages, substring match is sufficient
818
798
assert expected_result in answer , f"[{ key } ] not found in [{ answer } ]"
819
799
800
+ def test_iloc_with_numpy_bool_array (self ):
801
+ df = DataFrame (list (range (5 )), index = list ("ABCDE" ), columns = ["a" ])
802
+ result = df .iloc [np .array ([True , False , True , False , True ], dtype = bool )]
803
+ expected = DataFrame ({"a" : [0 , 2 , 4 ]}, index = ["A" , "C" , "E" ])
804
+ tm .assert_frame_equal (result , expected )
805
+
806
+ def test_iloc_series_mask_with_index_mismatch_raises (self ):
807
+ df = DataFrame (list (range (5 )), index = list ("ABCDE" ), columns = ["a" ])
808
+ mask = df .a % 2 == 0
809
+ msg = "Unalignable boolean Series provided as indexer"
810
+ with pytest .raises (IndexingError , match = msg ):
811
+ df .iloc [Series ([True ] * len (mask ), dtype = bool )]
812
+
813
+ def test_iloc_series_mask_all_true (self ):
814
+ df = DataFrame (list (range (5 )), columns = ["a" ])
815
+ mask = Series ([True ] * len (df ), dtype = bool )
816
+ result = df .iloc [mask ]
817
+ tm .assert_frame_equal (result , df )
818
+
819
+ def test_iloc_series_mask_alternate_true (self ):
820
+ df = DataFrame (list (range (5 )), columns = ["a" ])
821
+ mask = Series ([True , False , True , False , True ], dtype = bool )
822
+ result = df .iloc [mask ]
823
+ expected = DataFrame ({"a" : [0 , 2 , 4 ]}, index = [0 , 2 , 4 ])
824
+ tm .assert_frame_equal (result , expected )
825
+
820
826
def test_iloc_non_unique_indexing (self ):
821
827
# GH 4017, non-unique indexing (on the axis)
822
828
df = DataFrame ({"A" : [0.1 ] * 3000 , "B" : [1 ] * 3000 })
0 commit comments