@@ -21,21 +21,23 @@ class TestDatetimeIndexSetOps(object):
21
21
'dateutil/US/Pacific' ]
22
22
23
23
# TODO: moved from test_datetimelike; dedup with version below
24
- def test_union2 (self ):
24
+ @pytest .mark .parametrize ("sort" , [None , False ])
25
+ def test_union2 (self , sort ):
25
26
everything = tm .makeDateIndex (10 )
26
27
first = everything [:5 ]
27
28
second = everything [5 :]
28
- union = first .union (second )
29
- assert tm .equalContents (union , everything )
29
+ union = first .union (second , sort = sort )
30
+ tm .assert_index_equal (union , everything )
30
31
31
32
# GH 10149
32
33
cases = [klass (second .values ) for klass in [np .array , Series , list ]]
33
34
for case in cases :
34
- result = first .union (case )
35
- assert tm .equalContents (result , everything )
35
+ result = first .union (case , sort = sort )
36
+ tm .assert_index_equal (result , everything )
36
37
37
38
@pytest .mark .parametrize ("tz" , tz )
38
- def test_union (self , tz ):
39
+ @pytest .mark .parametrize ("sort" , [None , False ])
40
+ def test_union (self , tz , sort ):
39
41
rng1 = pd .date_range ('1/1/2000' , freq = 'D' , periods = 5 , tz = tz )
40
42
other1 = pd .date_range ('1/6/2000' , freq = 'D' , periods = 5 , tz = tz )
41
43
expected1 = pd .date_range ('1/1/2000' , freq = 'D' , periods = 10 , tz = tz )
@@ -52,52 +54,68 @@ def test_union(self, tz):
52
54
(rng2 , other2 , expected2 ),
53
55
(rng3 , other3 , expected3 )]:
54
56
55
- result_union = rng .union (other )
57
+ result_union = rng .union (other , sort = sort )
56
58
tm .assert_index_equal (result_union , expected )
57
59
58
- def test_union_coverage (self ):
60
+ result_union = other .union (rng , sort = sort )
61
+ if sort is None :
62
+ tm .assert_index_equal (result_union , expected )
63
+ else :
64
+ assert tm .equalContents (result_union , expected )
65
+
66
+ @pytest .mark .parametrize ("sort" , [None , False ])
67
+ def test_union_coverage (self , sort ):
59
68
idx = DatetimeIndex (['2000-01-03' , '2000-01-01' , '2000-01-02' ])
60
69
ordered = DatetimeIndex (idx .sort_values (), freq = 'infer' )
61
- result = ordered .union (idx )
70
+ result = ordered .union (idx , sort = sort )
62
71
tm .assert_index_equal (result , ordered )
63
72
64
- result = ordered [:0 ].union (ordered )
73
+ result = ordered [:0 ].union (ordered , sort = sort )
65
74
tm .assert_index_equal (result , ordered )
66
75
assert result .freq == ordered .freq
67
76
68
- def test_union_bug_1730 (self ):
77
+ @pytest .mark .parametrize ("sort" , [None , False ])
78
+ def test_union_bug_1730 (self , sort ):
69
79
rng_a = date_range ('1/1/2012' , periods = 4 , freq = '3H' )
70
80
rng_b = date_range ('1/1/2012' , periods = 4 , freq = '4H' )
71
81
72
- result = rng_a .union (rng_b )
82
+ result = rng_a .union (rng_b , sort = sort )
73
83
exp = DatetimeIndex (sorted (set (list (rng_a )) | set (list (rng_b ))))
74
84
tm .assert_index_equal (result , exp )
75
85
76
- def test_union_bug_1745 (self ):
86
+ @pytest .mark .parametrize ("sort" , [None , False ])
87
+ def test_union_bug_1745 (self , sort ):
77
88
left = DatetimeIndex (['2012-05-11 15:19:49.695000' ])
78
89
right = DatetimeIndex (['2012-05-29 13:04:21.322000' ,
79
90
'2012-05-11 15:27:24.873000' ,
80
91
'2012-05-11 15:31:05.350000' ])
81
92
82
- result = left .union (right )
83
- exp = DatetimeIndex (sorted (set (list (left )) | set (list (right ))))
93
+ result = left .union (right , sort = sort )
94
+ exp = DatetimeIndex (['2012-05-11 15:19:49.695000' ,
95
+ '2012-05-29 13:04:21.322000' ,
96
+ '2012-05-11 15:27:24.873000' ,
97
+ '2012-05-11 15:31:05.350000' ])
98
+ if sort is None :
99
+ exp = DatetimeIndex (sorted (set (list (left )) | set (list (right ))))
84
100
tm .assert_index_equal (result , exp )
85
101
86
- def test_union_bug_4564 (self ):
102
+ @pytest .mark .parametrize ("sort" , [None , False ])
103
+ def test_union_bug_4564 (self , sort ):
87
104
from pandas import DateOffset
88
105
left = date_range ("2013-01-01" , "2013-02-01" )
89
106
right = left + DateOffset (minutes = 15 )
90
107
91
- result = left .union (right )
108
+ result = left .union (right , sort = sort )
92
109
exp = DatetimeIndex (sorted (set (list (left )) | set (list (right ))))
93
110
tm .assert_index_equal (result , exp )
94
111
95
- def test_union_freq_both_none (self ):
112
+ @pytest .mark .parametrize ("sort" , [None , False ])
113
+ def test_union_freq_both_none (self , sort ):
96
114
# GH11086
97
115
expected = bdate_range ('20150101' , periods = 10 )
98
116
expected .freq = None
99
117
100
- result = expected .union (expected )
118
+ result = expected .union (expected , sort = sort )
101
119
tm .assert_index_equal (result , expected )
102
120
assert result .freq is None
103
121
@@ -112,11 +130,14 @@ def test_union_dataframe_index(self):
112
130
exp = pd .date_range ('1/1/1980' , '1/1/2012' , freq = 'MS' )
113
131
tm .assert_index_equal (df .index , exp )
114
132
115
- def test_union_with_DatetimeIndex (self ):
133
+ @pytest .mark .parametrize ("sort" , [None , False ])
134
+ def test_union_with_DatetimeIndex (self , sort ):
116
135
i1 = Int64Index (np .arange (0 , 20 , 2 ))
117
136
i2 = date_range (start = '2012-01-03 00:00:00' , periods = 10 , freq = 'D' )
118
- i1 .union (i2 ) # Works
119
- i2 .union (i1 ) # Fails with "AttributeError: can't set attribute"
137
+ # Works
138
+ i1 .union (i2 , sort = sort )
139
+ # Fails with "AttributeError: can't set attribute"
140
+ i2 .union (i1 , sort = sort )
120
141
121
142
# TODO: moved from test_datetimelike; de-duplicate with version below
122
143
def test_intersection2 (self ):
@@ -262,11 +283,12 @@ def test_datetimeindex_diff(self, sort):
262
283
periods = 98 )
263
284
assert len (dti1 .difference (dti2 , sort )) == 2
264
285
265
- def test_datetimeindex_union_join_empty (self ):
286
+ @pytest .mark .parametrize ("sort" , [None , False ])
287
+ def test_datetimeindex_union_join_empty (self , sort ):
266
288
dti = date_range (start = '1/1/2001' , end = '2/1/2001' , freq = 'D' )
267
289
empty = Index ([])
268
290
269
- result = dti .union (empty )
291
+ result = dti .union (empty , sort = sort )
270
292
assert isinstance (result , DatetimeIndex )
271
293
assert result is result
272
294
@@ -287,35 +309,39 @@ class TestBusinessDatetimeIndex(object):
287
309
def setup_method (self , method ):
288
310
self .rng = bdate_range (START , END )
289
311
290
- def test_union (self ):
312
+ @pytest .mark .parametrize ("sort" , [None , False ])
313
+ def test_union (self , sort ):
291
314
# overlapping
292
315
left = self .rng [:10 ]
293
316
right = self .rng [5 :10 ]
294
317
295
- the_union = left .union (right )
318
+ the_union = left .union (right , sort = sort )
296
319
assert isinstance (the_union , DatetimeIndex )
297
320
298
321
# non-overlapping, gap in middle
299
322
left = self .rng [:5 ]
300
323
right = self .rng [10 :]
301
324
302
- the_union = left .union (right )
325
+ the_union = left .union (right , sort = sort )
303
326
assert isinstance (the_union , Index )
304
327
305
328
# non-overlapping, no gap
306
329
left = self .rng [:5 ]
307
330
right = self .rng [5 :10 ]
308
331
309
- the_union = left .union (right )
332
+ the_union = left .union (right , sort = sort )
310
333
assert isinstance (the_union , DatetimeIndex )
311
334
312
335
# order does not matter
313
- tm .assert_index_equal (right .union (left ), the_union )
336
+ if sort is None :
337
+ tm .assert_index_equal (right .union (left , sort = sort ), the_union )
338
+ else :
339
+ assert tm .equalContents (right .union (left , sort = sort ), the_union )
314
340
315
341
# overlapping, but different offset
316
342
rng = date_range (START , END , freq = BMonthEnd ())
317
343
318
- the_union = self .rng .union (rng )
344
+ the_union = self .rng .union (rng , sort = sort )
319
345
assert isinstance (the_union , DatetimeIndex )
320
346
321
347
def test_outer_join (self ):
@@ -350,16 +376,20 @@ def test_outer_join(self):
350
376
assert isinstance (the_join , DatetimeIndex )
351
377
assert the_join .freq is None
352
378
353
- def test_union_not_cacheable (self ):
379
+ @pytest .mark .parametrize ("sort" , [None , False ])
380
+ def test_union_not_cacheable (self , sort ):
354
381
rng = date_range ('1/1/2000' , periods = 50 , freq = Minute ())
355
382
rng1 = rng [10 :]
356
383
rng2 = rng [:25 ]
357
- the_union = rng1 .union (rng2 )
358
- tm .assert_index_equal (the_union , rng )
384
+ the_union = rng1 .union (rng2 , sort = sort )
385
+ if sort is None :
386
+ tm .assert_index_equal (the_union , rng )
387
+ else :
388
+ assert tm .equalContents (the_union , rng )
359
389
360
390
rng1 = rng [10 :]
361
391
rng2 = rng [15 :35 ]
362
- the_union = rng1 .union (rng2 )
392
+ the_union = rng1 .union (rng2 , sort = sort )
363
393
expected = rng [10 :]
364
394
tm .assert_index_equal (the_union , expected )
365
395
@@ -388,7 +418,8 @@ def test_intersection_bug(self):
388
418
result = a .intersection (b )
389
419
tm .assert_index_equal (result , b )
390
420
391
- def test_month_range_union_tz_pytz (self ):
421
+ @pytest .mark .parametrize ("sort" , [None , False ])
422
+ def test_month_range_union_tz_pytz (self , sort ):
392
423
from pytz import timezone
393
424
tz = timezone ('US/Eastern' )
394
425
@@ -403,10 +434,11 @@ def test_month_range_union_tz_pytz(self):
403
434
late_dr = date_range (start = late_start , end = late_end , tz = tz ,
404
435
freq = MonthEnd ())
405
436
406
- early_dr .union (late_dr )
437
+ early_dr .union (late_dr , sort = sort )
407
438
408
439
@td .skip_if_windows_python_3
409
- def test_month_range_union_tz_dateutil (self ):
440
+ @pytest .mark .parametrize ("sort" , [None , False ])
441
+ def test_month_range_union_tz_dateutil (self , sort ):
410
442
from pandas ._libs .tslibs .timezones import dateutil_gettz
411
443
tz = dateutil_gettz ('US/Eastern' )
412
444
@@ -421,43 +453,45 @@ def test_month_range_union_tz_dateutil(self):
421
453
late_dr = date_range (start = late_start , end = late_end , tz = tz ,
422
454
freq = MonthEnd ())
423
455
424
- early_dr .union (late_dr )
456
+ early_dr .union (late_dr , sort = sort )
425
457
426
458
427
459
class TestCustomDatetimeIndex (object ):
428
460
429
461
def setup_method (self , method ):
430
462
self .rng = bdate_range (START , END , freq = 'C' )
431
463
432
- def test_union (self ):
464
+ @pytest .mark .parametrize ("sort" , [None , False ])
465
+ def test_union (self , sort ):
433
466
# overlapping
434
467
left = self .rng [:10 ]
435
468
right = self .rng [5 :10 ]
436
469
437
- the_union = left .union (right )
470
+ the_union = left .union (right , sort = sort )
438
471
assert isinstance (the_union , DatetimeIndex )
439
472
440
473
# non-overlapping, gap in middle
441
474
left = self .rng [:5 ]
442
475
right = self .rng [10 :]
443
476
444
- the_union = left .union (right )
477
+ the_union = left .union (right , sort )
445
478
assert isinstance (the_union , Index )
446
479
447
480
# non-overlapping, no gap
448
481
left = self .rng [:5 ]
449
482
right = self .rng [5 :10 ]
450
483
451
- the_union = left .union (right )
484
+ the_union = left .union (right , sort = sort )
452
485
assert isinstance (the_union , DatetimeIndex )
453
486
454
487
# order does not matter
455
- tm .assert_index_equal (right .union (left ), the_union )
488
+ if sort is None :
489
+ tm .assert_index_equal (right .union (left , sort = sort ), the_union )
456
490
457
491
# overlapping, but different offset
458
492
rng = date_range (START , END , freq = BMonthEnd ())
459
493
460
- the_union = self .rng .union (rng )
494
+ the_union = self .rng .union (rng , sort = sort )
461
495
assert isinstance (the_union , DatetimeIndex )
462
496
463
497
def test_outer_join (self ):
0 commit comments