Skip to content

BUG: Fix join on MultiIndex for mixed Datetimelike and string levels #32739

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ MultiIndex
# Common elements are now guaranteed to be ordered by the left side
left.intersection(right, sort=False)

-
- Bug in :meth:`Index.join` for MultiIndex when joining level between datetimelike and string(:issue:`26558`)

I/O
^^^
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woa, we don't want to do anything like this.

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
18 changes: 18 additions & 0 deletions pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,21 @@ def test_join_multi_wrong_order():
tm.assert_index_equal(midx1, join_idx)
assert lidx is None
tm.assert_numpy_array_equal(ridx, exp_ridx)


@pytest.mark.parametrize(
("data", "func"), [("2020-03-15", pd.to_datetime), ("1d", pd.to_timedelta)]
)
def test_join_mixed_index_datetime_string(data, func, join_type):
# GH 26558
# Joining multiindex with mixed datetime and string level
mi1 = pd.MultiIndex.from_tuples([(data,)])
mi2 = pd.MultiIndex.from_tuples([(func(data),)])

_, indexer_left, indexer_right = mi1.join(mi2, how=join_type, return_indexers=True)
assert indexer_left is None
assert indexer_right is None

_, indexer_left, indexer_right = mi2.join(mi1, how=join_type, return_indexers=True)
assert indexer_left is None
assert indexer_right is None