Skip to content

Commit 4429d0f

Browse files
authored
BUG: merge raising KeyError with differently named indexes and on keywords (#45100)
1 parent ac79b7c commit 4429d0f

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ Reshaping
915915
- Fixed metadata propagation in :meth:`Dataframe.apply` method, consequently fixing the same issue for :meth:`Dataframe.transform`, :meth:`Dataframe.nunique` and :meth:`Dataframe.mode` (:issue:`28283`)
916916
- Bug in :func:`concat` casting levels of :class:`MultiIndex` to float if the only consist of missing values (:issue:`44900`)
917917
- Bug in :meth:`DataFrame.stack` with ``ExtensionDtype`` columns incorrectly raising (:issue:`43561`)
918+
- Bug in :func:`merge` raising ``KeyError`` when joining over differently named indexes with on keywords (:issue:`45094`)
918919
- Bug in :meth:`Series.unstack` with object doing unwanted type inference on resulting columns (:issue:`44595`)
919920
- Bug in :class:`MultiIndex` failing join operations with overlapping ``IntervalIndex`` levels (:issue:`44096`)
920921
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` results is different ``dtype`` based on ``regex`` parameter (:issue:`44864`)

pandas/core/reshape/merge.py

+1
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ def _maybe_restore_index_levels(self, result: DataFrame) -> None:
820820
if (
821821
self.orig_left._is_level_reference(left_key)
822822
and self.orig_right._is_level_reference(right_key)
823+
and left_key == right_key
823824
and name not in result.index.names
824825
):
825826

pandas/tests/reshape/merge/test_merge.py

+9
Original file line numberDiff line numberDiff line change
@@ -2620,3 +2620,12 @@ def test_merge_outer_with_NaN(dtype):
26202620
dtype=dtype,
26212621
)
26222622
tm.assert_frame_equal(result, expected)
2623+
2624+
2625+
def test_merge_different_index_names():
2626+
# GH#45094
2627+
left = DataFrame({"a": [1]}, index=pd.Index([1], name="c"))
2628+
right = DataFrame({"a": [1]}, index=pd.Index([1], name="d"))
2629+
result = merge(left, right, left_on="c", right_on="d")
2630+
expected = DataFrame({"a_x": [1], "a_y": 1})
2631+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)