diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 365cfb1585f00..c78545a12f43b 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -806,6 +806,25 @@ def _fast_union(self, other, sort=None): else: return left + def _union(self, other, sort): + if not len(other) or self.equals(other) or not len(self): + return super()._union(other, sort=sort) + + # We are called by `union`, which is responsible for this validation + assert isinstance(other, type(self)) + + this, other = self._maybe_utc_convert(other) + + if this._can_fast_union(other): + return this._fast_union(other, sort=sort) + else: + result = Index._union(this, other, sort=sort) + if isinstance(result, type(self)): + assert result._data.dtype == this.dtype + if result.freq is None: + result._set_freq("infer") + return result + # -------------------------------------------------------------------- # Join Methods _join_precedence = 10 diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 4b2362bea27b3..40d3823c9700b 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -379,25 +379,6 @@ def _formatter_func(self): # -------------------------------------------------------------------- # Set Operation Methods - def _union(self, other: "DatetimeIndex", sort): - if not len(other) or self.equals(other) or not len(self): - return super()._union(other, sort=sort) - - # We are called by `union`, which is responsible for this validation - assert isinstance(other, DatetimeIndex) - - this, other = self._maybe_utc_convert(other) - - if this._can_fast_union(other): - return this._fast_union(other, sort=sort) - else: - result = Index._union(this, other, sort=sort) - if isinstance(result, DatetimeIndex): - assert result._data.dtype == this.dtype - if result.freq is None: - result._set_freq("infer") - return result - def union_many(self, others): """ A bit of a hack to accelerate unioning a collection of indexes. diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 1bdb4bf8b0a00..ee6e5b984ae7b 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -246,24 +246,6 @@ def astype(self, dtype, copy=True): return Index(result.astype("i8"), name=self.name) return DatetimeIndexOpsMixin.astype(self, dtype, copy=copy) - def _union(self, other: "TimedeltaIndex", sort): - if len(other) == 0 or self.equals(other) or len(self) == 0: - return super()._union(other, sort=sort) - - # We are called by `union`, which is responsible for this validation - assert isinstance(other, TimedeltaIndex) - - this, other = self, other - - if this._can_fast_union(other): - return this._fast_union(other, sort=sort) - else: - result = Index._union(this, other, sort=sort) - if isinstance(result, TimedeltaIndex): - if result.freq is None: - result._set_freq("infer") - return result - def _maybe_promote(self, other): if other.inferred_type == "timedelta": other = TimedeltaIndex(other)