Skip to content

Commit 79d06e9

Browse files
BUG: Skip axis=1 concat in merge for empty non-key columns (#62949)
1 parent 8f359f8 commit 79d06e9

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,7 @@ Reshaping
12061206
- 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`)
12071207
- Bug in :meth:`DataFrame.merge` with :class:`CategoricalDtype` columns incorrectly raising ``RecursionError`` (:issue:`56376`)
12081208
- Bug in :meth:`DataFrame.merge` with a ``float32`` index incorrectly casting the index to ``float64`` (:issue:`41626`)
1209+
- Bug in :meth:`DataFrame.join`` when joining with empty columns of differing dtypes (:issue:`62949``)
12091210

12101211
Sparse
12111212
^^^^^^

pandas/core/reshape/merge.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,14 @@ def _reindex_and_concat(
11311131

11321132
left.columns = llabels
11331133
right.columns = rlabels
1134-
result = concat([left, right], axis=1)
1134+
if len(left.columns) == 0 and len(right.columns) == 0:
1135+
result = left.copy()
1136+
elif len(left.columns) == 0:
1137+
result = right.copy()
1138+
elif len(right.columns) == 0:
1139+
result = left.copy()
1140+
else:
1141+
result = concat([left, right], axis=1)
11351142
return result
11361143

11371144
def get_result(self) -> DataFrame:

pandas/tests/reshape/merge/test_merge.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,21 @@ def test_join_append_timedeltas2(self):
738738
)
739739
tm.assert_frame_equal(result, expected)
740740

741+
def test_join_empty_columns_warning(self):
742+
pd.set_option("infer_string", True)
743+
df = DataFrame({"a": "x", "b": ["y", "z"]}).set_index(["a", "b"])
744+
df.columns = Index([], dtype="int64")
745+
ser = DataFrame({"b": ["y", "z"], "value": [1, 2]}).set_index("b")["value"]
746+
747+
with tm.assert_produces_warning(None): # No warning expected
748+
result = df.join(ser, on="b")
749+
750+
expected = DataFrame(
751+
{"value": [1, 2]},
752+
index=MultiIndex.from_tuples([("x", "y"), ("x", "z")], names=["a", "b"]),
753+
)
754+
tm.assert_frame_equal(result, expected)
755+
741756
@pytest.mark.parametrize("unit", ["D", "h", "m", "s", "ms", "us", "ns"])
742757
def test_other_datetime_unit(self, unit):
743758
# GH 13389

0 commit comments

Comments
 (0)