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 9 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 user-defined 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
9 changes: 6 additions & 3 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2383,15 +2383,18 @@ 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:
return f"{x}{suffix}"
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment on why this is done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the expectation of handling user defined types

return x + suffix
except TypeError:
return f"{x}{suffix}"
return x

lrenamer = partial(renamer, suffix=lsuffix)
Expand Down
25 changes: 25 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,28 @@ 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():
# GH#46885
class Column:
def __init__(self, name):
self.name = name

def __eq__(self, other):
return other.name == self.name

def __hash__(self):
return hash(self.name)

def __add__(self, other):
return Column(name=f"{self.name}@{other}")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add another example with a Column that does not have a __add__

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