@@ -790,32 +790,38 @@ def test_intersection_equal_sort_true(self):
790
790
sorted_ = pd .Index (['a' , 'b' , 'c' ])
791
791
tm .assert_index_equal (idx .intersection (idx , sort = True ), sorted_ )
792
792
793
- @pytest .mark .parametrize ("sort" , [None , False ])
793
+ @pytest .mark .parametrize ("sort" , [None , True , False ])
794
794
def test_chained_union (self , sort ):
795
795
# Chained unions handles names correctly
796
796
i1 = Index ([1 , 2 ], name = 'i1' )
797
797
i2 = Index ([5 , 6 ], name = 'i2' )
798
798
i3 = Index ([3 , 4 ], name = 'i3' )
799
- union = i1 .union (i2 .union (i3 , sort = sort ), sort = sort )
800
- expected = i1 .union (i2 , sort = sort ).union (i3 , sort = sort )
799
+
800
+ warning = FutureWarning if sort is None else None
801
+ with tm .assert_produces_warning (warning ):
802
+ union = i1 .union (i2 .union (i3 , sort = sort ), sort = sort )
803
+ expected = i1 .union (i2 , sort = sort ).union (i3 , sort = sort )
801
804
tm .assert_index_equal (union , expected )
802
805
803
806
j1 = Index ([1 , 2 ], name = 'j1' )
804
807
j2 = Index ([], name = 'j2' )
805
808
j3 = Index ([], name = 'j3' )
806
- union = j1 .union (j2 .union (j3 , sort = sort ), sort = sort )
807
- expected = j1 .union (j2 , sort = sort ).union (j3 , sort = sort )
809
+ with tm .assert_produces_warning (warning ):
810
+ union = j1 .union (j2 .union (j3 , sort = sort ), sort = sort )
811
+ expected = j1 .union (j2 , sort = sort ).union (j3 , sort = sort )
808
812
tm .assert_index_equal (union , expected )
809
813
810
- @pytest .mark .parametrize ("sort" , [None , False ])
814
+ @pytest .mark .parametrize ("sort" , [None , True , False ])
811
815
def test_union (self , sort ):
812
816
# TODO: Replace with fixturesult
813
817
first = self .strIndex [5 :20 ]
814
818
second = self .strIndex [:10 ]
815
819
everything = self .strIndex [:20 ]
816
820
817
- union = first .union (second , sort = sort )
818
- if sort is None :
821
+ warning = FutureWarning if sort is None else None
822
+ with tm .assert_produces_warning (warning ):
823
+ union = first .union (second , sort = sort )
824
+ if sort is not False :
819
825
tm .assert_index_equal (union , everything .sort_values ())
820
826
assert tm .equalContents (union , everything )
821
827
@@ -826,21 +832,14 @@ def test_union_sort_other_special(self, slice_):
826
832
idx = pd .Index ([1 , 0 , 2 ])
827
833
# default, sort=None
828
834
other = idx [slice_ ]
829
- tm .assert_index_equal (idx .union (other ), idx )
830
- tm .assert_index_equal (other .union (idx ), idx )
835
+ with tm .assert_produces_warning (FutureWarning ):
836
+ tm .assert_index_equal (idx .union (other ), idx )
837
+ tm .assert_index_equal (other .union (idx ), idx )
831
838
832
839
# sort=False
833
840
tm .assert_index_equal (idx .union (other , sort = False ), idx )
834
841
835
- @pytest .mark .xfail (reason = "Not implemented" )
836
- @pytest .mark .parametrize ('slice_' , [slice (None ), slice (0 )])
837
- def test_union_sort_special_true (self , slice_ ):
838
- # TODO decide on True behaviour
839
842
# sort=True
840
- idx = pd .Index ([1 , 0 , 2 ])
841
- # default, sort=None
842
- other = idx [slice_ ]
843
-
844
843
result = idx .union (other , sort = True )
845
844
expected = pd .Index ([0 , 1 , 2 ])
846
845
tm .assert_index_equal (result , expected )
@@ -849,31 +848,29 @@ def test_union_sort_other_incomparable(self):
849
848
# https://github.com/pandas-dev/pandas/issues/24959
850
849
idx = pd .Index ([1 , pd .Timestamp ('2000' )])
851
850
# default (sort=None)
852
- with tm .assert_produces_warning (RuntimeWarning ):
851
+ with tm .assert_produces_warning (RuntimeWarning ,
852
+ raise_on_extra_warnings = False ):
853
853
result = idx .union (idx [:1 ])
854
-
855
854
tm .assert_index_equal (result , idx )
856
855
857
856
# sort=None
858
- with tm .assert_produces_warning (RuntimeWarning ):
857
+ with tm .assert_produces_warning (RuntimeWarning ,
858
+ raise_on_extra_warnings = False ):
859
859
result = idx .union (idx [:1 ], sort = None )
860
860
tm .assert_index_equal (result , idx )
861
861
862
+ # sort=True
863
+ with tm .assert_produces_warning (RuntimeWarning ):
864
+ result = idx .union (idx [:1 ], sort = True )
865
+ tm .assert_index_equal (result , idx )
866
+
862
867
# sort=False
863
868
result = idx .union (idx [:1 ], sort = False )
864
869
tm .assert_index_equal (result , idx )
865
870
866
- @pytest .mark .xfail (reason = "Not implemented" )
867
- def test_union_sort_other_incomparable_true (self ):
868
- # TODO decide on True behaviour
869
- # sort=True
870
- idx = pd .Index ([1 , pd .Timestamp ('2000' )])
871
- with pytest .raises (TypeError , match = '.*' ):
872
- idx .union (idx [:1 ], sort = True )
873
-
874
871
@pytest .mark .parametrize ("klass" , [
875
872
np .array , Series , list ])
876
- @pytest .mark .parametrize ("sort" , [None , False ])
873
+ @pytest .mark .parametrize ("sort" , [None , True , False ])
877
874
def test_union_from_iterables (self , klass , sort ):
878
875
# GH 10149
879
876
# TODO: Replace with fixturesult
@@ -882,51 +879,68 @@ def test_union_from_iterables(self, klass, sort):
882
879
everything = self .strIndex [:20 ]
883
880
884
881
case = klass (second .values )
885
- result = first .union (case , sort = sort )
886
- if sort is None :
882
+
883
+ warning = FutureWarning if sort is None else None
884
+ with tm .assert_produces_warning (warning ):
885
+ result = first .union (case , sort = sort )
886
+
887
+ if sort is not False :
887
888
tm .assert_index_equal (result , everything .sort_values ())
888
889
assert tm .equalContents (result , everything )
889
890
890
- @pytest .mark .parametrize ("sort" , [None , False ])
891
+ @pytest .mark .parametrize ("sort" , [None , True , False ])
891
892
def test_union_identity (self , sort ):
892
893
# TODO: replace with fixturesult
893
894
first = self .strIndex [5 :20 ]
894
895
895
- union = first .union (first , sort = sort )
896
+ warning = FutureWarning if sort is None else None
897
+ with tm .assert_produces_warning (warning ):
898
+ union = first .union (first , sort = sort )
899
+
896
900
# i.e. identity is not preserved when sort is True
897
901
assert (union is first ) is (not sort )
898
902
899
- union = first .union ([], sort = sort )
903
+ with tm .assert_produces_warning (warning ):
904
+ union = first .union ([], sort = sort )
900
905
assert (union is first ) is (not sort )
901
906
902
- union = Index ([]).union (first , sort = sort )
907
+ with tm .assert_produces_warning (warning ):
908
+ union = Index ([]).union (first , sort = sort )
903
909
assert (union is first ) is (not sort )
904
910
905
911
@pytest .mark .parametrize ("first_list" , [list ('ba' ), list ()])
906
912
@pytest .mark .parametrize ("second_list" , [list ('ab' ), list ()])
907
913
@pytest .mark .parametrize ("first_name, second_name, expected_name" , [
908
914
('A' , 'B' , None ), (None , 'B' , None ), ('A' , None , None )])
909
- @pytest .mark .parametrize ("sort" , [None , False ])
915
+ @pytest .mark .parametrize ("sort" , [None , True , False ])
910
916
def test_union_name_preservation (self , first_list , second_list , first_name ,
911
917
second_name , expected_name , sort ):
912
918
first = Index (first_list , name = first_name )
913
919
second = Index (second_list , name = second_name )
914
- union = first .union (second , sort = sort )
920
+
921
+ warning = FutureWarning if sort is None else None
922
+ with tm .assert_produces_warning (warning ):
923
+ union = first .union (second , sort = sort )
915
924
916
925
vals = set (first_list ).union (second_list )
917
926
918
927
if sort is None and len (first_list ) > 0 and len (second_list ) > 0 :
919
928
expected = Index (sorted (vals ), name = expected_name )
920
929
tm .assert_index_equal (union , expected )
930
+ elif sort :
931
+ expected = Index (sorted (vals ), name = expected_name )
932
+ tm .assert_index_equal (union , expected )
921
933
else :
922
934
expected = Index (vals , name = expected_name )
923
935
assert tm .equalContents (union , expected )
924
936
925
- @pytest .mark .parametrize ("sort" , [None , False ])
937
+ @pytest .mark .parametrize ("sort" , [None , True , False ])
926
938
def test_union_dt_as_obj (self , sort ):
927
939
# TODO: Replace with fixturesult
928
- firstCat = self .strIndex .union (self .dateIndex )
929
- secondCat = self .strIndex .union (self .strIndex )
940
+ warning = FutureWarning if sort is None else None
941
+ with tm .assert_produces_warning (warning , check_stacklevel = False ):
942
+ firstCat = self .strIndex .union (self .dateIndex , sort = sort )
943
+ secondCat = self .strIndex .union (self .strIndex , sort = sort )
930
944
931
945
if self .dateIndex .dtype == np .object_ :
932
946
appended = np .append (self .strIndex , self .dateIndex )
@@ -939,15 +953,6 @@ def test_union_dt_as_obj(self, sort):
939
953
tm .assert_contains_all (self .strIndex , secondCat )
940
954
tm .assert_contains_all (self .dateIndex , firstCat )
941
955
942
- @pytest .mark .parametrize ("method" , ['union' , 'intersection' , 'difference' ,
943
- 'symmetric_difference' ])
944
- def test_setops_disallow_true (self , method ):
945
- idx1 = pd .Index (['a' , 'b' ])
946
- idx2 = pd .Index (['b' , 'c' ])
947
-
948
- with pytest .raises (ValueError , match = "The 'sort' keyword only takes" ):
949
- getattr (idx1 , method )(idx2 , sort = True )
950
-
951
956
def test_map_identity_mapping (self ):
952
957
# GH 12766
953
958
# TODO: replace with fixture
@@ -1714,7 +1719,9 @@ def test_tuple_union_bug(self, method, expected, sort):
1714
1719
(2 , 'B' ), (1 , 'C' ), (2 , 'C' )],
1715
1720
dtype = [('num' , int ), ('let' , 'a1' )]))
1716
1721
1717
- result = getattr (index1 , method )(index2 , sort = sort )
1722
+ warning = FutureWarning if method == 'union' else None
1723
+ with tm .assert_produces_warning (warning ):
1724
+ result = getattr (index1 , method )(index2 , sort = sort )
1718
1725
assert result .ndim == 1
1719
1726
1720
1727
expected = Index (expected )
@@ -1924,12 +1931,14 @@ def test_outer_join_sort(self):
1924
1931
left_index = Index (np .random .permutation (15 ))
1925
1932
right_index = tm .makeDateIndex (10 )
1926
1933
1927
- with tm .assert_produces_warning (RuntimeWarning ):
1934
+ with tm .assert_produces_warning (RuntimeWarning ,
1935
+ raise_on_extra_warnings = False ):
1928
1936
result = left_index .join (right_index , how = 'outer' )
1929
1937
1930
1938
# right_index in this case because DatetimeIndex has join precedence
1931
1939
# over Int64Index
1932
- with tm .assert_produces_warning (RuntimeWarning ):
1940
+ with tm .assert_produces_warning (RuntimeWarning ,
1941
+ raise_on_extra_warnings = False ):
1933
1942
expected = right_index .astype (object ).union (
1934
1943
left_index .astype (object ))
1935
1944
@@ -2240,7 +2249,8 @@ def test_union_base(self):
2240
2249
first = index [3 :]
2241
2250
second = index [:5 ]
2242
2251
2243
- result = first .union (second )
2252
+ with tm .assert_produces_warning (FutureWarning ):
2253
+ result = first .union (second )
2244
2254
2245
2255
expected = Index ([0 , 1 , 2 , 'a' , 'b' , 'c' ])
2246
2256
tm .assert_index_equal (result , expected )
@@ -2253,7 +2263,8 @@ def test_union_different_type_base(self, klass):
2253
2263
first = index [3 :]
2254
2264
second = index [:5 ]
2255
2265
2256
- result = first .union (klass (second .values ))
2266
+ with tm .assert_produces_warning (FutureWarning ):
2267
+ result = first .union (klass (second .values ))
2257
2268
2258
2269
assert tm .equalContents (result , index )
2259
2270
0 commit comments