@@ -880,6 +880,16 @@ def test_union(self, closed):
880
880
tm .assert_index_equal (index .union (index ), index )
881
881
tm .assert_index_equal (index .union (index [:1 ]), index )
882
882
883
+ # GH 19101: empty result, same dtype
884
+ index = IntervalIndex (np .array ([], dtype = 'int64' ), closed = closed )
885
+ result = index .union (index )
886
+ tm .assert_index_equal (result , index )
887
+
888
+ # GH 19101: empty result, different dtypes
889
+ other = IntervalIndex (np .array ([], dtype = 'float64' ), closed = closed )
890
+ result = index .union (other )
891
+ tm .assert_index_equal (result , other )
892
+
883
893
def test_intersection (self , closed ):
884
894
index = self .create_index (closed = closed )
885
895
other = IntervalIndex .from_breaks (range (5 , 13 ), closed = closed )
@@ -893,14 +903,51 @@ def test_intersection(self, closed):
893
903
894
904
tm .assert_index_equal (index .intersection (index ), index )
895
905
906
+ # GH 19101: empty result, same dtype
907
+ other = IntervalIndex .from_breaks (range (300 , 314 ), closed = closed )
908
+ expected = IntervalIndex (np .array ([], dtype = 'int64' ), closed = closed )
909
+ result = index .intersection (other )
910
+ tm .assert_index_equal (result , expected )
911
+
912
+ # GH 19101: empty result, different dtypes
913
+ breaks = np .arange (300 , 314 , dtype = 'float64' )
914
+ other = IntervalIndex .from_breaks (breaks , closed = closed )
915
+ expected = IntervalIndex (np .array ([], dtype = 'float64' ), closed = closed )
916
+ result = index .intersection (other )
917
+ tm .assert_index_equal (result , expected )
918
+
896
919
def test_difference (self , closed ):
897
920
index = self .create_index (closed = closed )
898
921
tm .assert_index_equal (index .difference (index [:1 ]), index [1 :])
899
922
923
+ # GH 19101: empty result, same dtype
924
+ result = index .difference (index )
925
+ expected = IntervalIndex (np .array ([], dtype = 'int64' ), closed = closed )
926
+ tm .assert_index_equal (result , expected )
927
+
928
+ # GH 19101: empty result, different dtypes
929
+ other = IntervalIndex .from_arrays (index .left .astype ('float64' ),
930
+ index .right , closed = closed )
931
+ result = index .difference (other )
932
+ expected = IntervalIndex (np .array ([], dtype = 'float64' ), closed = closed )
933
+ tm .assert_index_equal (result , expected )
934
+
900
935
def test_symmetric_difference (self , closed ):
901
- idx = self .create_index (closed = closed )
902
- result = idx [1 :].symmetric_difference (idx [:- 1 ])
903
- expected = IntervalIndex ([idx [0 ], idx [- 1 ]])
936
+ index = self .create_index (closed = closed )
937
+ result = index [1 :].symmetric_difference (index [:- 1 ])
938
+ expected = IntervalIndex ([index [0 ], index [- 1 ]])
939
+ tm .assert_index_equal (result , expected )
940
+
941
+ # GH 19101: empty result, same dtype
942
+ result = index .symmetric_difference (index )
943
+ expected = IntervalIndex (np .array ([], dtype = 'int64' ), closed = closed )
944
+ tm .assert_index_equal (result , expected )
945
+
946
+ # GH 19101: empty result, different dtypes
947
+ other = IntervalIndex .from_arrays (index .left .astype ('float64' ),
948
+ index .right , closed = closed )
949
+ result = index .symmetric_difference (other )
950
+ expected = IntervalIndex (np .array ([], dtype = 'float64' ), closed = closed )
904
951
tm .assert_index_equal (result , expected )
905
952
906
953
@pytest .mark .parametrize ('op_name' , [
@@ -909,17 +956,25 @@ def test_set_operation_errors(self, closed, op_name):
909
956
index = self .create_index (closed = closed )
910
957
set_op = getattr (index , op_name )
911
958
912
- # test errors
959
+ # non-IntervalIndex
913
960
msg = ('can only do set operations between two IntervalIndex objects '
914
961
'that are closed on the same side' )
915
962
with tm .assert_raises_regex (ValueError , msg ):
916
963
set_op (Index ([1 , 2 , 3 ]))
917
964
965
+ # mixed closed
918
966
for other_closed in {'right' , 'left' , 'both' , 'neither' } - {closed }:
919
967
other = self .create_index (closed = other_closed )
920
968
with tm .assert_raises_regex (ValueError , msg ):
921
969
set_op (other )
922
970
971
+ # GH 19016: incompatible dtypes
972
+ other = interval_range (Timestamp ('20180101' ), periods = 9 , closed = closed )
973
+ msg = ('can only do set operations between two IntervalIndex objects '
974
+ 'that have compatible dtypes' )
975
+ with tm .assert_raises_regex (TypeError , msg ):
976
+ set_op (other )
977
+
923
978
def test_isin (self , closed ):
924
979
index = self .create_index (closed = closed )
925
980
0 commit comments