@@ -2155,19 +2155,20 @@ def test_merge_multiindex_columns():
2155
2155
tm .assert_frame_equal (result , expected )
2156
2156
2157
2157
2158
- def test_right_merge_preserves_row_order ():
2158
+ @pytest .mark .parametrize ("how" , ["left" , "right" ])
2159
+ def test_merge_preserves_row_order (how ):
2159
2160
# GH 27453
2160
2161
population = [
2161
2162
("Jenn" , "Jamaica" , 3 ),
2162
2163
("Beth" , "Bulgaria" , 7 ),
2163
2164
("Carl" , "Canada" , 30 ),
2164
2165
]
2165
2166
columns = ["name" , "country" , "population" ]
2166
- pop = DataFrame (population , columns = columns )
2167
+ population_df = DataFrame (population , columns = columns )
2167
2168
2168
2169
people = [("Abe" , "America" ), ("Beth" , "Bulgaria" ), ("Carl" , "Canada" )]
2169
2170
columns = ["name" , "country" ]
2170
- ppl = DataFrame (people , columns = columns )
2171
+ people_df = DataFrame (people , columns = columns )
2171
2172
2172
2173
expected_data = [
2173
2174
("Abe" , "America" , np .nan ),
@@ -2177,33 +2178,10 @@ def test_right_merge_preserves_row_order():
2177
2178
expected_cols = ["name" , "country" , "population" ]
2178
2179
expected = DataFrame (expected_data , columns = expected_cols )
2179
2180
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
2208
2185
2186
+ result = left_df .merge (right_df , on = ("name" , "country" ), how = how )
2209
2187
assert_frame_equal (expected , result )
0 commit comments