Skip to content

Commit 08d02e2

Browse files
authored
Backport PR pandas-dev#49284 on branch 1.5.x (REGR: MultiIndex.join does not work for ea dtypes) (pandas-dev#49627)
REGR: MultiIndex.join does not work for ea dtypes (pandas-dev#49284) * REGR: MultiIndex.join does not work for ea dtypes * Update base.py (cherry picked from commit f82b1c6)
1 parent c8018b5 commit 08d02e2

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

doc/source/whatsnew/v1.5.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ including other versions of pandas.
1313

1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
16+
- Fixed regression in :meth:`MultiIndex.join` for extension array dtypes (:issue:`49277`)
1617
- Fixed regression in :meth:`Series.replace` raising ``RecursionError`` with numeric dtype and when specifying ``value=None`` (:issue:`45725`)
1718
- Fixed regression in :meth:`DataFrame.plot` preventing :class:`~matplotlib.colors.Colormap` instance
1819
from being passed using the ``colormap`` argument if Matplotlib 3.6+ is used (:issue:`49374`)

pandas/core/indexes/base.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -4701,8 +4701,10 @@ def join(
47014701
return self._join_non_unique(other, how=how)
47024702
elif not self.is_unique or not other.is_unique:
47034703
if self.is_monotonic_increasing and other.is_monotonic_increasing:
4704-
if self._can_use_libjoin:
4704+
if not is_interval_dtype(self.dtype):
47054705
# otherwise we will fall through to _join_via_get_indexer
4706+
# GH#39133
4707+
# go through object dtype for ea till engine is supported properly
47064708
return self._join_monotonic(other, how=how)
47074709
else:
47084710
return self._join_non_unique(other, how=how)
@@ -5079,7 +5081,7 @@ def _wrap_joined_index(self: _IndexT, joined: ArrayLike, other: _IndexT) -> _Ind
50795081
return self._constructor(joined, name=name) # type: ignore[return-value]
50805082
else:
50815083
name = get_op_result_name(self, other)
5082-
return self._constructor._with_infer(joined, name=name)
5084+
return self._constructor._with_infer(joined, name=name, dtype=self.dtype)
50835085

50845086
@cache_readonly
50855087
def _can_use_libjoin(self) -> bool:

pandas/tests/indexes/multi/test_join.py

+48
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Index,
66
Interval,
77
MultiIndex,
8+
Series,
9+
StringDtype,
810
)
911
import pandas._testing as tm
1012

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

160162
tm.assert_index_equal(result, expected)
163+
164+
165+
def test_join_midx_ea():
166+
# GH#49277
167+
midx = MultiIndex.from_arrays(
168+
[Series([1, 1, 3], dtype="Int64"), Series([1, 2, 3], dtype="Int64")],
169+
names=["a", "b"],
170+
)
171+
midx2 = MultiIndex.from_arrays(
172+
[Series([1], dtype="Int64"), Series([3], dtype="Int64")], names=["a", "c"]
173+
)
174+
result = midx.join(midx2, how="inner")
175+
expected = MultiIndex.from_arrays(
176+
[
177+
Series([1, 1], dtype="Int64"),
178+
Series([1, 2], dtype="Int64"),
179+
Series([3, 3], dtype="Int64"),
180+
],
181+
names=["a", "b", "c"],
182+
)
183+
tm.assert_index_equal(result, expected)
184+
185+
186+
def test_join_midx_string():
187+
# GH#49277
188+
midx = MultiIndex.from_arrays(
189+
[
190+
Series(["a", "a", "c"], dtype=StringDtype()),
191+
Series(["a", "b", "c"], dtype=StringDtype()),
192+
],
193+
names=["a", "b"],
194+
)
195+
midx2 = MultiIndex.from_arrays(
196+
[Series(["a"], dtype=StringDtype()), Series(["c"], dtype=StringDtype())],
197+
names=["a", "c"],
198+
)
199+
result = midx.join(midx2, how="inner")
200+
expected = MultiIndex.from_arrays(
201+
[
202+
Series(["a", "a"], dtype=StringDtype()),
203+
Series(["a", "b"], dtype=StringDtype()),
204+
Series(["c", "c"], dtype=StringDtype()),
205+
],
206+
names=["a", "b", "c"],
207+
)
208+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)