@@ -951,25 +951,27 @@ def test_indicator(self):
951
951
df1 = pd .DataFrame ({'col1' :[0 ,1 ], 'col_left' :['a' ,'b' ], 'col_conflict' :[1 ,2 ]})
952
952
df1_copy = df1 .copy ()
953
953
954
- df2 = pd .DataFrame ({'col1' :[1 ,2 ,3 ,4 ,5 ],'col_right' :[2 ,2 ,2 ,2 ,2 ],
954
+ df2 = pd .DataFrame ({'col1' :[1 ,2 ,3 ,4 ,5 ],'col_right' :[2 ,2 ,2 ,2 ,2 ],
955
955
'col_conflict' :[1 ,2 ,3 ,4 ,5 ]})
956
956
df2_copy = df2 .copy ()
957
-
958
- df_result = pd .DataFrame ({'col1' :[0 ,1 ,2 ,3 ,4 ,5 ],
957
+
958
+ df_result = pd .DataFrame ({'col1' :[0 ,1 ,2 ,3 ,4 ,5 ],
959
959
'col_conflict_x' :[1 ,2 ,np .nan ,np .nan ,np .nan ,np .nan ],
960
- 'col_left' :['a' ,'b' , np .nan ,np .nan ,np .nan ,np .nan ],
961
- 'col_conflict_y' :[np .nan ,1 ,2 ,3 ,4 ,5 ],
960
+ 'col_left' :['a' ,'b' , np .nan ,np .nan ,np .nan ,np .nan ],
961
+ 'col_conflict_y' :[np .nan ,1 ,2 ,3 ,4 ,5 ],
962
962
'col_right' :[np .nan , 2 ,2 ,2 ,2 ,2 ]},
963
963
dtype = 'float64' )
964
964
df_result ['_merge' ] = pd .Categorical (['left_only' ,'both' ,'right_only' ,
965
965
'right_only' ,'right_only' ,'right_only' ]
966
966
, categories = ['left_only' , 'right_only' , 'both' ])
967
967
968
- df_result = df_result [['col1' , 'col_conflict_x' , 'col_left' ,
968
+ df_result = df_result [['col1' , 'col_conflict_x' , 'col_left' ,
969
969
'col_conflict_y' , 'col_right' , '_merge' ]]
970
970
971
971
test = pd .merge (df1 , df2 , on = 'col1' , how = 'outer' , indicator = True )
972
972
assert_frame_equal (test , df_result )
973
+ test = df1 .merge (df2 , on = 'col1' , how = 'outer' , indicator = True )
974
+ assert_frame_equal (test , df_result )
973
975
974
976
# No side effects
975
977
assert_frame_equal (df1 , df1_copy )
@@ -981,49 +983,65 @@ def test_indicator(self):
981
983
982
984
test_custom_name = pd .merge (df1 , df2 , on = 'col1' , how = 'outer' , indicator = 'custom_name' )
983
985
assert_frame_equal (test_custom_name , df_result_custom_name )
986
+ test_custom_name = df1 .merge (df2 , on = 'col1' , how = 'outer' , indicator = 'custom_name' )
987
+ assert_frame_equal (test_custom_name , df_result_custom_name )
984
988
985
989
# Check only accepts strings and booleans
986
990
with tm .assertRaises (ValueError ):
987
991
pd .merge (df1 , df2 , on = 'col1' , how = 'outer' , indicator = 5 )
992
+ with tm .assertRaises (ValueError ):
993
+ df1 .merge (df2 , on = 'col1' , how = 'outer' , indicator = 5 )
988
994
989
995
# Check result integrity
990
-
996
+
991
997
test2 = pd .merge (df1 , df2 , on = 'col1' , how = 'left' , indicator = True )
992
998
self .assertTrue ((test2 ._merge != 'right_only' ).all ())
999
+ test2 = df1 .merge (df2 , on = 'col1' , how = 'left' , indicator = True )
1000
+ self .assertTrue ((test2 ._merge != 'right_only' ).all ())
993
1001
994
1002
test3 = pd .merge (df1 , df2 , on = 'col1' , how = 'right' , indicator = True )
995
1003
self .assertTrue ((test3 ._merge != 'left_only' ).all ())
1004
+ test3 = df1 .merge (df2 , on = 'col1' , how = 'right' , indicator = True )
1005
+ self .assertTrue ((test3 ._merge != 'left_only' ).all ())
996
1006
997
1007
test4 = pd .merge (df1 , df2 , on = 'col1' , how = 'inner' , indicator = True )
998
1008
self .assertTrue ((test4 ._merge == 'both' ).all ())
1009
+ test4 = df1 .merge (df2 , on = 'col1' , how = 'inner' , indicator = True )
1010
+ self .assertTrue ((test4 ._merge == 'both' ).all ())
999
1011
1000
1012
# Check if working name in df
1001
1013
for i in ['_right_indicator' , '_left_indicator' , '_merge' ]:
1002
1014
df_badcolumn = pd .DataFrame ({'col1' :[1 ,2 ], i :[2 ,2 ]})
1003
-
1015
+
1004
1016
with tm .assertRaises (ValueError ):
1005
1017
pd .merge (df1 , df_badcolumn , on = 'col1' , how = 'outer' , indicator = True )
1018
+ with tm .assertRaises (ValueError ):
1019
+ df1 .merge (df_badcolumn , on = 'col1' , how = 'outer' , indicator = True )
1006
1020
1007
1021
# Check for name conflict with custom name
1008
1022
df_badcolumn = pd .DataFrame ({'col1' :[1 ,2 ], 'custom_column_name' :[2 ,2 ]})
1009
-
1023
+
1010
1024
with tm .assertRaises (ValueError ):
1011
1025
pd .merge (df1 , df_badcolumn , on = 'col1' , how = 'outer' , indicator = 'custom_column_name' )
1026
+ with tm .assertRaises (ValueError ):
1027
+ df1 .merge (df_badcolumn , on = 'col1' , how = 'outer' , indicator = 'custom_column_name' )
1012
1028
1013
1029
# Merge on multiple columns
1014
1030
df3 = pd .DataFrame ({'col1' :[0 ,1 ], 'col2' :['a' ,'b' ]})
1015
1031
1016
1032
df4 = pd .DataFrame ({'col1' :[1 ,1 ,3 ], 'col2' :['b' ,'x' ,'y' ]})
1017
1033
1018
- hand_coded_result = pd .DataFrame ({'col1' :[0 ,1 ,1 ,3.0 ],
1034
+ hand_coded_result = pd .DataFrame ({'col1' :[0 ,1 ,1 ,3.0 ],
1019
1035
'col2' :['a' ,'b' ,'x' ,'y' ]})
1020
1036
hand_coded_result ['_merge' ] = pd .Categorical (
1021
1037
['left_only' ,'both' ,'right_only' ,'right_only' ]
1022
1038
, categories = ['left_only' , 'right_only' , 'both' ])
1023
-
1039
+
1024
1040
test5 = pd .merge (df3 , df4 , on = ['col1' , 'col2' ], how = 'outer' , indicator = True )
1025
1041
assert_frame_equal (test5 , hand_coded_result )
1026
-
1042
+ test5 = df3 .merge (df4 , on = ['col1' , 'col2' ], how = 'outer' , indicator = True )
1043
+ assert_frame_equal (test5 , hand_coded_result )
1044
+
1027
1045
1028
1046
def _check_merge (x , y ):
1029
1047
for how in ['inner' , 'left' , 'outer' ]:
0 commit comments