Skip to content

Commit 2ab9c0c

Browse files
simplify code and add tests
1 parent 4c1b181 commit 2ab9c0c

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

pandas/core/internals/managers.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -1964,19 +1964,13 @@ def items_overlap_with_suffix(left, lsuffix, right, rsuffix):
19641964
'{rename}'.format(rename=to_rename))
19651965

19661966
def lrenamer(x):
1967-
if x in to_rename:
1968-
if lsuffix is None:
1969-
return x
1970-
else:
1971-
return '{x}{lsuffix}'.format(x=x, lsuffix=lsuffix)
1967+
if x in to_rename and lsuffix is not None:
1968+
return '{x}{lsuffix}'.format(x=x, lsuffix=lsuffix)
19721969
return x
19731970

19741971
def rrenamer(x):
1975-
if x in to_rename:
1976-
if rsuffix is None:
1977-
return x
1978-
else:
1979-
return '{x}{rsuffix}'.format(x=x, rsuffix=rsuffix)
1972+
if x in to_rename and rsuffix is not None:
1973+
return '{x}{rsuffix}'.format(x=x, rsuffix=rsuffix)
19801974
return x
19811975

19821976
return (_transform_index(left, lrenamer),

pandas/tests/reshape/merge/test_merge.py

+13
Original file line numberDiff line numberDiff line change
@@ -1483,3 +1483,16 @@ def test_merge_suffix_errors(suffixes):
14831483
with pytest.raises(ValueError,
14841484
match="columns overlap but no suffix specified"):
14851485
a.merge(b, left_index=True, right_index=True, suffixes=suffixes)
1486+
1487+
1488+
@pytest.mark.parametrize("col1, col2, expected_cols", [
1489+
("a", "a", ["a_x", "a_y"]),
1490+
(0, 0, ["0_x", "0_y"])
1491+
])
1492+
def test_merge_default_suffix(col1, col2, expected_cols):
1493+
a = pd.DataFrame({col1: [1, 2]})
1494+
b = pd.DataFrame({col2: [3, 4]})
1495+
1496+
expected = pd.DataFrame([[1, 3], [2, 4]], columns=expected_cols)
1497+
result = a.merge(b, left_index=True, right_index=True)
1498+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)