From 79d06e9ae4669307a62f51f9af29261a60b992a0 Mon Sep 17 00:00:00 2001 From: Fahad Alam Jamal Date: Sun, 2 Nov 2025 10:59:41 +0000 Subject: [PATCH] BUG: Skip axis=1 concat in merge for empty non-key columns (#62949) --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/reshape/merge.py | 9 ++++++++- pandas/tests/reshape/merge/test_merge.py | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 12f522301e121..0922f23016b54 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1206,6 +1206,7 @@ Reshaping - 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`) +- Bug in :meth:`DataFrame.join`` when joining with empty columns of differing dtypes (:issue:`62949``) Sparse ^^^^^^ diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 571f708ccf108..16c925966cd9c 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1131,7 +1131,14 @@ def _reindex_and_concat( left.columns = llabels right.columns = rlabels - result = concat([left, right], axis=1) + if len(left.columns) == 0 and len(right.columns) == 0: + result = left.copy() + elif len(left.columns) == 0: + result = right.copy() + elif len(right.columns) == 0: + result = left.copy() + else: + result = concat([left, right], axis=1) return result def get_result(self) -> DataFrame: diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index d188cb396a7da..9ac39b8957657 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -738,6 +738,21 @@ def test_join_append_timedeltas2(self): ) tm.assert_frame_equal(result, expected) + def test_join_empty_columns_warning(self): + pd.set_option("infer_string", True) + df = DataFrame({"a": "x", "b": ["y", "z"]}).set_index(["a", "b"]) + df.columns = Index([], dtype="int64") + ser = DataFrame({"b": ["y", "z"], "value": [1, 2]}).set_index("b")["value"] + + with tm.assert_produces_warning(None): # No warning expected + result = df.join(ser, on="b") + + expected = DataFrame( + {"value": [1, 2]}, + index=MultiIndex.from_tuples([("x", "y"), ("x", "z")], names=["a", "b"]), + ) + tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize("unit", ["D", "h", "m", "s", "ms", "us", "ns"]) def test_other_datetime_unit(self, unit): # GH 13389