Skip to content

BUG-24212 fix usage of Index.take in pd.merge #24733

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

Merged
merged 6 commits into from
Jan 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,7 @@ Reshaping
- Bug in :func:`DataFrame.unstack` where a ``ValueError`` was raised when unstacking timezone aware values (:issue:`18338`)
- Bug in :func:`DataFrame.stack` where timezone aware values were converted to timezone naive values (:issue:`19420`)
- Bug in :func:`merge_asof` where a ``TypeError`` was raised when ``by_col`` were timezone aware values (:issue:`21184`)
- Bug in :func:`merge` when merging by index name would sometimes result in an incorrectly numbered index (:issue:`24212`)

.. _whatsnew_0240.bug_fixes.sparse:

Expand Down
14 changes: 14 additions & 0 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,26 @@ def _get_join_info(self):
if self.right_index:
if len(self.left) > 0:
join_index = self.left.index.take(left_indexer)
if (self.how == 'right' and -1 in left_indexer
and not isinstance(self.right.index, MultiIndex)):
Copy link
Contributor

Choose a reason for hiding this comment

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

make this into a function, something like

def create_join_index(index, indexer, other_indexer):
   ....

I think you can always call this, just not doing the replacement if what you are calling absent is empty

join_list = join_index.to_numpy()
absent = left_indexer == -1
join_list[absent] = self.right.index.to_numpy()[absent]
join_index = Index(join_list, dtype=join_index.dtype,
name=join_index.name)
else:
join_index = self.right.index.take(right_indexer)
left_indexer = np.array([-1] * len(join_index))
elif self.left_index:
if len(self.right) > 0:
join_index = self.right.index.take(right_indexer)
if (self.how == 'left' and -1 in right_indexer
and not isinstance(self.left.index, MultiIndex)):
join_list = join_index.to_numpy()
absent = right_indexer == -1
join_list[absent] = self.left.index.to_numpy()[absent]
join_index = Index(join_list, dtype=join_index.dtype,
name=join_index.name)
else:
join_index = self.left.index.take(left_indexer)
right_indexer = np.array([-1] * len(join_index))
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,16 @@ def test_merge_incompat_dtypes_error(self, df1_vals, df2_vals):
with pytest.raises(ValueError, match=msg):
pd.merge(df2, df1, on=['A'])

def test_merge_on_index_with_more_values(self): # GH 24212
df1 = pd.DataFrame([[1, 2], [2, 4], [3, 6], [4, 8]],
columns=['a', 'b'])
df2 = pd.DataFrame([[3, 30], [4, 40]],
columns=['a', 'c'])
df1.set_index('a', drop=False, inplace=True)
df2.set_index('a', inplace=True)
result = pd.merge(df1, df2, left_index=True, right_on='a', how='left')
assert 1 in result.index
Copy link
Member

Choose a reason for hiding this comment

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

Hmm...I think we should just check the entire DataFrame instead of just an element in result.index. Also, comment on what's being tested for here.



@pytest.fixture
def left():
Expand Down