@@ -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,51 +872,68 @@ 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
- union = first .union ([], sort = sort )
896
+ with tm .assert_produces_warning (warning ):
897
+ union = first .union ([], sort = sort )
893
898
assert (union is first ) is (not sort )
894
899
895
- union = Index ([]).union (first , sort = sort )
900
+ with tm .assert_produces_warning (warning ):
901
+ union = Index ([]).union (first , sort = sort )
896
902
assert (union is first ) is (not sort )
897
903
898
904
@pytest .mark .parametrize ("first_list" , [list ('ba' ), list ()])
899
905
@pytest .mark .parametrize ("second_list" , [list ('ab' ), list ()])
900
906
@pytest .mark .parametrize ("first_name, second_name, expected_name" , [
901
907
('A' , 'B' , None ), (None , 'B' , None ), ('A' , None , None )])
902
- @pytest .mark .parametrize ("sort" , [None , False ])
908
+ @pytest .mark .parametrize ("sort" , [None , True , False ])
903
909
def test_union_name_preservation (self , first_list , second_list , first_name ,
904
910
second_name , expected_name , sort ):
905
911
first = Index (first_list , name = first_name )
906
912
second = Index (second_list , name = second_name )
907
- union = first .union (second , sort = sort )
913
+
914
+ warning = FutureWarning if sort is None else None
915
+ with tm .assert_produces_warning (warning ):
916
+ union = first .union (second , sort = sort )
908
917
909
918
vals = set (first_list ).union (second_list )
910
919
911
920
if sort is None and len (first_list ) > 0 and len (second_list ) > 0 :
912
921
expected = Index (sorted (vals ), name = expected_name )
913
922
tm .assert_index_equal (union , expected )
923
+ elif sort :
924
+ expected = Index (sorted (vals ), name = expected_name )
925
+ tm .assert_index_equal (union , expected )
914
926
else :
915
927
expected = Index (vals , name = expected_name )
916
928
assert tm .equalContents (union , expected )
917
929
918
- @pytest .mark .parametrize ("sort" , [None , False ])
930
+ @pytest .mark .parametrize ("sort" , [None , True , False ])
919
931
def test_union_dt_as_obj (self , sort ):
920
932
# TODO: Replace with fixturesult
921
- firstCat = self .strIndex .union (self .dateIndex )
922
- secondCat = self .strIndex .union (self .strIndex )
933
+ warning = FutureWarning if sort is None else None
934
+ with tm .assert_produces_warning (warning , check_stacklevel = False ):
935
+ firstCat = self .strIndex .union (self .dateIndex , sort = sort )
936
+ secondCat = self .strIndex .union (self .strIndex , sort = sort )
923
937
924
938
if self .dateIndex .dtype == np .object_ :
925
939
appended = np .append (self .strIndex , self .dateIndex )
@@ -932,15 +946,6 @@ def test_union_dt_as_obj(self, sort):
932
946
tm .assert_contains_all (self .strIndex , secondCat )
933
947
tm .assert_contains_all (self .dateIndex , firstCat )
934
948
935
- @pytest .mark .parametrize ("method" , ['union' , 'intersection' , 'difference' ,
936
- 'symmetric_difference' ])
937
- def test_setops_disallow_true (self , method ):
938
- idx1 = pd .Index (['a' , 'b' ])
939
- idx2 = pd .Index (['b' , 'c' ])
940
-
941
- with pytest .raises (ValueError , match = "The 'sort' keyword only takes" ):
942
- getattr (idx1 , method )(idx2 , sort = True )
943
-
944
949
def test_map_identity_mapping (self ):
945
950
# GH 12766
946
951
# TODO: replace with fixture
@@ -1707,7 +1712,9 @@ def test_tuple_union_bug(self, method, expected, sort):
1707
1712
(2 , 'B' ), (1 , 'C' ), (2 , 'C' )],
1708
1713
dtype = [('num' , int ), ('let' , 'a1' )]))
1709
1714
1710
- result = getattr (index1 , method )(index2 , sort = sort )
1715
+ warning = FutureWarning if method == 'union' else None
1716
+ with tm .assert_produces_warning (warning ):
1717
+ result = getattr (index1 , method )(index2 , sort = sort )
1711
1718
assert result .ndim == 1
1712
1719
1713
1720
expected = Index (expected )
@@ -1917,12 +1924,14 @@ def test_outer_join_sort(self):
1917
1924
left_index = Index (np .random .permutation (15 ))
1918
1925
right_index = tm .makeDateIndex (10 )
1919
1926
1920
- with tm .assert_produces_warning (RuntimeWarning ):
1927
+ with tm .assert_produces_warning (RuntimeWarning ,
1928
+ raise_on_extra_warnings = False ):
1921
1929
result = left_index .join (right_index , how = 'outer' )
1922
1930
1923
1931
# right_index in this case because DatetimeIndex has join precedence
1924
1932
# over Int64Index
1925
- with tm .assert_produces_warning (RuntimeWarning ):
1933
+ with tm .assert_produces_warning (RuntimeWarning ,
1934
+ raise_on_extra_warnings = False ):
1926
1935
expected = right_index .astype (object ).union (
1927
1936
left_index .astype (object ))
1928
1937
@@ -2233,7 +2242,8 @@ def test_union_base(self):
2233
2242
first = index [3 :]
2234
2243
second = index [:5 ]
2235
2244
2236
- result = first .union (second )
2245
+ with tm .assert_produces_warning (FutureWarning ):
2246
+ result = first .union (second )
2237
2247
2238
2248
expected = Index ([0 , 1 , 2 , 'a' , 'b' , 'c' ])
2239
2249
tm .assert_index_equal (result , expected )
@@ -2246,7 +2256,8 @@ def test_union_different_type_base(self, klass):
2246
2256
first = index [3 :]
2247
2257
second = index [:5 ]
2248
2258
2249
- result = first .union (klass (second .values ))
2259
+ with tm .assert_produces_warning (FutureWarning ):
2260
+ result = first .union (klass (second .values ))
2250
2261
2251
2262
assert tm .equalContents (result , index )
2252
2263
0 commit comments