Skip to content
Prev Previous commit
Next Next commit
BUG-24212 clarify test
  • Loading branch information
JustinZhengBC committed Jan 12, 2019
commit d81b596deb920ea35e854de004e68a468c6e07ee
1 change: 1 addition & 0 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ def _get_join_info(self):
and not isinstance(self.left.index, MultiIndex)):
# if values missing (-1) from right index,
# take from left index instead
print(right_indexer)
join_list = join_index.to_numpy()
absent = right_indexer == -1
join_list[absent] = self.left.index.to_numpy()[absent]
Expand Down
30 changes: 19 additions & 11 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,25 @@ def test_merge_two_empty_df_no_division_error(self):
with np.errstate(divide='raise'):
merge(a, a, on=('a', 'b'))

def test_merge_on_index_with_more_values(self):
# GH 24212
# pd.merge gets [-1, -1, 0, 1] as right_indexer, ensure that -1 is
# interpreted as a missing value instead of the last element
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')
expected = pd.DataFrame([[1, 2, np.nan],
[2, 4, np.nan],
[3, 6, 30.0],
[4, 8, 40.0]],
columns=['a', 'b', 'c'])
expected.set_index('a', drop=False, inplace=True)
assert_frame_equal(result, expected)


def _check_merge(x, y):
for how in ['inner', 'left', 'outer']:
Expand Down Expand Up @@ -1102,17 +1121,6 @@ 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


@pytest.fixture
def left():
Expand Down