Skip to content

Commit 311ad1c

Browse files
committed
BUG: pandas-dev#26558 Fix join on MultiIndex with datetime and string
1 parent a50bfe9 commit 311ad1c

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

pandas/core/dtypes/generic.py

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def _check(cls, inst) -> bool:
2828
ABCTimedeltaIndex = create_pandas_abc_type(
2929
"ABCTimedeltaIndex", "_typ", ("timedeltaindex",)
3030
)
31+
ABCDatetimeTimedeltaMixin = create_pandas_abc_type(
32+
"ABCDatetimeTimedeltaMixin", "_typ", ("datetimeindex", "timedeltaindex",)
33+
)
3134
ABCPeriodIndex = create_pandas_abc_type("ABCPeriodIndex", "_typ", ("periodindex",))
3235
ABCCategoricalIndex = create_pandas_abc_type(
3336
"ABCCategoricalIndex", "_typ", ("categoricalindex",)

pandas/core/indexes/base.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
ABCCategorical,
5555
ABCDataFrame,
5656
ABCDatetimeIndex,
57+
ABCDatetimeTimedeltaMixin,
5758
ABCIntervalIndex,
5859
ABCMultiIndex,
5960
ABCPandasArray,
@@ -3408,7 +3409,16 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)
34083409

34093410
# have the same levels/names so a simple join
34103411
if self.names == other.names:
3411-
pass
3412+
# For datetimelike levels, we maybe convert the corresponding level
3413+
for i, (left, right) in enumerate(zip(self.levels, other.levels)):
3414+
if isinstance(left, ABCDatetimeTimedeltaMixin):
3415+
left, right = left._prepare_for_join(right)
3416+
self = self.set_levels(left, i)
3417+
other = other.set_levels(right, i)
3418+
elif isinstance(right, ABCDatetimeTimedeltaMixin):
3419+
right, left = right._prepare_for_join(left)
3420+
self = self.set_levels(left, i)
3421+
other = other.set_levels(right, i)
34123422
else:
34133423
return self._join_multi(other, how=how, return_indexers=return_indexers)
34143424

pandas/core/indexes/datetimelike.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -830,13 +830,7 @@ def join(
830830
"""
831831
See Index.join
832832
"""
833-
if self._is_convertible_to_index_for_join(other):
834-
try:
835-
other = type(self)(other)
836-
except (TypeError, ValueError):
837-
pass
838-
839-
this, other = self._maybe_utc_convert(other)
833+
this, other = self._prepare_for_join(other)
840834
return Index.join(
841835
this,
842836
other,
@@ -846,6 +840,16 @@ def join(
846840
sort=sort,
847841
)
848842

843+
def _prepare_for_join(self, other):
844+
if self._is_convertible_to_index_for_join(other):
845+
try:
846+
other = type(self)(other)
847+
except (TypeError, ValueError):
848+
pass
849+
850+
this, other = self._maybe_utc_convert(other)
851+
return this, other
852+
849853
def _maybe_utc_convert(self, other):
850854
this = self
851855
if not hasattr(self, "tz"):

0 commit comments

Comments
 (0)