Skip to content

Commit 77f15dc

Browse files
authored
REF: share Index.join (pandas-dev#42238)
1 parent a59036c commit 77f15dc

File tree

2 files changed

+16
-35
lines changed

2 files changed

+16
-35
lines changed

pandas/core/indexes/base.py

+14
Original file line numberDiff line numberDiff line change
@@ -3870,6 +3870,7 @@ def _reindex_non_unique(
38703870
# --------------------------------------------------------------------
38713871
# Join Methods
38723872

3873+
@final
38733874
@_maybe_return_indexers
38743875
def join(
38753876
self,
@@ -3901,6 +3902,19 @@ def join(
39013902
self_is_mi = isinstance(self, ABCMultiIndex)
39023903
other_is_mi = isinstance(other, ABCMultiIndex)
39033904

3905+
if isinstance(self, ABCDatetimeIndex) and isinstance(other, ABCDatetimeIndex):
3906+
if (self.tz is None) ^ (other.tz is None):
3907+
# Raise instead of casting to object below.
3908+
raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex")
3909+
3910+
if not self._is_multi and not other._is_multi:
3911+
# We have specific handling for MultiIndex below
3912+
pself, pother = self._maybe_promote(other)
3913+
if pself is not self or pother is not other:
3914+
return pself.join(
3915+
pother, how=how, level=level, return_indexers=True, sort=sort
3916+
)
3917+
39043918
lindexer: np.ndarray | None
39053919
rindexer: np.ndarray | None
39063920

pandas/core/indexes/datetimelike.py

+2-35
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin):
506506
_is_monotonic_decreasing = Index.is_monotonic_decreasing
507507
_is_unique = Index.is_unique
508508

509+
_join_precedence = 10
510+
509511
def _with_freq(self, freq):
510512
arr = self._data._with_freq(freq)
511513
return type(self)._simple_new(arr, name=self._name)
@@ -679,38 +681,3 @@ def _union(self, other, sort):
679681
return result
680682
else:
681683
return super()._union(other, sort=sort)._with_freq("infer")
682-
683-
# --------------------------------------------------------------------
684-
# Join Methods
685-
_join_precedence = 10
686-
687-
def join(
688-
self,
689-
other,
690-
how: str = "left",
691-
level=None,
692-
return_indexers: bool = False,
693-
sort: bool = False,
694-
):
695-
"""
696-
See Index.join
697-
"""
698-
pself, pother = self._maybe_promote(other)
699-
if pself is not self or pother is not other:
700-
return pself.join(
701-
pother, how=how, level=level, return_indexers=return_indexers, sort=sort
702-
)
703-
704-
self._maybe_utc_convert(other) # raises if we dont have tzawareness compat
705-
return Index.join(
706-
self,
707-
other,
708-
how=how,
709-
level=level,
710-
return_indexers=return_indexers,
711-
sort=sort,
712-
)
713-
714-
def _maybe_utc_convert(self: _T, other: Index) -> tuple[_T, Index]:
715-
# Overridden by DatetimeIndex
716-
return self, other

0 commit comments

Comments
 (0)