@@ -251,6 +251,124 @@ 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
+ # # non-iterable input
257
+ cases = [0.5 , 'xxx' ]
258
+ methods = [idx .intersection , idx .union , idx .difference , idx .sym_diff ]
259
+
260
+ for method in methods :
261
+ for case in cases :
262
+ assertRaisesRegexp (TypeError ,
263
+ "Input must be Index or array-like" ,
264
+ method , case )
265
+
266
+ def test_intersection_base (self ):
267
+ for name , idx in compat .iteritems (self .indices ):
268
+ first = idx [:5 ]
269
+ second = idx [:3 ]
270
+ intersect = first .intersection (second )
271
+
272
+ if isinstance (idx , CategoricalIndex ):
273
+ pass
274
+ else :
275
+ self .assertTrue (tm .equalContents (intersect , second ))
276
+
277
+ # GH 10149
278
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
279
+ for case in cases :
280
+ if isinstance (idx , PeriodIndex ):
281
+ msg = "can only call with other PeriodIndex-ed objects"
282
+ with tm .assertRaisesRegexp (ValueError , msg ):
283
+ result = first .intersection (case )
284
+ elif isinstance (idx , CategoricalIndex ):
285
+ pass
286
+ elif isinstance (idx , MultiIndex ):
287
+ pass
288
+ else :
289
+ result = first .intersection (case )
290
+ self .assertTrue (tm .equalContents (result , second ))
291
+
292
+ def test_union_base (self ):
293
+ for name , idx in compat .iteritems (self .indices ):
294
+ first = idx [3 :]
295
+ second = idx [:5 ]
296
+ everything = idx
297
+ union = first .union (second )
298
+ self .assertTrue (tm .equalContents (union , everything ))
299
+
300
+ # GH 10149
301
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
302
+ for case in cases :
303
+ if isinstance (idx , PeriodIndex ):
304
+ msg = "can only call with other PeriodIndex-ed objects"
305
+ with tm .assertRaisesRegexp (ValueError , msg ):
306
+ result = first .union (case )
307
+ elif isinstance (idx , MultiIndex ):
308
+ pass
309
+ elif isinstance (idx , CategoricalIndex ):
310
+ pass
311
+ else :
312
+ result = first .union (case )
313
+ self .assertTrue (tm .equalContents (result , everything ))
314
+
315
+ def test_difference_base (self ):
316
+ for name , idx in compat .iteritems (self .indices ):
317
+ first = idx [2 :]
318
+ second = idx [:4 ]
319
+ answer = idx [4 :]
320
+ result = first .difference (second )
321
+
322
+ if isinstance (idx , CategoricalIndex ):
323
+ pass
324
+ else :
325
+ self .assertTrue (tm .equalContents (result , answer ))
326
+
327
+ # GH 10149
328
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
329
+ for case in cases :
330
+ if isinstance (idx , PeriodIndex ):
331
+ msg = "can only call with other PeriodIndex-ed objects"
332
+ with tm .assertRaisesRegexp (ValueError , msg ):
333
+ result = first .difference (case )
334
+ elif isinstance (idx , MultiIndex ):
335
+ pass
336
+ elif isinstance (idx , CategoricalIndex ):
337
+ pass
338
+ elif isinstance (idx , (DatetimeIndex , TimedeltaIndex )):
339
+ self .assertEqual (result .__class__ , answer .__class__ )
340
+ self .assert_numpy_array_equal (result .asi8 , answer .asi8 )
341
+ else :
342
+ result = first .difference (case )
343
+ self .assertTrue (tm .equalContents (result , answer ))
344
+
345
+ def test_symmetric_diff (self ):
346
+ for name , idx in compat .iteritems (self .indices ):
347
+ first = idx [1 :]
348
+ second = idx [:- 1 ]
349
+ if isinstance (idx , CategoricalIndex ):
350
+ pass
351
+ else :
352
+ answer = idx [[0 , - 1 ]]
353
+ result = first .sym_diff (second )
354
+ self .assertTrue (tm .equalContents (result , answer ))
355
+
356
+ # GH 10149
357
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
358
+ for case in cases :
359
+ if isinstance (idx , PeriodIndex ):
360
+ msg = "can only call with other PeriodIndex-ed objects"
361
+ with tm .assertRaisesRegexp (ValueError , msg ):
362
+ result = first .sym_diff (case )
363
+ elif isinstance (idx , MultiIndex ):
364
+ pass
365
+ elif isinstance (idx , CategoricalIndex ):
366
+ pass
367
+ else :
368
+ result = first .sym_diff (case )
369
+ self .assertTrue (tm .equalContents (result , answer ))
370
+
371
+
254
372
class TestIndex (Base , tm .TestCase ):
255
373
_holder = Index
256
374
_multiprocess_can_split_ = True
@@ -620,16 +738,12 @@ def test_intersection(self):
620
738
first = self .strIndex [:20 ]
621
739
second = self .strIndex [:10 ]
622
740
intersect = first .intersection (second )
623
-
624
741
self .assertTrue (tm .equalContents (intersect , second ))
625
742
626
743
# Corner cases
627
744
inter = first .intersection (first )
628
745
self .assertIs (inter , first )
629
746
630
- # non-iterable input
631
- assertRaisesRegexp (TypeError , "iterable" , first .intersection , 0.5 )
632
-
633
747
idx1 = Index ([1 , 2 , 3 , 4 , 5 ], name = 'idx' )
634
748
# if target has the same name, it is preserved
635
749
idx2 = Index ([3 , 4 , 5 , 6 , 7 ], name = 'idx' )
@@ -671,6 +785,12 @@ def test_union(self):
671
785
union = first .union (second )
672
786
self .assertTrue (tm .equalContents (union , everything ))
673
787
788
+ # GH 10149
789
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
790
+ for case in cases :
791
+ result = first .union (case )
792
+ self .assertTrue (tm .equalContents (result , everything ))
793
+
674
794
# Corner cases
675
795
union = first .union (first )
676
796
self .assertIs (union , first )
@@ -681,9 +801,6 @@ def test_union(self):
681
801
union = Index ([]).union (first )
682
802
self .assertIs (union , first )
683
803
684
- # non-iterable input
685
- assertRaisesRegexp (TypeError , "iterable" , first .union , 0.5 )
686
-
687
804
# preserve names
688
805
first .name = 'A'
689
806
second .name = 'A'
@@ -792,11 +909,7 @@ def test_difference(self):
792
909
self .assertEqual (len (result ), 0 )
793
910
self .assertEqual (result .name , first .name )
794
911
795
- # non-iterable input
796
- assertRaisesRegexp (TypeError , "iterable" , first .difference , 0.5 )
797
-
798
912
def test_symmetric_diff (self ):
799
-
800
913
# smoke
801
914
idx1 = Index ([1 , 2 , 3 , 4 ], name = 'idx1' )
802
915
idx2 = Index ([2 , 3 , 4 , 5 ])
@@ -842,10 +955,6 @@ def test_symmetric_diff(self):
842
955
self .assertTrue (tm .equalContents (result , expected ))
843
956
self .assertEqual (result .name , 'new_name' )
844
957
845
- # other isn't iterable
846
- with tm .assertRaises (TypeError ):
847
- Index (idx1 ,dtype = 'object' ).difference (1 )
848
-
849
958
def test_is_numeric (self ):
850
959
self .assertFalse (self .dateIndex .is_numeric ())
851
960
self .assertFalse (self .strIndex .is_numeric ())
@@ -1786,6 +1895,7 @@ def test_equals(self):
1786
1895
self .assertFalse (CategoricalIndex (list ('aabca' ) + [np .nan ],categories = ['c' ,'a' ,'b' ,np .nan ]).equals (list ('aabca' )))
1787
1896
self .assertTrue (CategoricalIndex (list ('aabca' ) + [np .nan ],categories = ['c' ,'a' ,'b' ,np .nan ]).equals (list ('aabca' ) + [np .nan ]))
1788
1897
1898
+
1789
1899
class Numeric (Base ):
1790
1900
1791
1901
def test_numeric_compat (self ):
@@ -2642,6 +2752,36 @@ def test_time_overflow_for_32bit_machines(self):
2642
2752
idx2 = pd .date_range (end = '2000' , periods = periods , freq = 'S' )
2643
2753
self .assertEqual (len (idx2 ), periods )
2644
2754
2755
+ def test_intersection (self ):
2756
+ first = self .index
2757
+ second = self .index [5 :]
2758
+ intersect = first .intersection (second )
2759
+ self .assertTrue (tm .equalContents (intersect , second ))
2760
+
2761
+ # GH 10149
2762
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
2763
+ for case in cases :
2764
+ result = first .intersection (case )
2765
+ self .assertTrue (tm .equalContents (result , second ))
2766
+
2767
+ third = Index (['a' , 'b' , 'c' ])
2768
+ result = first .intersection (third )
2769
+ expected = pd .Index ([], dtype = object )
2770
+ self .assert_index_equal (result , expected )
2771
+
2772
+ def test_union (self ):
2773
+ first = self .index [:5 ]
2774
+ second = self .index [5 :]
2775
+ everything = self .index
2776
+ union = first .union (second )
2777
+ self .assertTrue (tm .equalContents (union , everything ))
2778
+
2779
+ # GH 10149
2780
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
2781
+ for case in cases :
2782
+ result = first .union (case )
2783
+ self .assertTrue (tm .equalContents (result , everything ))
2784
+
2645
2785
2646
2786
class TestPeriodIndex (DatetimeLike , tm .TestCase ):
2647
2787
_holder = PeriodIndex
@@ -2652,7 +2792,7 @@ def setUp(self):
2652
2792
self .setup_indices ()
2653
2793
2654
2794
def create_index (self ):
2655
- return period_range ('20130101' ,periods = 5 ,freq = 'D' )
2795
+ return period_range ('20130101' , periods = 5 , freq = 'D' )
2656
2796
2657
2797
def test_pickle_compat_construction (self ):
2658
2798
pass
0 commit comments