Skip to content

Commit

Permalink
BUG: pandas-dev#26558 Fix join on MultiIndex with datetime and string
Browse files Browse the repository at this point in the history
  • Loading branch information
nrebena committed Mar 15, 2020
1 parent a50bfe9 commit 311ad1c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pandas/core/dtypes/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def _check(cls, inst) -> bool:
ABCTimedeltaIndex = create_pandas_abc_type(
"ABCTimedeltaIndex", "_typ", ("timedeltaindex",)
)
ABCDatetimeTimedeltaMixin = create_pandas_abc_type(
"ABCDatetimeTimedeltaMixin", "_typ", ("datetimeindex", "timedeltaindex",)
)
ABCPeriodIndex = create_pandas_abc_type("ABCPeriodIndex", "_typ", ("periodindex",))
ABCCategoricalIndex = create_pandas_abc_type(
"ABCCategoricalIndex", "_typ", ("categoricalindex",)
Expand Down
12 changes: 11 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
ABCCategorical,
ABCDataFrame,
ABCDatetimeIndex,
ABCDatetimeTimedeltaMixin,
ABCIntervalIndex,
ABCMultiIndex,
ABCPandasArray,
Expand Down Expand Up @@ -3408,7 +3409,16 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)

# have the same levels/names so a simple join
if self.names == other.names:
pass
# For datetimelike levels, we maybe convert the corresponding level
for i, (left, right) in enumerate(zip(self.levels, other.levels)):
if isinstance(left, ABCDatetimeTimedeltaMixin):
left, right = left._prepare_for_join(right)
self = self.set_levels(left, i)
other = other.set_levels(right, i)
elif isinstance(right, ABCDatetimeTimedeltaMixin):
right, left = right._prepare_for_join(left)
self = self.set_levels(left, i)
other = other.set_levels(right, i)
else:
return self._join_multi(other, how=how, return_indexers=return_indexers)

Expand Down
18 changes: 11 additions & 7 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,13 +830,7 @@ def join(
"""
See Index.join
"""
if self._is_convertible_to_index_for_join(other):
try:
other = type(self)(other)
except (TypeError, ValueError):
pass

this, other = self._maybe_utc_convert(other)
this, other = self._prepare_for_join(other)
return Index.join(
this,
other,
Expand All @@ -846,6 +840,16 @@ def join(
sort=sort,
)

def _prepare_for_join(self, other):
if self._is_convertible_to_index_for_join(other):
try:
other = type(self)(other)
except (TypeError, ValueError):
pass

this, other = self._maybe_utc_convert(other)
return this, other

def _maybe_utc_convert(self, other):
this = self
if not hasattr(self, "tz"):
Expand Down

0 comments on commit 311ad1c

Please sign in to comment.