-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
Merge nonstring columns #46879
Changes from 9 commits
4c7722e
cb22a1e
7342a72
ac929ea
5f3f90e
496419d
4af2f89
9fef0f3
6aa110e
b58d477
bfe59be
8ec9e56
4112c72
0375b94
c8e956d
989aefa
f1792a6
c8224f9
5c1dacb
dc7ff6e
edef8fe
ce72b94
5522ad6
f62a084
4b1df82
461d5b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2383,15 +2383,18 @@ def renamer(x, suffix): | |
|
||
Parameters | ||
---------- | ||
x : original column name | ||
x : original column | ||
ericman93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
suffix : str or None | ||
|
||
Returns | ||
------- | ||
x : renamed column name | ||
x : renamed column | ||
ericman93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
if x in to_rename and suffix is not None: | ||
return f"{x}{suffix}" | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment on why this is done There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}") | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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) |
Uh oh!
There was an error while loading. Please reload this page.