Skip to content

Commit b6dc9ba

Browse files
Charlie-XIAOtopper-123
authored andcommitted
BUG Merge not behaving correctly when having MultiIndex with a single level (pandas-dev#53215)
* fix merge when MultiIndex with single level * resolved conversations * fixed code style
1 parent 4f7e46e commit b6dc9ba

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ Reshaping
458458
^^^^^^^^^
459459
- Bug in :func:`crosstab` when ``dropna=False`` would not keep ``np.nan`` in the result (:issue:`10772`)
460460
- Bug in :meth:`DataFrame.agg` and :meth:`Series.agg` on non-unique columns would return incorrect type when dist-like argument passed in (:issue:`51099`)
461+
- Bug in :meth:`DataFrame.merge` not merging correctly when having ``MultiIndex`` with single level (:issue:`52331`)
461462
- Bug in :meth:`DataFrame.stack` losing extension dtypes when columns is a :class:`MultiIndex` and frame contains mixed dtypes (:issue:`45740`)
462463
- Bug in :meth:`DataFrame.transpose` inferring dtype for object column (:issue:`51546`)
463464
- Bug in :meth:`Series.combine_first` converting ``int64`` dtype to ``float64`` and losing precision on very large integers (:issue:`51764`)

pandas/core/reshape/merge.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -2267,23 +2267,14 @@ def _get_no_sort_one_missing_indexer(
22672267
def _left_join_on_index(
22682268
left_ax: Index, right_ax: Index, join_keys, sort: bool = False
22692269
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp]]:
2270-
if len(join_keys) > 1:
2271-
if not (
2272-
isinstance(right_ax, MultiIndex) and len(join_keys) == right_ax.nlevels
2273-
):
2274-
raise AssertionError(
2275-
"If more than one join key is given then "
2276-
"'right_ax' must be a MultiIndex and the "
2277-
"number of join keys must be the number of levels in right_ax"
2278-
)
2279-
2270+
if isinstance(right_ax, MultiIndex):
22802271
left_indexer, right_indexer = _get_multiindex_indexer(
22812272
join_keys, right_ax, sort=sort
22822273
)
22832274
else:
2284-
jkey = join_keys[0]
2285-
2286-
left_indexer, right_indexer = _get_single_indexer(jkey, right_ax, sort=sort)
2275+
left_indexer, right_indexer = _get_single_indexer(
2276+
join_keys[0], right_ax, sort=sort
2277+
)
22872278

22882279
if sort or len(left_ax) != len(left_indexer):
22892280
# if asked to sort or there are 1-to-many matches

pandas/tests/reshape/merge/test_merge.py

+13
Original file line numberDiff line numberDiff line change
@@ -2797,3 +2797,16 @@ def test_merge_datetime_different_resolution(tzinfo):
27972797
)
27982798
result = df1.merge(df2, on="t")
27992799
tm.assert_frame_equal(result, expected)
2800+
2801+
2802+
def test_merge_multiindex_single_level():
2803+
# GH #52331
2804+
df = DataFrame({"col": ["A", "B"]})
2805+
df2 = DataFrame(
2806+
data={"b": [100]},
2807+
index=MultiIndex.from_tuples([("A",), ("C",)], names=["col"]),
2808+
)
2809+
expected = DataFrame({"col": ["A", "B"], "b": [100, np.nan]})
2810+
2811+
result = df.merge(df2, left_on=["col"], right_index=True, how="left")
2812+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)