@@ -2096,19 +2096,20 @@ def test_merge_equal_cat_dtypes2():
2096
2096
tm .assert_frame_equal (result , expected , check_categorical = False )
2097
2097
2098
2098
2099
- def test_right_merge_preserves_row_order ():
2099
+ @pytest .mark .parametrize ("how" , ["left" , "right" ])
2100
+ def test_merge_preserves_row_order (how ):
2100
2101
# GH 27453
2101
2102
population = [
2102
2103
("Jenn" , "Jamaica" , 3 ),
2103
2104
("Beth" , "Bulgaria" , 7 ),
2104
2105
("Carl" , "Canada" , 30 ),
2105
2106
]
2106
2107
columns = ["name" , "country" , "population" ]
2107
- pop = DataFrame (population , columns = columns )
2108
+ population_df = DataFrame (population , columns = columns )
2108
2109
2109
2110
people = [("Abe" , "America" ), ("Beth" , "Bulgaria" ), ("Carl" , "Canada" )]
2110
2111
columns = ["name" , "country" ]
2111
- ppl = DataFrame (people , columns = columns )
2112
+ people_df = DataFrame (people , columns = columns )
2112
2113
2113
2114
expected_data = [
2114
2115
("Abe" , "America" , np .nan ),
@@ -2118,33 +2119,10 @@ def test_right_merge_preserves_row_order():
2118
2119
expected_cols = ["name" , "country" , "population" ]
2119
2120
expected = DataFrame (expected_data , columns = expected_cols )
2120
2121
2121
- result = pop .merge (ppl , on = ("name" , "country" ), how = "right" )
2122
-
2123
- assert_frame_equal (expected , result )
2124
-
2125
-
2126
- def test_left_merge_preserves_row_order ():
2127
- # GH 27453
2128
- population = [
2129
- ("Jenn" , "Jamaica" , 3 ),
2130
- ("Beth" , "Bulgaria" , 7 ),
2131
- ("Carl" , "Canada" , 30 ),
2132
- ]
2133
- columns = ["name" , "country" , "population" ]
2134
- pop = DataFrame (population , columns = columns )
2135
-
2136
- people = [("Abe" , "America" ), ("Beth" , "Bulgaria" ), ("Carl" , "Canada" )]
2137
- columns = ["name" , "country" ]
2138
- ppl = DataFrame (people , columns = columns )
2139
-
2140
- expected_data = [
2141
- ("Abe" , "America" , np .nan ),
2142
- ("Beth" , "Bulgaria" , 7 ),
2143
- ("Carl" , "Canada" , 30 ),
2144
- ]
2145
- expected_cols = ["name" , "country" , "population" ]
2146
- expected = DataFrame (expected_data , columns = expected_cols )
2147
-
2148
- result = ppl .merge (pop , on = ("name" , "country" ), how = "left" )
2122
+ if how == "right" :
2123
+ left_df , right_df = population_df , people_df
2124
+ elif how == "left" :
2125
+ left_df , right_df = people_df , population_df
2149
2126
2127
+ result = left_df .merge (right_df , on = ("name" , "country" ), how = how )
2150
2128
assert_frame_equal (expected , result )
0 commit comments