Skip to content

Commit 774172b

Browse files
authored
BUG: MergeError not raised when both right_on and right_index are specified (#63248)
1 parent d5fad5f commit 774172b

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ Reshaping
13461346
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)
13471347
- Bug in :meth:`concat` where concatenating DataFrame and Series with ``ignore_index = True`` drops the series name (:issue:`60723`, :issue:`56257`)
13481348
- Bug in :func:`melt` where calling with duplicate column names in ``id_vars`` raised a misleading ``AttributeError`` (:issue:`61475`)
1349+
- Bug in :meth:`DataFrame.merge` where specifying both ``right_on`` and ``right_index`` did not raise a ``MergeError`` if ``left_on`` is also specified. Now raises a ``MergeError`` in such cases. (:issue:`63242`)
13491350
- Bug in :meth:`DataFrame.merge` where user-provided suffixes could result in duplicate column names if the resulting names matched existing columns. Now raises a :class:`MergeError` in such cases. (:issue:`61402`)
13501351
- Bug in :meth:`DataFrame.merge` with :class:`CategoricalDtype` columns incorrectly raising ``RecursionError`` (:issue:`56376`)
13511352
- Bug in :meth:`DataFrame.merge` with a ``float32`` index incorrectly casting the index to ``float64`` (:issue:`41626`)

pandas/core/reshape/merge.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,10 @@ def _validate_left_right_on(self, left_on, right_on):
19281928
)
19291929
if not self.right_index and right_on is None:
19301930
raise MergeError('Must pass "right_on" OR "right_index".')
1931+
if self.right_index and right_on is not None:
1932+
raise MergeError(
1933+
'Can only pass argument "right_on" OR "right_index" not both.'
1934+
)
19311935
n = len(left_on)
19321936
if self.right_index:
19331937
if len(left_on) != self.right.index.nlevels:

pandas/tests/reshape/merge/test_merge.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3149,3 +3149,11 @@ def test_merge_pyarrow_datetime_duplicates():
31493149
)
31503150
expected = expected.convert_dtypes(dtype_backend="pyarrow")
31513151
tm.assert_frame_equal(result, expected)
3152+
3153+
3154+
def test_merge_right_on_and_right_index():
3155+
df1 = DataFrame({"col": [1, 2, 3]})
3156+
df2 = DataFrame({"col": [2, 3, 4]})
3157+
3158+
with pytest.raises(pd.errors.MergeError):
3159+
df1.merge(df2, left_on="col", right_on="col", right_index=True)

0 commit comments

Comments
 (0)