Skip to content

Commit b187a0d

Browse files
jbrockmendeljreback
authored andcommitted
REF: share _union between DTI/TDI (#30714)
1 parent 72db40e commit b187a0d

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

pandas/core/indexes/datetimelike.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,25 @@ def _fast_union(self, other, sort=None):
808808
else:
809809
return left
810810

811+
def _union(self, other, sort):
812+
if not len(other) or self.equals(other) or not len(self):
813+
return super()._union(other, sort=sort)
814+
815+
# We are called by `union`, which is responsible for this validation
816+
assert isinstance(other, type(self))
817+
818+
this, other = self._maybe_utc_convert(other)
819+
820+
if this._can_fast_union(other):
821+
return this._fast_union(other, sort=sort)
822+
else:
823+
result = Index._union(this, other, sort=sort)
824+
if isinstance(result, type(self)):
825+
assert result._data.dtype == this.dtype
826+
if result.freq is None:
827+
result._set_freq("infer")
828+
return result
829+
811830
# --------------------------------------------------------------------
812831
# Join Methods
813832
_join_precedence = 10

pandas/core/indexes/datetimes.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -379,25 +379,6 @@ def _formatter_func(self):
379379
# --------------------------------------------------------------------
380380
# Set Operation Methods
381381

382-
def _union(self, other: "DatetimeIndex", sort):
383-
if not len(other) or self.equals(other) or not len(self):
384-
return super()._union(other, sort=sort)
385-
386-
# We are called by `union`, which is responsible for this validation
387-
assert isinstance(other, DatetimeIndex)
388-
389-
this, other = self._maybe_utc_convert(other)
390-
391-
if this._can_fast_union(other):
392-
return this._fast_union(other, sort=sort)
393-
else:
394-
result = Index._union(this, other, sort=sort)
395-
if isinstance(result, DatetimeIndex):
396-
assert result._data.dtype == this.dtype
397-
if result.freq is None:
398-
result._set_freq("infer")
399-
return result
400-
401382
def union_many(self, others):
402383
"""
403384
A bit of a hack to accelerate unioning a collection of indexes.

pandas/core/indexes/timedeltas.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,6 @@ def astype(self, dtype, copy=True):
246246
return Index(result.astype("i8"), name=self.name)
247247
return DatetimeIndexOpsMixin.astype(self, dtype, copy=copy)
248248

249-
def _union(self, other: "TimedeltaIndex", sort):
250-
if len(other) == 0 or self.equals(other) or len(self) == 0:
251-
return super()._union(other, sort=sort)
252-
253-
# We are called by `union`, which is responsible for this validation
254-
assert isinstance(other, TimedeltaIndex)
255-
256-
this, other = self, other
257-
258-
if this._can_fast_union(other):
259-
return this._fast_union(other, sort=sort)
260-
else:
261-
result = Index._union(this, other, sort=sort)
262-
if isinstance(result, TimedeltaIndex):
263-
if result.freq is None:
264-
result._set_freq("infer")
265-
return result
266-
267249
def _maybe_promote(self, other):
268250
if other.inferred_type == "timedelta":
269251
other = TimedeltaIndex(other)

0 commit comments

Comments
 (0)