Skip to content

ERR automatic broadcast for merging different levels, #9455 #12219

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 1 commit into from
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: 1 addition & 1 deletion doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ API changes




- ``pandas.merge()`` and ``DataFrame.join()`` will show a ``UserWarning`` when merging/joining a single- with a multi-leveled dataframe (:issue:`9455`, :issue:`12219`)



Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/frame/test_axis_select_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ def test_drop_multiindex_not_lexsorted(self):

tm.assert_frame_equal(result, expected)

def test_merge_join_different_levels(self):
# GH 9455

# first dataframe
df1 = DataFrame(columns=['a', 'b'], data=[[1, 11], [0, 22]])

# second dataframe
columns = MultiIndex.from_tuples([('a', ''), ('c', 'c1')])
df2 = DataFrame(columns=columns, data=[[1, 33], [0, 44]])

# merge
columns = ['a', 'b', ('c', 'c1')]
expected = DataFrame(columns=columns, data=[[1, 11, 33], [0, 22, 44]])
with tm.assert_produces_warning(UserWarning):
result = pd.merge(df1, df2, on='a')
tm.assert_frame_equal(result, expected)

# join, see discussion in GH 12219
columns = ['a', 'b', ('a', ''), ('c', 'c1')]
expected = DataFrame(columns=columns,
data=[[1, 11, 0, 44], [0, 22, 1, 33]])
with tm.assert_produces_warning(UserWarning):
result = df1.join(df2, on='a')
tm.assert_frame_equal(result, expected)

def test_reindex(self):
newFrame = self.frame.reindex(self.ts1.index)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
SQL-style merge routines
"""

import warnings

import numpy as np
from pandas.compat import range, lrange, lzip, zip, map, filter
import pandas.compat as compat
Expand Down Expand Up @@ -193,6 +195,13 @@ def __init__(self, left, right, how='inner', on=None,
'can not merge DataFrame with instance of '
'type {0}'.format(type(right)))

# warn user when merging between different levels
if left.columns.nlevels != right.columns.nlevels:
Copy link
Contributor

Choose a reason for hiding this comment

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

ahh didn't even notice, this is ONLY checking the levels, it should be doing something like:

if left._get_axis(axis).nlevels != right._get_axis(axis).nlevels:
   ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I see. I've been away for some time, but I just went back. I can do a PR for that.

msg = ('merging between different levels can give an unintended '
'result ({0} levels on the left, {1} on the right)')
msg = msg.format(left.columns.nlevels, right.columns.nlevels)
warnings.warn(msg, UserWarning)

# note this function has side effects
(self.left_join_keys,
self.right_join_keys,
Expand Down
6 changes: 4 additions & 2 deletions pandas/tools/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,15 @@ def test_join_inner_multiindex(self):
# _assert_same_contents(expected, expected2.ix[:, expected.columns])

def test_join_hierarchical_mixed(self):
# GH 2024
df = DataFrame([(1, 2, 3), (4, 5, 6)], columns=['a', 'b', 'c'])
new_df = df.groupby(['a']).agg({'b': [np.mean, np.sum]})
other_df = DataFrame(
[(1, 2, 3), (7, 10, 6)], columns=['a', 'b', 'd'])
other_df.set_index('a', inplace=True)

result = merge(new_df, other_df, left_index=True, right_index=True)
# GH 9455, 12219
Copy link
Contributor

Choose a reason for hiding this comment

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

is the only time this warning is shown in the entire codebase?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean?

The message is shown when pandas.tools.merge:merge is used with different levels, that is in DataFrame.mergeand DataFrame.join, and I think that's pretty much it.

with tm.assert_produces_warning(UserWarning):
result = merge(new_df, other_df, left_index=True, right_index=True)
self.assertTrue(('b', 'mean') in result)
self.assertTrue('b' in result)

Expand Down