@@ -286,6 +286,30 @@ def union(self, other):
286
286
if not isinstance (other , DateRange ) or other .offset != self .offset :
287
287
return Index .union (self .view (Index ), other )
288
288
289
+ if self ._can_fast_union (other ):
290
+ return self ._fast_union (other )
291
+ else :
292
+ return Index .union (self , other )
293
+
294
+ def _wrap_union_result (self , other , result ):
295
+ name = self .name if self .name == other .name else None
296
+ if isinstance (other , DateRange ) and self ._can_fast_union (other ):
297
+ result = self ._view_like (result )
298
+ result .name = name
299
+ return result
300
+ else :
301
+ return Index (result , name = name )
302
+
303
+ def _wrap_joined_index (self , joined , other ):
304
+ name = self .name if self .name == other .name else None
305
+ if isinstance (other , DateRange ) and self ._can_fast_union (other ):
306
+ joined = self ._view_like (joined )
307
+ joined .name = name
308
+ return joined
309
+ else :
310
+ return Index (joined , name = name )
311
+
312
+ def _can_fast_union (self , other ):
289
313
offset = self .offset
290
314
291
315
# to make our life easier, "sort" the two ranges
@@ -298,10 +322,30 @@ def union(self, other):
298
322
right_start = right [0 ]
299
323
300
324
# Only need to "adjoin", not overlap
301
- if (left_end + offset ) >= right_start :
302
- return left ._fast_union (right )
325
+ return (left_end + offset ) >= right_start
326
+
327
+ def _fast_union (self , other ):
328
+ # to make our life easier, "sort" the two ranges
329
+ if self [0 ] <= other [0 ]:
330
+ left , right = self , other
303
331
else :
304
- return Index .union (self , other )
332
+ left , right = other , self
333
+
334
+ left_start , left_end = left [0 ], left [- 1 ]
335
+ right_end = right [- 1 ]
336
+
337
+ if not _will_use_cache (self .offset ):
338
+ # concatenate dates
339
+ if left_end < right_end :
340
+ loc = right .searchsorted (left_end , side = 'right' )
341
+ right_chunk = right .values [loc :]
342
+ dates = np .concatenate ((left .values , right_chunk ))
343
+ return self ._view_like (dates )
344
+ else :
345
+ return left
346
+ else :
347
+ return DateRange (left_start , max (left_end , right_end ),
348
+ offset = left .offset )
305
349
306
350
def intersection (self , other ):
307
351
"""
@@ -335,35 +379,13 @@ def intersection(self, other):
335
379
left_chunk = left .values [lslice ]
336
380
return self ._view_like (left_chunk )
337
381
338
- def _fast_union (self , other ):
339
- left , right = self , other
340
-
341
- left_start , left_end = left [0 ], left [- 1 ]
342
- right_end = right [- 1 ]
343
-
344
- if not _will_use_cache (self .offset ):
345
- # concatenate dates
346
- if left_end < right_end :
347
- loc = right .searchsorted (left_end , side = 'right' )
348
- right_chunk = right .values [loc :]
349
- dates = np .concatenate ((left .values , right_chunk ))
350
- return self ._view_like (dates )
351
- else :
352
- return left
353
- else :
354
- return DateRange (left_start , max (left_end , right_end ),
355
- offset = left .offset )
356
-
357
382
def _view_like (self , ndarray ):
358
383
result = ndarray .view (DateRange )
359
384
result .offset = self .offset
360
385
result .tzinfo = self .tzinfo
361
386
result .name = self .name
362
387
return result
363
388
364
- def _wrap_union_result (self , other , result ):
365
- return Index (result )
366
-
367
389
def tz_normalize (self , tz ):
368
390
"""
369
391
Convert DateRange from one time zone to another (using pytz)
0 commit comments