@@ -251,6 +251,129 @@ 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
+ elif isinstance (idx , TimedeltaIndex ):
312
+ # checked by tdi._is_convertible_to_index
313
+ pass
314
+ else :
315
+ result = first .union (case )
316
+ self .assertTrue (tm .equalContents (result , everything ))
317
+
318
+ def test_difference_base (self ):
319
+ for name , idx in compat .iteritems (self .indices ):
320
+ first = idx [2 :]
321
+ second = idx [:4 ]
322
+ answer = idx [4 :]
323
+ result = first .difference (second )
324
+
325
+ if isinstance (idx , CategoricalIndex ):
326
+ pass
327
+ else :
328
+ self .assertTrue (tm .equalContents (result , answer ))
329
+
330
+ # GH 10149
331
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
332
+ for case in cases :
333
+ if isinstance (idx , PeriodIndex ):
334
+ msg = "can only call with other PeriodIndex-ed objects"
335
+ with tm .assertRaisesRegexp (ValueError , msg ):
336
+ result = first .difference (case )
337
+ elif isinstance (idx , MultiIndex ):
338
+ pass
339
+ elif isinstance (idx , CategoricalIndex ):
340
+ pass
341
+ elif isinstance (idx , TimedeltaIndex ):
342
+ pass
343
+ elif isinstance (idx , DatetimeIndex ):
344
+ # freq is not preserved even if possible
345
+ self .assert_numpy_array_equal (result .asi8 , answer .asi8 )
346
+ else :
347
+ result = first .difference (case )
348
+ self .assertTrue (tm .equalContents (result , answer ))
349
+
350
+ def test_symmetric_diff (self ):
351
+ for name , idx in compat .iteritems (self .indices ):
352
+ first = idx [1 :]
353
+ second = idx [:- 1 ]
354
+ if isinstance (idx , CategoricalIndex ):
355
+ pass
356
+ else :
357
+ answer = idx [[0 , - 1 ]]
358
+ result = first .sym_diff (second )
359
+ self .assertTrue (tm .equalContents (result , answer ))
360
+
361
+ # GH 10149
362
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
363
+ for case in cases :
364
+ if isinstance (idx , PeriodIndex ):
365
+ msg = "can only call with other PeriodIndex-ed objects"
366
+ with tm .assertRaisesRegexp (ValueError , msg ):
367
+ result = first .sym_diff (case )
368
+ elif isinstance (idx , MultiIndex ):
369
+ pass
370
+ elif isinstance (idx , CategoricalIndex ):
371
+ pass
372
+ else :
373
+ result = first .sym_diff (case )
374
+ self .assertTrue (tm .equalContents (result , answer ))
375
+
376
+
254
377
class TestIndex (Base , tm .TestCase ):
255
378
_holder = Index
256
379
_multiprocess_can_split_ = True
@@ -620,16 +743,12 @@ def test_intersection(self):
620
743
first = self .strIndex [:20 ]
621
744
second = self .strIndex [:10 ]
622
745
intersect = first .intersection (second )
623
-
624
746
self .assertTrue (tm .equalContents (intersect , second ))
625
747
626
748
# Corner cases
627
749
inter = first .intersection (first )
628
750
self .assertIs (inter , first )
629
751
630
- # non-iterable input
631
- assertRaisesRegexp (TypeError , "iterable" , first .intersection , 0.5 )
632
-
633
752
idx1 = Index ([1 , 2 , 3 , 4 , 5 ], name = 'idx' )
634
753
# if target has the same name, it is preserved
635
754
idx2 = Index ([3 , 4 , 5 , 6 , 7 ], name = 'idx' )
@@ -671,6 +790,12 @@ def test_union(self):
671
790
union = first .union (second )
672
791
self .assertTrue (tm .equalContents (union , everything ))
673
792
793
+ # GH 10149
794
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
795
+ for case in cases :
796
+ result = first .union (case )
797
+ self .assertTrue (tm .equalContents (result , everything ))
798
+
674
799
# Corner cases
675
800
union = first .union (first )
676
801
self .assertIs (union , first )
@@ -681,9 +806,6 @@ def test_union(self):
681
806
union = Index ([]).union (first )
682
807
self .assertIs (union , first )
683
808
684
- # non-iterable input
685
- assertRaisesRegexp (TypeError , "iterable" , first .union , 0.5 )
686
-
687
809
# preserve names
688
810
first .name = 'A'
689
811
second .name = 'A'
@@ -792,11 +914,7 @@ def test_difference(self):
792
914
self .assertEqual (len (result ), 0 )
793
915
self .assertEqual (result .name , first .name )
794
916
795
- # non-iterable input
796
- assertRaisesRegexp (TypeError , "iterable" , first .difference , 0.5 )
797
-
798
917
def test_symmetric_diff (self ):
799
-
800
918
# smoke
801
919
idx1 = Index ([1 , 2 , 3 , 4 ], name = 'idx1' )
802
920
idx2 = Index ([2 , 3 , 4 , 5 ])
@@ -842,10 +960,6 @@ def test_symmetric_diff(self):
842
960
self .assertTrue (tm .equalContents (result , expected ))
843
961
self .assertEqual (result .name , 'new_name' )
844
962
845
- # other isn't iterable
846
- with tm .assertRaises (TypeError ):
847
- Index (idx1 ,dtype = 'object' ).difference (1 )
848
-
849
963
def test_is_numeric (self ):
850
964
self .assertFalse (self .dateIndex .is_numeric ())
851
965
self .assertFalse (self .strIndex .is_numeric ())
@@ -1786,6 +1900,7 @@ def test_equals(self):
1786
1900
self .assertFalse (CategoricalIndex (list ('aabca' ) + [np .nan ],categories = ['c' ,'a' ,'b' ,np .nan ]).equals (list ('aabca' )))
1787
1901
self .assertTrue (CategoricalIndex (list ('aabca' ) + [np .nan ],categories = ['c' ,'a' ,'b' ,np .nan ]).equals (list ('aabca' ) + [np .nan ]))
1788
1902
1903
+
1789
1904
class Numeric (Base ):
1790
1905
1791
1906
def test_numeric_compat (self ):
@@ -2642,6 +2757,36 @@ def test_time_overflow_for_32bit_machines(self):
2642
2757
idx2 = pd .date_range (end = '2000' , periods = periods , freq = 'S' )
2643
2758
self .assertEqual (len (idx2 ), periods )
2644
2759
2760
+ def test_intersection (self ):
2761
+ first = self .index
2762
+ second = self .index [5 :]
2763
+ intersect = first .intersection (second )
2764
+ self .assertTrue (tm .equalContents (intersect , second ))
2765
+
2766
+ # GH 10149
2767
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
2768
+ for case in cases :
2769
+ result = first .intersection (case )
2770
+ self .assertTrue (tm .equalContents (result , second ))
2771
+
2772
+ third = Index (['a' , 'b' , 'c' ])
2773
+ result = first .intersection (third )
2774
+ expected = pd .Index ([], dtype = object )
2775
+ self .assert_index_equal (result , expected )
2776
+
2777
+ def test_union (self ):
2778
+ first = self .index [:5 ]
2779
+ second = self .index [5 :]
2780
+ everything = self .index
2781
+ union = first .union (second )
2782
+ self .assertTrue (tm .equalContents (union , everything ))
2783
+
2784
+ # GH 10149
2785
+ cases = [klass (second .values ) for klass in [np .array , Series , list ]]
2786
+ for case in cases :
2787
+ result = first .union (case )
2788
+ self .assertTrue (tm .equalContents (result , everything ))
2789
+
2645
2790
2646
2791
class TestPeriodIndex (DatetimeLike , tm .TestCase ):
2647
2792
_holder = PeriodIndex
@@ -2652,7 +2797,7 @@ def setUp(self):
2652
2797
self .setup_indices ()
2653
2798
2654
2799
def create_index (self ):
2655
- return period_range ('20130101' ,periods = 5 ,freq = 'D' )
2800
+ return period_range ('20130101' , periods = 5 , freq = 'D' )
2656
2801
2657
2802
def test_pickle_compat_construction (self ):
2658
2803
pass
0 commit comments