Skip to content

REF: share Index.join #42238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3870,6 +3870,7 @@ def _reindex_non_unique(
# --------------------------------------------------------------------
# Join Methods

@final
@_maybe_return_indexers
def join(
self,
Expand Down Expand Up @@ -3901,6 +3902,19 @@ def join(
self_is_mi = isinstance(self, ABCMultiIndex)
other_is_mi = isinstance(other, ABCMultiIndex)

if isinstance(self, ABCDatetimeIndex) and isinstance(other, ABCDatetimeIndex):
if (self.tz is None) ^ (other.tz is None):
# Raise instead of casting to object below.
raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex")

if not self._is_multi and not other._is_multi:
# We have specific handling for MultiIndex below
pself, pother = self._maybe_promote(other)
if pself is not self or pother is not other:
return pself.join(
pother, how=how, level=level, return_indexers=True, sort=sort
)

lindexer: np.ndarray | None
rindexer: np.ndarray | None

Expand Down
37 changes: 2 additions & 35 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin):
_is_monotonic_decreasing = Index.is_monotonic_decreasing
_is_unique = Index.is_unique

_join_precedence = 10

def _with_freq(self, freq):
arr = self._data._with_freq(freq)
return type(self)._simple_new(arr, name=self._name)
Expand Down Expand Up @@ -678,38 +680,3 @@ def _union(self, other, sort):
return result
else:
return super()._union(other, sort=sort)._with_freq("infer")

# --------------------------------------------------------------------
# Join Methods
_join_precedence = 10

def join(
self,
other,
how: str = "left",
level=None,
return_indexers: bool = False,
sort: bool = False,
):
"""
See Index.join
"""
pself, pother = self._maybe_promote(other)
if pself is not self or pother is not other:
return pself.join(
pother, how=how, level=level, return_indexers=return_indexers, sort=sort
)

self._maybe_utc_convert(other) # raises if we dont have tzawareness compat
return Index.join(
self,
other,
how=how,
level=level,
return_indexers=return_indexers,
sort=sort,
)

def _maybe_utc_convert(self: _T, other: Index) -> tuple[_T, Index]:
# Overridden by DatetimeIndex
return self, other