@@ -2155,19 +2155,20 @@ def test_merge_multiindex_columns():
21552155 tm .assert_frame_equal (result , expected )
21562156
21572157
2158- def test_right_merge_preserves_row_order ():
2158+ @pytest .mark .parametrize ("how" , ["left" , "right" ])
2159+ def test_merge_preserves_row_order (how ):
21592160 # GH 27453
21602161 population = [
21612162 ("Jenn" , "Jamaica" , 3 ),
21622163 ("Beth" , "Bulgaria" , 7 ),
21632164 ("Carl" , "Canada" , 30 ),
21642165 ]
21652166 columns = ["name" , "country" , "population" ]
2166- pop = DataFrame (population , columns = columns )
2167+ population_df = DataFrame (population , columns = columns )
21672168
21682169 people = [("Abe" , "America" ), ("Beth" , "Bulgaria" ), ("Carl" , "Canada" )]
21692170 columns = ["name" , "country" ]
2170- ppl = DataFrame (people , columns = columns )
2171+ people_df = DataFrame (people , columns = columns )
21712172
21722173 expected_data = [
21732174 ("Abe" , "America" , np .nan ),
@@ -2177,33 +2178,10 @@ def test_right_merge_preserves_row_order():
21772178 expected_cols = ["name" , "country" , "population" ]
21782179 expected = DataFrame (expected_data , columns = expected_cols )
21792180
2180- result = pop .merge (ppl , on = ("name" , "country" ), how = "right" )
2181-
2182- assert_frame_equal (expected , result )
2183-
2184-
2185- def test_left_merge_preserves_row_order ():
2186- # GH 27453
2187- population = [
2188- ("Jenn" , "Jamaica" , 3 ),
2189- ("Beth" , "Bulgaria" , 7 ),
2190- ("Carl" , "Canada" , 30 ),
2191- ]
2192- columns = ["name" , "country" , "population" ]
2193- pop = DataFrame (population , columns = columns )
2194-
2195- people = [("Abe" , "America" ), ("Beth" , "Bulgaria" ), ("Carl" , "Canada" )]
2196- columns = ["name" , "country" ]
2197- ppl = DataFrame (people , columns = columns )
2198-
2199- expected_data = [
2200- ("Abe" , "America" , np .nan ),
2201- ("Beth" , "Bulgaria" , 7 ),
2202- ("Carl" , "Canada" , 30 ),
2203- ]
2204- expected_cols = ["name" , "country" , "population" ]
2205- expected = DataFrame (expected_data , columns = expected_cols )
2206-
2207- result = ppl .merge (pop , on = ("name" , "country" ), how = "left" )
2181+ if how == "right" :
2182+ left_df , right_df = population_df , people_df
2183+ elif how == "left" :
2184+ left_df , right_df = people_df , population_df
22082185
2186+ result = left_df .merge (right_df , on = ("name" , "country" ), how = how )
22092187 assert_frame_equal (expected , result )
0 commit comments