Skip to content
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

REGR: MultiIndex.join does not work for ea dtypes #49284

Merged
merged 6 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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.5.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Fixed regression in :meth:`MultiIndex.join` for extension array dtypes (:issue:`49277`)
-

.. ---------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4636,8 +4636,9 @@ def join(
return self._join_non_unique(other, how=how)
elif not self.is_unique or not other.is_unique:
if self.is_monotonic_increasing and other.is_monotonic_increasing:
if self._can_use_libjoin:
if not is_interval_dtype(self.dtype):
Copy link
Member

Choose a reason for hiding this comment

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

So _can_use_libjoin shouldn't be updated instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, in most cases going through object dtype is slower than the alternative. In our case here the alternative can not handle non interval dtype

# otherwise we will fall through to _join_via_get_indexer
# go through object dtype for ea till engine is supported properly
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a GH issue related to an engine for EAs?

Copy link
Member Author

Choose a reason for hiding this comment

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

return self._join_monotonic(other, how=how)
else:
return self._join_non_unique(other, how=how)
Expand Down Expand Up @@ -5012,7 +5013,7 @@ def _wrap_joined_index(self: _IndexT, joined: ArrayLike, other: _IndexT) -> _Ind
return self._constructor(joined, name=name) # type: ignore[return-value]
else:
name = get_op_result_name(self, other)
return self._constructor._with_infer(joined, name=name)
return self._constructor._with_infer(joined, name=name, dtype=self.dtype)

@cache_readonly
def _can_use_libjoin(self) -> bool:
Expand Down
48 changes: 48 additions & 0 deletions pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Index,
Interval,
MultiIndex,
Series,
StringDtype,
)
import pandas._testing as tm

Expand Down Expand Up @@ -158,3 +160,49 @@ def test_join_overlapping_interval_level():
result = idx_1.join(idx_2, how="outer")

tm.assert_index_equal(result, expected)


def test_join_midx_ea():
# GH#49277
midx = MultiIndex.from_arrays(
[Series([1, 1, 3], dtype="Int64"), Series([1, 2, 3], dtype="Int64")],
names=["a", "b"],
)
midx2 = MultiIndex.from_arrays(
[Series([1], dtype="Int64"), Series([3], dtype="Int64")], names=["a", "c"]
)
result = midx.join(midx2, how="inner")
expected = MultiIndex.from_arrays(
[
Series([1, 1], dtype="Int64"),
Series([1, 2], dtype="Int64"),
Series([3, 3], dtype="Int64"),
],
names=["a", "b", "c"],
)
tm.assert_index_equal(result, expected)


def test_join_midx_string():
# GH#49277
midx = MultiIndex.from_arrays(
[
Series(["a", "a", "c"], dtype=StringDtype()),
Series(["a", "b", "c"], dtype=StringDtype()),
],
names=["a", "b"],
)
midx2 = MultiIndex.from_arrays(
[Series(["a"], dtype=StringDtype()), Series(["c"], dtype=StringDtype())],
names=["a", "c"],
)
result = midx.join(midx2, how="inner")
expected = MultiIndex.from_arrays(
[
Series(["a", "a"], dtype=StringDtype()),
Series(["a", "b"], dtype=StringDtype()),
Series(["c", "c"], dtype=StringDtype()),
],
names=["a", "b", "c"],
)
tm.assert_index_equal(result, expected)