Skip to content

ENH: allow duplicate column names if they are not merged upon #10639

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
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Other enhancements

- ``regex`` argument to ``DataFrame.filter`` now handles numeric column names instead of raising ``ValueError`` (:issue:`10384`).

- ``pd.merge`` will now allow duplicate column names if they are not merged upon (:issue:`10639`).

Copy link
Contributor

Choose a reason for hiding this comment

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

use pd.merge

if you were a user and saw this, would you want to see a mini-example? (and should we add a mini-example to the merge docs proper?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i don't think there is a need for an example since the behavior now is very natural. Merging 'just works' despite some duplicate column names which are irrelevant to the merge process.

.. _whatsnew_0170.api:

.. _whatsnew_0170.api_breaking:
Expand Down
11 changes: 3 additions & 8 deletions pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,14 @@ def _validate_specification(self):
if self.left_on is None:
raise MergeError('Must pass left_on or left_index=True')
else:
if not self.left.columns.is_unique:
raise MergeError("Left data columns not unique: %s"
% repr(self.left.columns))

if not self.right.columns.is_unique:
raise MergeError("Right data columns not unique: %s"
% repr(self.right.columns))

# use the common columns
common_cols = self.left.columns.intersection(
self.right.columns)
if len(common_cols) == 0:
raise MergeError('No common columns to perform merge on')
if not common_cols.is_unique:
raise MergeError("Data columns not unique: %s"
% repr(common_cols))
self.left_on = self.right_on = common_cols
elif self.on is not None:
if self.left_on is not None or self.right_on is not None:
Expand Down
10 changes: 9 additions & 1 deletion pandas/tools/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,6 @@ def test_join_append_timedeltas(self):
assert_frame_equal(result, expected)

def test_overlapping_columns_error_message(self):
# #2649
df = DataFrame({'key': [1, 2, 3],
'v1': [4, 5, 6],
'v2': [7, 8, 9]})
Expand All @@ -853,7 +852,16 @@ def test_overlapping_columns_error_message(self):

df.columns = ['key', 'foo', 'foo']
df2.columns = ['key', 'bar', 'bar']
expected = DataFrame({'key': [1, 2, 3],
'v1': [4, 5, 6],
'v2': [7, 8, 9],
'v3': [4, 5, 6],
'v4': [7, 8, 9]})
expected.columns = ['key', 'foo', 'foo', 'bar', 'bar']
assert_frame_equal(merge(df, df2), expected)

# #2649
df2.columns = ['key1', 'foo', 'foo']
self.assertRaises(ValueError, merge, df, df2)

def _check_merge(x, y):
Expand Down