Skip to content

Commit 1dcf9ca

Browse files
jbrockmendeljreback
authored andcommitted
REF: Share _fast_union between DTI/TDI (#30704)
1 parent 69e2cc5 commit 1dcf9ca

File tree

3 files changed

+35
-75
lines changed

3 files changed

+35
-75
lines changed

pandas/core/indexes/datetimelike.py

+35
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
is_period_dtype,
2424
is_scalar,
2525
)
26+
from pandas.core.dtypes.concat import concat_compat
2627
from pandas.core.dtypes.generic import ABCIndex, ABCIndexClass, ABCSeries
2728

2829
from pandas.core import algorithms
@@ -771,6 +772,40 @@ def _can_fast_union(self, other) -> bool:
771772
# this will raise
772773
return False
773774

775+
def _fast_union(self, other, sort=None):
776+
if len(other) == 0:
777+
return self.view(type(self))
778+
779+
if len(self) == 0:
780+
return other.view(type(self))
781+
782+
# to make our life easier, "sort" the two ranges
783+
if self[0] <= other[0]:
784+
left, right = self, other
785+
elif sort is False:
786+
# TDIs are not in the "correct" order and we don't want
787+
# to sort but want to remove overlaps
788+
left, right = self, other
789+
left_start = left[0]
790+
loc = right.searchsorted(left_start, side="left")
791+
right_chunk = right.values[:loc]
792+
dates = concat_compat((left.values, right_chunk))
793+
return self._shallow_copy(dates)
794+
else:
795+
left, right = other, self
796+
797+
left_end = left[-1]
798+
right_end = right[-1]
799+
800+
# concatenate
801+
if left_end < right_end:
802+
loc = right.searchsorted(left_end, side="right")
803+
right_chunk = right.values[loc:]
804+
dates = concat_compat((left.values, right_chunk))
805+
return self._shallow_copy(dates)
806+
else:
807+
return left
808+
774809
# --------------------------------------------------------------------
775810
# Join Methods
776811
_join_precedence = 10

pandas/core/indexes/datetimes.py

-40
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pandas.util._decorators import Appender, Substitution, cache_readonly
1111

1212
from pandas.core.dtypes.common import _NS_DTYPE, is_float, is_integer, is_scalar
13-
from pandas.core.dtypes.concat import concat_compat
1413
from pandas.core.dtypes.dtypes import DatetimeTZDtype
1514
from pandas.core.dtypes.missing import isna
1615

@@ -431,45 +430,6 @@ def union_many(self, others):
431430
this._data._dtype = dtype
432431
return this
433432

434-
def _fast_union(self, other, sort=None):
435-
if len(other) == 0:
436-
return self.view(type(self))
437-
438-
if len(self) == 0:
439-
return other.view(type(self))
440-
441-
# Both DTIs are monotonic. Check if they are already
442-
# in the "correct" order
443-
if self[0] <= other[0]:
444-
left, right = self, other
445-
# DTIs are not in the "correct" order and we don't want
446-
# to sort but want to remove overlaps
447-
elif sort is False:
448-
left, right = self, other
449-
left_start = left[0]
450-
loc = right.searchsorted(left_start, side="left")
451-
right_chunk = right.values[:loc]
452-
dates = concat_compat((left.values, right_chunk))
453-
return self._shallow_copy(dates)
454-
# DTIs are not in the "correct" order and we want
455-
# to sort
456-
else:
457-
left, right = other, self
458-
459-
left_end = left[-1]
460-
right_end = right[-1]
461-
462-
# TODO: consider re-implementing freq._should_cache for fastpath
463-
464-
# concatenate dates
465-
if left_end < right_end:
466-
loc = right.searchsorted(left_end, side="right")
467-
right_chunk = right.values[loc:]
468-
dates = concat_compat((left.values, right_chunk))
469-
return self._shallow_copy(dates)
470-
else:
471-
return left
472-
473433
def _wrap_setop_result(self, other, result):
474434
name = get_op_result_name(self, other)
475435
return self._shallow_copy(result, name=name, freq=None, tz=self.tz)

pandas/core/indexes/timedeltas.py

-35
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
is_timedelta64_ns_dtype,
1717
pandas_dtype,
1818
)
19-
from pandas.core.dtypes.concat import concat_compat
2019
from pandas.core.dtypes.missing import isna
2120

2221
from pandas.core.accessor import delegate_names
@@ -265,40 +264,6 @@ def _union(self, other: "TimedeltaIndex", sort):
265264
result._set_freq("infer")
266265
return result
267266

268-
def _fast_union(self, other, sort=None):
269-
if len(other) == 0:
270-
return self.view(type(self))
271-
272-
if len(self) == 0:
273-
return other.view(type(self))
274-
275-
# to make our life easier, "sort" the two ranges
276-
if self[0] <= other[0]:
277-
left, right = self, other
278-
elif sort is False:
279-
# TDIs are not in the "correct" order and we don't want
280-
# to sort but want to remove overlaps
281-
left, right = self, other
282-
left_start = left[0]
283-
loc = right.searchsorted(left_start, side="left")
284-
right_chunk = right.values[:loc]
285-
dates = concat_compat((left.values, right_chunk))
286-
return self._shallow_copy(dates)
287-
else:
288-
left, right = other, self
289-
290-
left_end = left[-1]
291-
right_end = right[-1]
292-
293-
# concatenate
294-
if left_end < right_end:
295-
loc = right.searchsorted(left_end, side="right")
296-
right_chunk = right.values[loc:]
297-
dates = concat_compat((left.values, right_chunk))
298-
return self._shallow_copy(dates)
299-
else:
300-
return left
301-
302267
def _maybe_promote(self, other):
303268
if other.inferred_type == "timedelta":
304269
other = TimedeltaIndex(other)

0 commit comments

Comments
 (0)