From 4450eda7d60dda19e8c16e8ae55dce0867da7a03 Mon Sep 17 00:00:00 2001 From: lexzlei Date: Tue, 2 Dec 2025 01:18:22 -0500 Subject: [PATCH 1/4] BUG: raise MergeError when both right_on and right_index are specified --- pandas/core/reshape/merge.py | 4 ++++ pandas/tests/reshape/merge/test_merge.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 0de300dcaf55f..c9721f11d6c3a 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..22a5124668dbd 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 = pd.DataFrame({"col": [1, 2, 3]}) + df2 = pd.DataFrame({"col": [2, 3, 4]}) + + with pytest.raises(pd.errors.MergeError): + df1.merge(df2, left_on="col", right_on="col", right_index=True) From 02dab469db2a6004b98b8fcdd671dc0862c571f4 Mon Sep 17 00:00:00 2001 From: lexzlei Date: Tue, 2 Dec 2025 01:31:34 -0500 Subject: [PATCH 2/4] BUG: raise MergeError when both right_on and right_index are specified --- pandas/core/reshape/merge.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index c9721f11d6c3a..e24e73ff5557d 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1928,10 +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.' - # ) + 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: From 085ee3a4b11805578c8a4c232f60d465e6e3dab2 Mon Sep 17 00:00:00 2001 From: lexzlei Date: Tue, 2 Dec 2025 01:44:48 -0500 Subject: [PATCH 3/4] added entry in latest doc/source/whatsnew/v3.0.0.rst --- doc/source/whatsnew/v3.0.0.rst | 1 + 1 file changed, 1 insertion(+) 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`) From e2f26d48b554e95c79c6bde66e8b0c49afdfc9a5 Mon Sep 17 00:00:00 2001 From: lexzlei Date: Tue, 2 Dec 2025 01:58:55 -0500 Subject: [PATCH 4/4] Fix namespace usage in test_merge.py --- pandas/tests/reshape/merge/test_merge.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index 22a5124668dbd..ad6ac5c1d8481 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -3152,8 +3152,8 @@ def test_merge_pyarrow_datetime_duplicates(): def test_merge_right_on_and_right_index(): - df1 = pd.DataFrame({"col": [1, 2, 3]}) - df2 = pd.DataFrame({"col": [2, 3, 4]}) + 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)