Skip to content

Merge nonstring columns #46879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,12 @@ Strings
- Bug in :meth:`str.startswith` and :meth:`str.endswith` when using other series as parameter _pat_. Now raises ``TypeError`` (:issue:`3485`)
-

Merge
^^^^^^^
- Bug in :meth:`merge` supports complex types merge with identical columns
-


Interval
^^^^^^^^
- Bug in :meth:`IntervalArray.__setitem__` when setting ``np.nan`` into an integer-backed array raising ``ValueError`` instead of ``TypeError`` (:issue:`45484`)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2383,14 +2383,14 @@ def renamer(x, suffix):

Parameters
----------
x : original column name
x : original column
suffix : str or None

Returns
-------
x : renamed column name
x : renamed column
"""
if x in to_rename and suffix is not None:
if x in to_rename and isinstance(x, (str, int, float)) and suffix is not None:
return f"{x}{suffix}"
return x

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2669,3 +2669,18 @@ def test_merge_different_index_names():
result = merge(left, right, left_on="c", right_on="d")
expected = DataFrame({"a_x": [1], "a_y": 1})
tm.assert_frame_equal(result, expected)


def test_merge_complex_column():
class Column:
def __init__(self, name):
self.name = name

merged_column = Column(name="Z")
left = DataFrame({merged_column: [1], "X": [2]})
right = DataFrame({merged_column: [1], "Y": [6]})
result = merge(left, right, left_index=True, right_index=True)
expected = DataFrame(
[[1, 2, 1, 6]], columns=[merged_column, "X", merged_column, "Y"]
)
tm.assert_frame_equal(result, expected)