diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 6121f2097a2f1..92dfb997b3c11 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1309,6 +1309,7 @@ Reshaping - Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`) - Bug in :meth:`concat` where concatenating DataFrame and Series with ``ignore_index = True`` drops the series name (:issue:`60723`, :issue:`56257`) - Bug in :func:`melt` where calling with duplicate column names in ``id_vars`` raised a misleading ``AttributeError`` (:issue:`61475`) +- 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`) - 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`) - Bug in :meth:`DataFrame.merge` with :class:`CategoricalDtype` columns incorrectly raising ``RecursionError`` (:issue:`56376`) - Bug in :meth:`DataFrame.merge` with a ``float32`` index incorrectly casting the index to ``float64`` (:issue:`41626`) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 0de300dcaf55f..e24e73ff5557d 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1928,6 +1928,10 @@ def _validate_left_right_on(self, left_on, right_on): ) if not self.right_index and right_on is None: raise MergeError('Must pass "right_on" OR "right_index".') + if self.right_index and right_on is not None: + raise MergeError( + 'Can only pass argument "right_on" OR "right_index" not both.' + ) n = len(left_on) if self.right_index: if len(left_on) != self.right.index.nlevels: diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index f6796694621c3..ad6ac5c1d8481 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -3149,3 +3149,11 @@ def test_merge_pyarrow_datetime_duplicates(): ) expected = expected.convert_dtypes(dtype_backend="pyarrow") tm.assert_frame_equal(result, expected) + + +def test_merge_right_on_and_right_index(): + df1 = DataFrame({"col": [1, 2, 3]}) + df2 = DataFrame({"col": [2, 3, 4]}) + + with pytest.raises(pd.errors.MergeError): + df1.merge(df2, left_on="col", right_on="col", right_index=True)