@@ -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,49 @@ 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
+ result = index .intersection (other )
916
+ tm .assert_index_equal (result , expected )
917
+
896
918
def test_difference (self , closed ):
897
919
index = self .create_index (closed = closed )
898
920
tm .assert_index_equal (index .difference (index [:1 ]), index [1 :])
899
921
922
+ # GH 19101: empty result, same dtype
923
+ result = index .difference (index )
924
+ expected = IntervalIndex (np .array ([], dtype = 'int64' ), closed = closed )
925
+ tm .assert_index_equal (result , expected )
926
+
927
+ # GH 19101: empty result, different dtypes
928
+ other = IntervalIndex .from_arrays (index .left .astype ('float64' ),
929
+ index .right , closed = closed )
930
+ result = index .difference (other )
931
+ tm .assert_index_equal (result , expected )
932
+
900
933
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 ]])
934
+ index = self .create_index (closed = closed )
935
+ result = index [1 :].symmetric_difference (index [:- 1 ])
936
+ expected = IntervalIndex ([index [0 ], index [- 1 ]])
937
+ tm .assert_index_equal (result , expected )
938
+
939
+ # GH 19101: empty result, same dtype
940
+ result = index .symmetric_difference (index )
941
+ expected = IntervalIndex (np .array ([], dtype = 'int64' ), closed = closed )
942
+ tm .assert_index_equal (result , expected )
943
+
944
+ # GH 19101: empty result, different dtypes
945
+ other = IntervalIndex .from_arrays (index .left .astype ('float64' ),
946
+ index .right , closed = closed )
947
+ result = index .symmetric_difference (other )
948
+ expected = IntervalIndex (np .array ([], dtype = 'float64' ), closed = closed )
904
949
tm .assert_index_equal (result , expected )
905
950
906
951
@pytest .mark .parametrize ('op_name' , [
@@ -909,17 +954,29 @@ def test_set_operation_errors(self, closed, op_name):
909
954
index = self .create_index (closed = closed )
910
955
set_op = getattr (index , op_name )
911
956
912
- # test errors
957
+ # non-IntervalIndex
913
958
msg = ('can only do set operations between two IntervalIndex objects '
914
959
'that are closed on the same side' )
915
960
with tm .assert_raises_regex (ValueError , msg ):
916
961
set_op (Index ([1 , 2 , 3 ]))
917
962
963
+ # mixed closed
918
964
for other_closed in {'right' , 'left' , 'both' , 'neither' } - {closed }:
919
965
other = self .create_index (closed = other_closed )
920
966
with tm .assert_raises_regex (ValueError , msg ):
921
967
set_op (other )
922
968
969
+ # GH 19016: incompatible dtypes
970
+ other = interval_range (Timestamp ('20180101' ), periods = 9 , closed = closed )
971
+ if op_name in ('union' , 'symmetric_difference' ):
972
+ msg = ('can only do {op} between two IntervalIndex objects '
973
+ 'that have compatible dtypes' ).format (op = op_name )
974
+ with tm .assert_raises_regex (TypeError , msg ):
975
+ set_op (other )
976
+ else :
977
+ # should not raise
978
+ set_op (other )
979
+
923
980
def test_isin (self , closed ):
924
981
index = self .create_index (closed = closed )
925
982
0 commit comments