@@ -1355,3 +1355,81 @@ def test_dtype_on_merged_different(self, change, how, left, right):
13551355 np .dtype ('int64' )],
13561356 index = ['X' , 'Y' , 'Z' ])
13571357 assert_series_equal (result , expected )
1358+
1359+ class TestMergeOnIndexes (object ):
1360+
1361+ def test_merge_on_indexes (self ):
1362+ df1 = pd .DataFrame ({'a' : [20 , 10 , 0 ]}, index = [2 , 1 , 0 ])
1363+ df2 = pd .DataFrame ({'b' : [100 , 200 , 300 ]}, index = [1 , 2 , 3 ])
1364+
1365+ # default how='inner'
1366+ result = pd .merge (df1 , df2 , left_index = True , right_index = True )
1367+ expected = pd .DataFrame ({'a' : [20 , 10 ], 'b' : [200 , 100 ]},
1368+ index = [2 , 1 ])
1369+ tm .assert_frame_equal (result , expected )
1370+
1371+ # how='left'
1372+ result = pd .merge (df1 , df2 , left_index = True , right_index = True , how = 'left' )
1373+ expected = pd .DataFrame ({'a' : [20 , 10 , 0 ], 'b' : [200 , 100 , np .nan ]},
1374+ index = [2 , 1 , 0 ])
1375+ tm .assert_frame_equal (result , expected )
1376+
1377+ # how='right'
1378+ result = pd .merge (df1 , df2 , left_index = True , right_index = True , how = 'right' )
1379+ expected = pd .DataFrame ({'a' : [10 , 20 , np .nan ], 'b' : [100 , 200 , 300 ]},
1380+ index = [1 , 2 , 3 ])
1381+ tm .assert_frame_equal (result , expected )
1382+
1383+ # how='inner'
1384+ result = pd .merge (df1 , df2 , left_index = True , right_index = True , how = 'inner' )
1385+ expected = pd .DataFrame ({'a' : [20 , 10 ], 'b' : [200 , 100 ]},
1386+ index = [2 , 1 ])
1387+ tm .assert_frame_equal (result , expected )
1388+
1389+ # how='outer'
1390+ result = pd .merge (df1 , df2 , left_index = True , right_index = True , how = 'outer' )
1391+ expected = pd .DataFrame ({'a' : [0 , 10 , 20 , np .nan ],
1392+ 'b' : [np .nan , 100 , 200 , 300 ]},
1393+ index = [0 , 1 , 2 , 3 ])
1394+ tm .assert_frame_equal (result , expected )
1395+
1396+ def test_merge_on_indexes_sort (self ):
1397+ df1 = pd .DataFrame ({'a' : [20 , 10 , 0 ]}, index = [2 , 1 , 0 ])
1398+ df2 = pd .DataFrame ({'b' : [100 , 200 , 300 ]}, index = [1 , 2 , 3 ])
1399+
1400+ # default how='inner'
1401+ result = pd .merge (df1 , df2 , left_index = True , right_index = True , sort = True )
1402+ expected = pd .DataFrame ({'a' : [10 , 20 ], 'b' : [100 , 200 ]},
1403+ index = [1 , 2 ])
1404+ tm .assert_frame_equal (result , expected )
1405+
1406+ # how='left'
1407+ result = pd .merge (df1 , df2 , left_index = True , right_index = True , how = 'left' , sort = True )
1408+ expected = pd .DataFrame ({'a' : [0 , 10 , 20 ], 'b' : [np .nan , 100 , 200 ]},
1409+ index = [0 , 1 , 2 ])
1410+ tm .assert_frame_equal (result , expected )
1411+
1412+ # how='right' (already sorted)
1413+ result = pd .merge (df1 , df2 , left_index = True , right_index = True , how = 'right' , sort = True )
1414+ expected = pd .DataFrame ({'a' : [10 , 20 , np .nan ], 'b' : [100 , 200 , 300 ]},
1415+ index = [1 , 2 , 3 ])
1416+ tm .assert_frame_equal (result , expected )
1417+
1418+ # how='right'
1419+ result = pd .merge (df2 , df1 , left_index = True , right_index = True , how = 'right' , sort = True )
1420+ expected = pd .DataFrame ([[np .nan , 0 ], [100 , 10 ], [200 , 20 ]],
1421+ columns = ['b' , 'a' ], index = [0 , 1 , 2 ])
1422+ tm .assert_frame_equal (result , expected )
1423+
1424+ # how='inner'
1425+ result = pd .merge (df1 , df2 , left_index = True , right_index = True , how = 'inner' , sort = True )
1426+ expected = pd .DataFrame ({'a' : [10 , 20 ], 'b' : [100 , 200 ]},
1427+ index = [1 , 2 ])
1428+ tm .assert_frame_equal (result , expected )
1429+
1430+ # how='outer'
1431+ result = pd .merge (df1 , df2 , left_index = True , right_index = True , how = 'outer' , sort = True )
1432+ expected = pd .DataFrame ({'a' : [0 , 10 , 20 , np .nan ],
1433+ 'b' : [np .nan , 100 , 200 , 300 ]},
1434+ index = [0 , 1 , 2 , 3 ])
1435+ tm .assert_frame_equal (result , expected )
0 commit comments