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