@@ -251,6 +251,20 @@ def test_take(self):
251
251
expected = ind [indexer ]
252
252
self .assertTrue (result .equals (expected ))
253
253
254
+ def test_setops_errorcases (self ):
255
+ for name , idx in compat .iteritems (self .indices ):
256
+
257
+ # # non-iterable input
258
+ cases = [0.5 , 'xxx' ]
259
+ methods = [idx .intersection , idx .union , idx .difference , idx .sym_diff ]
260
+
261
+ for method in methods :
262
+ for case in cases :
263
+ assertRaisesRegexp (TypeError ,
264
+ "Input must be Index or array-like" ,
265
+ method , case )
266
+
267
+
254
268
class TestIndex (Base , tm .TestCase ):
255
269
_holder = Index
256
270
_multiprocess_can_split_ = True
@@ -620,16 +634,18 @@ def test_intersection(self):
620
634
first = self .strIndex [:20 ]
621
635
second = self .strIndex [:10 ]
622
636
intersect = first .intersection (second )
623
-
624
637
self .assertTrue (tm .equalContents (intersect , second ))
625
638
639
+ # GH 10149
640
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
641
+ for case in cases :
642
+ result = first .intersection (case )
643
+ self .assertTrue (tm .equalContents (result , second ))
644
+
626
645
# Corner cases
627
646
inter = first .intersection (first )
628
647
self .assertIs (inter , first )
629
648
630
- # non-iterable input
631
- assertRaisesRegexp (TypeError , "iterable" , first .intersection , 0.5 )
632
-
633
649
idx1 = Index ([1 , 2 , 3 , 4 , 5 ], name = 'idx' )
634
650
# if target has the same name, it is preserved
635
651
idx2 = Index ([3 , 4 , 5 , 6 , 7 ], name = 'idx' )
@@ -671,6 +687,12 @@ def test_union(self):
671
687
union = first .union (second )
672
688
self .assertTrue (tm .equalContents (union , everything ))
673
689
690
+ # GH 10149
691
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
692
+ for case in cases :
693
+ result = first .union (case )
694
+ self .assertTrue (tm .equalContents (result , everything ))
695
+
674
696
# Corner cases
675
697
union = first .union (first )
676
698
self .assertIs (union , first )
@@ -681,9 +703,6 @@ def test_union(self):
681
703
union = Index ([]).union (first )
682
704
self .assertIs (union , first )
683
705
684
- # non-iterable input
685
- assertRaisesRegexp (TypeError , "iterable" , first .union , 0.5 )
686
-
687
706
# preserve names
688
707
first .name = 'A'
689
708
second .name = 'A'
@@ -777,6 +796,12 @@ def test_difference(self):
777
796
self .assertTrue (tm .equalContents (result , answer ))
778
797
self .assertEqual (result .name , None )
779
798
799
+ # GH 10149
800
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
801
+ for case in cases :
802
+ result = first .difference (case )
803
+ self .assertTrue (tm .equalContents (result , answer ))
804
+
780
805
# same names
781
806
second .name = 'name'
782
807
result = first .difference (second )
@@ -792,9 +817,6 @@ def test_difference(self):
792
817
self .assertEqual (len (result ), 0 )
793
818
self .assertEqual (result .name , first .name )
794
819
795
- # non-iterable input
796
- assertRaisesRegexp (TypeError , "iterable" , first .difference , 0.5 )
797
-
798
820
def test_symmetric_diff (self ):
799
821
800
822
# smoke
@@ -810,6 +832,12 @@ def test_symmetric_diff(self):
810
832
self .assertTrue (tm .equalContents (result , expected ))
811
833
self .assertIsNone (result .name )
812
834
835
+ # GH 10149
836
+ cases = [klass (idx2 .values ) for klass in [np .array , Series , list ]]
837
+ for case in cases :
838
+ result = idx1 .sym_diff (case )
839
+ self .assertTrue (tm .equalContents (result , expected ))
840
+
813
841
# multiIndex
814
842
idx1 = MultiIndex .from_tuples (self .tuples )
815
843
idx2 = MultiIndex .from_tuples ([('foo' , 1 ), ('bar' , 3 )])
@@ -842,10 +870,6 @@ def test_symmetric_diff(self):
842
870
self .assertTrue (tm .equalContents (result , expected ))
843
871
self .assertEqual (result .name , 'new_name' )
844
872
845
- # other isn't iterable
846
- with tm .assertRaises (TypeError ):
847
- Index (idx1 ,dtype = 'object' ).difference (1 )
848
-
849
873
def test_is_numeric (self ):
850
874
self .assertFalse (self .dateIndex .is_numeric ())
851
875
self .assertFalse (self .strIndex .is_numeric ())
@@ -2642,6 +2666,36 @@ def test_time_overflow_for_32bit_machines(self):
2642
2666
idx2 = pd .date_range (end = '2000' , periods = periods , freq = 'S' )
2643
2667
self .assertEqual (len (idx2 ), periods )
2644
2668
2669
+ def test_intersection (self ):
2670
+ first = self .index
2671
+ second = self .index [5 :]
2672
+ intersect = first .intersection (second )
2673
+ self .assertTrue (tm .equalContents (intersect , second ))
2674
+
2675
+ # GH 10149
2676
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
2677
+ for case in cases :
2678
+ result = first .intersection (case )
2679
+ self .assertTrue (tm .equalContents (result , second ))
2680
+
2681
+ third = Index (['a' , 'b' , 'c' ])
2682
+ result = first .intersection (third )
2683
+ expected = pd .Index ([], dtype = object )
2684
+ self .assert_index_equal (result , expected )
2685
+
2686
+ def test_union (self ):
2687
+ first = self .index [:5 ]
2688
+ second = self .index [5 :]
2689
+ everything = self .index
2690
+ union = first .union (second )
2691
+ self .assertTrue (tm .equalContents (union , everything ))
2692
+
2693
+ # GH 10149
2694
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
2695
+ for case in cases :
2696
+ result = first .union (case )
2697
+ self .assertTrue (tm .equalContents (result , everything ))
2698
+
2645
2699
2646
2700
class TestPeriodIndex (DatetimeLike , tm .TestCase ):
2647
2701
_holder = PeriodIndex
0 commit comments