Skip to content

Commit 611513d

Browse files
committed
Keep join columns untouched
1 parent 2917cc1 commit 611513d

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

pandas/core/reshape/merge.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,12 @@ def _reindex_and_concat(
10951095
left = self.left[:]
10961096
right = self.right[:]
10971097

1098+
keep_left = [x for x in self.left_on if isinstance(x, str)]
1099+
keep_right = [x for x in self.right_on if isinstance(x, str)]
1100+
10981101
llabels, rlabels = _items_overlap_with_suffix(
1099-
self.left._info_axis, self.right._info_axis, self.suffixes, self.force_suffixes
1102+
self.left._info_axis, self.right._info_axis, self.suffixes,
1103+
self.force_suffixes, keep_left, keep_right
11001104
)
11011105

11021106
if left_indexer is not None and not is_range_indexer(left_indexer, len(left)):
@@ -3020,7 +3024,8 @@ def _validate_operand(obj: DataFrame | Series) -> DataFrame:
30203024

30213025

30223026
def _items_overlap_with_suffix(
3023-
left: Index, right: Index, suffixes: Suffixes, force_suffixes: bool = False
3027+
left: Index, right: Index, suffixes: Suffixes, force_suffixes: bool,
3028+
keep_left: list, keep_right: list
30243029
) -> tuple[Index, Index]:
30253030
"""
30263031
Suffixes type validation.
@@ -3035,11 +3040,12 @@ def _items_overlap_with_suffix(
30353040
f"Passing 'suffixes' as a {type(suffixes)}, is not supported. "
30363041
"Provide 'suffixes' as a tuple instead."
30373042
)
3038-
3039-
if not force_suffixes:
3040-
to_rename = left.intersection(right)
3041-
else:
3043+
3044+
if force_suffixes:
30423045
to_rename = left.union(right)
3046+
else:
3047+
to_rename = left.intersection(right)
3048+
keep_left, keep_right = [], []
30433049

30443050
if len(to_rename) == 0:
30453051
return left, right
@@ -3049,7 +3055,7 @@ def _items_overlap_with_suffix(
30493055
if not lsuffix and not rsuffix:
30503056
raise ValueError(f"columns overlap but no suffix specified: {to_rename}")
30513057

3052-
def renamer(x, suffix: str | None):
3058+
def renamer(x, suffix: str | None, keep: list):
30533059
"""
30543060
Rename the left and right indices.
30553061
@@ -3065,12 +3071,12 @@ def renamer(x, suffix: str | None):
30653071
-------
30663072
x : renamed column name
30673073
"""
3068-
if x in to_rename and suffix is not None:
3074+
if x in to_rename and suffix is not None and (x not in keep):
30693075
return f"{x}{suffix}"
30703076
return x
30713077

3072-
lrenamer = partial(renamer, suffix=lsuffix)
3073-
rrenamer = partial(renamer, suffix=rsuffix)
3078+
lrenamer = partial(renamer, suffix=lsuffix, keep=keep_left)
3079+
rrenamer = partial(renamer, suffix=rsuffix, keep=keep_right)
30743080

30753081
llabels = left._transform_index(lrenamer)
30763082
rlabels = right._transform_index(rrenamer)

pandas/tests/reshape/merge/test_merge.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,8 +2382,8 @@ def test_merge_suffix_with_force_simple(force_suffixes):
23822382
})
23832383

23842384
if force_suffixes:
2385-
expected = DataFrame([[2, 2, "B", "D"], [3, 3, "C", "E"]],
2386-
columns=["ID_left", "Value_left", "ID_right", "Value_right"])
2385+
expected = DataFrame([[2, "B", "D"], [3, "C", "E"]],
2386+
columns=["ID", "Value_left", "Value_right"])
23872387
else:
23882388
expected = DataFrame([[2, "B", "D"], [3, "C", "E"]],
23892389
columns=["ID", "Value_left", "Value_right"])
@@ -2399,7 +2399,7 @@ def test_merge_suffix_with_force_multi_column(force_suffixes):
23992399

24002400
if force_suffixes:
24012401
expected = DataFrame([[1, 4, "A", 1, 4, "a"], [2, 5, "B", 2, 5, "b"], [3, 6, "C", 3, 6, "c"]],
2402-
columns=["A_x", "B_x", "ALPHABET_x", "a_y", "b_y", "alphabet_y"])
2402+
columns=["A", "B", "ALPHABET_x", "a", "b", "alphabet_y"])
24032403
else:
24042404
expected = DataFrame([[1, 4, "A", 1, 4, "a"], [2, 5, "B", 2, 5, "b"], [3, 6, "C", 3, 6, "c"]],
24052405
columns=["A", "B", "ALPHABET", "a", "b", "alphabet"])

0 commit comments

Comments
 (0)