13
13
from pandas .util .testing import assert_frame_equal
14
14
15
15
16
+ def test_filter_deprecated (int_frame ):
17
+ with tm .assert_produces_warning (FutureWarning ):
18
+ int_frame .filter (like = 'A' )
19
+
20
+
16
21
class TestDataFrameSelectReindex (TestData ):
17
22
# These are specific reindex-based tests; other indexing tests should go in
18
23
# test_indexing
@@ -775,93 +780,105 @@ def test_align_series_combinations(self):
775
780
tm .assert_series_equal (res1 , exp2 )
776
781
tm .assert_frame_equal (res2 , exp1 )
777
782
778
- def test_filter (self ):
783
+ def test_select (self ):
779
784
# Items
780
- filtered = self .frame .filter (['A' , 'B' , 'E' ])
781
- assert len (filtered .columns ) == 2
782
- assert 'E' not in filtered
785
+ selected = self .frame .select (['A' , 'B' , 'E' ])
786
+ assert len (selected .columns ) == 2
787
+ assert 'E' not in selected
783
788
784
- filtered = self .frame .filter (['A' , 'B' , 'E' ], axis = 'columns' )
785
- assert len (filtered .columns ) == 2
786
- assert 'E' not in filtered
789
+ selected = self .frame .select (['A' , 'B' , 'E' ], axis = 'columns' )
790
+ assert len (selected .columns ) == 2
791
+ assert 'E' not in selected
787
792
788
793
# Other axis
789
794
idx = self .frame .index [0 :4 ]
790
- filtered = self .frame .filter (idx , axis = 'index' )
795
+ selected = self .frame .select (idx , axis = 'index' )
791
796
expected = self .frame .reindex (index = idx )
792
- tm .assert_frame_equal (filtered , expected )
797
+ tm .assert_frame_equal (selected , expected )
793
798
794
799
# like
795
800
fcopy = self .frame .copy ()
796
801
fcopy ['AA' ] = 1
797
802
798
- filtered = fcopy .filter (like = 'A' )
799
- assert len (filtered .columns ) == 2
800
- assert 'AA' in filtered
803
+ selected = fcopy .select (like = 'A' )
804
+ assert len (selected .columns ) == 2
805
+ assert 'AA' in selected
801
806
802
807
# like with ints in column names
803
808
df = DataFrame (0. , index = [0 , 1 , 2 ], columns = [0 , 1 , '_A' , '_B' ])
804
- filtered = df .filter (like = '_' )
805
- assert len (filtered .columns ) == 2
809
+ selected = df .select (like = '_' )
810
+ assert len (selected .columns ) == 2
806
811
807
812
# regex with ints in column names
808
813
# from PR #10384
809
814
df = DataFrame (0. , index = [0 , 1 , 2 ], columns = ['A1' , 1 , 'B' , 2 , 'C' ])
810
815
expected = DataFrame (
811
816
0. , index = [0 , 1 , 2 ], columns = pd .Index ([1 , 2 ], dtype = object ))
812
- filtered = df .filter (regex = '^[0-9]+$' )
813
- tm .assert_frame_equal (filtered , expected )
817
+ selected = df .select (regex = '^[0-9]+$' )
818
+ tm .assert_frame_equal (selected , expected )
814
819
815
820
expected = DataFrame (0. , index = [0 , 1 , 2 ], columns = [0 , '0' , 1 , '1' ])
816
821
# shouldn't remove anything
817
- filtered = expected .filter (regex = '^[0-9]+$' )
818
- tm .assert_frame_equal (filtered , expected )
822
+ selected = expected .select (regex = '^[0-9]+$' )
823
+ tm .assert_frame_equal (selected , expected )
819
824
820
825
# pass in None
821
826
with pytest .raises (TypeError , match = 'Must pass' ):
822
- self .frame .filter ()
827
+ self .frame .select ()
823
828
with pytest .raises (TypeError , match = 'Must pass' ):
824
- self .frame .filter (items = None )
829
+ self .frame .select (items = None )
825
830
with pytest .raises (TypeError , match = 'Must pass' ):
826
- self .frame .filter (axis = 1 )
831
+ self .frame .select (axis = 1 )
827
832
828
833
# test mutually exclusive arguments
829
834
with pytest .raises (TypeError , match = 'mutually exclusive' ):
830
- self .frame .filter (items = ['one' , 'three' ], regex = 'e$' , like = 'bbi' )
835
+ self .frame .select (items = ['one' , 'three' ], regex = 'e$' , like = 'bbi' )
831
836
with pytest .raises (TypeError , match = 'mutually exclusive' ):
832
- self .frame .filter (items = ['one' , 'three' ], regex = 'e$' , axis = 1 )
837
+ self .frame .select (items = ['one' , 'three' ], regex = 'e$' , axis = 1 )
833
838
with pytest .raises (TypeError , match = 'mutually exclusive' ):
834
- self .frame .filter (items = ['one' , 'three' ], regex = 'e$' )
839
+ self .frame .select (items = ['one' , 'three' ], regex = 'e$' )
835
840
with pytest .raises (TypeError , match = 'mutually exclusive' ):
836
- self .frame .filter (items = ['one' , 'three' ], like = 'bbi' , axis = 0 )
841
+ self .frame .select (items = ['one' , 'three' ], like = 'bbi' , axis = 0 )
837
842
with pytest .raises (TypeError , match = 'mutually exclusive' ):
838
- self .frame .filter (items = ['one' , 'three' ], like = 'bbi' )
843
+ self .frame .select (items = ['one' , 'three' ], like = 'bbi' )
839
844
840
845
# objects
841
- filtered = self .mixed_frame .filter (like = 'foo' )
842
- assert 'foo' in filtered
846
+ selected = self .mixed_frame .select (like = 'foo' )
847
+ assert 'foo' in selected
843
848
844
849
# unicode columns, won't ascii-encode
845
850
df = self .frame .rename (columns = {'B' : '\u2202 ' })
846
- filtered = df .filter (like = 'C' )
847
- assert 'C' in filtered
851
+ selected = df .select (like = 'C' )
852
+ assert 'C' in selected
853
+
854
+ def test_select_regex_search (self ):
855
+ import re
848
856
849
- def test_filter_regex_search (self ):
850
857
fcopy = self .frame .copy ()
851
858
fcopy ['AA' ] = 1
852
859
853
860
# regex
854
- filtered = fcopy .filter (regex = '[A]+' )
855
- assert len (filtered .columns ) == 2
856
- assert 'AA' in filtered
861
+ selected = fcopy .select (regex = '[A]+' )
862
+ assert len (selected .columns ) == 2
863
+ assert 'AA' in selected
864
+
865
+ # regex, ignore case
866
+ selected = fcopy .select (regex = '[a]+' , flags = re .IGNORECASE )
867
+ assert len (selected .columns ) == 2
868
+ assert 'AA' in selected
857
869
858
870
# doesn't have to be at beginning
859
871
df = DataFrame ({'aBBa' : [1 , 2 ],
860
872
'BBaBB' : [1 , 2 ],
861
873
'aCCa' : [1 , 2 ],
862
874
'aCCaBB' : [1 , 2 ]})
863
875
864
- result = df .filter (regex = 'BB' )
876
+ result = df .select (regex = 'BB' )
877
+ exp = df [[x for x in df .columns if 'BB' in x ]]
878
+ assert_frame_equal (result , exp )
879
+
880
+ # ignore case
881
+ result = df .select (regex = 'bb' , flags = re .IGNORECASE )
865
882
exp = df [[x for x in df .columns if 'BB' in x ]]
866
883
assert_frame_equal (result , exp )
867
884
@@ -870,29 +887,29 @@ def test_filter_regex_search(self):
870
887
('a' , DataFrame ({'a' : [1 , 2 ]})),
871
888
('あ' , DataFrame ({'あ' : [3 , 4 ]}))
872
889
])
873
- def test_filter_unicode (self , name , expected ):
890
+ def test_select_unicode (self , name , expected ):
874
891
# GH13101
875
892
df = DataFrame ({'a' : [1 , 2 ], 'あ' : [3 , 4 ]})
876
893
877
- assert_frame_equal (df .filter (like = name ), expected )
878
- assert_frame_equal (df .filter (regex = name ), expected )
894
+ assert_frame_equal (df .select (like = name ), expected )
895
+ assert_frame_equal (df .select (regex = name ), expected )
879
896
880
897
@pytest .mark .parametrize ('name' , ['a' , 'a' ])
881
- def test_filter_bytestring (self , name ):
898
+ def test_select_bytestring (self , name ):
882
899
# GH13101
883
900
df = DataFrame ({b'a' : [1 , 2 ], b'b' : [3 , 4 ]})
884
901
expected = DataFrame ({b'a' : [1 , 2 ]})
885
902
886
- assert_frame_equal (df .filter (like = name ), expected )
887
- assert_frame_equal (df .filter (regex = name ), expected )
903
+ assert_frame_equal (df .select (like = name ), expected )
904
+ assert_frame_equal (df .select (regex = name ), expected )
888
905
889
- def test_filter_corner (self ):
906
+ def test_select_corner (self ):
890
907
empty = DataFrame ()
891
908
892
- result = empty .filter ([])
909
+ result = empty .select ([])
893
910
assert_frame_equal (result , empty )
894
911
895
- result = empty .filter (like = 'foo' )
912
+ result = empty .select (like = 'foo' )
896
913
assert_frame_equal (result , empty )
897
914
898
915
def test_take (self ):
0 commit comments