Skip to content

Commit bab654e

Browse files
author
Marco Gorelli
committed
Add tests with merging on index, using original OP's example as test
1 parent 2d77a5c commit bab654e

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

doc/source/whatsnew/v1.1.0.rst

+21
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ Backwards incompatible API changes
6767
- :meth:`DataFrame.at` and :meth:`Series.at` will raise a ``TypeError`` instead of a ``ValueError`` if an incompatible key is passed, and ``KeyError`` if a missing key is passed, matching the behavior of ``.loc[]`` (:issue:`31722`)
6868
-
6969

70+
:meth:`DataFrame.merge` preserves right frame's row order
71+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72+
:meth:`DataFrame.merge` now preserves right frame's row order when executing a right merge (:issue:`27453`)
73+
74+
.. ipython:: python
75+
left_df = pd.DataFrame({'animal': ['dog', 'pig'], 'max_speed': [40, 11]})
76+
right_df = pd.DataFrame({'animal': ['quetzal', 'pig'], 'max_speed': [80, 11]})
77+
left_df
78+
right_df
79+
*pandas 1.0.x*
80+
81+
.. code-block:: python
82+
>>> left_df.merge(right_df, on=['animal', 'max_speed'], how="right")
83+
animal max_speed
84+
0 pig 11
85+
1 quetzal 80
86+
*pandas 1.1.0*
87+
88+
.. ipython:: python
89+
left_df.merge(right_df, on=['animal', 'max_speed'], how="right")
90+
7091
.. ---------------------------------------------------------------------------
7192
7293
.. _whatsnew_110.deprecations:

pandas/tests/reshape/merge/test_merge.py

+38-32
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,44 @@ def test_merge_right_index_right(self):
13181318
result = left.merge(right, left_on="key", right_index=True, how="right")
13191319
tm.assert_frame_equal(result, expected)
13201320

1321+
@pytest.mark.parametrize("how", ["left", "right"])
1322+
def test_merge_preserves_row_order(self, how):
1323+
# GH 27453
1324+
a = [2, 5, 3, 5]
1325+
df1 = pd.DataFrame({"A": a, "B": [8, 2, 4, 1]})
1326+
df2 = pd.DataFrame({"A": a, "B": [7, 1, 3, 0]})
1327+
1328+
result = df1.merge(df2[["A", "B"]], on=["A", "B"], how=how)
1329+
expected = pd.DataFrame({"A": a})
1330+
if how == "right":
1331+
expected["B"] = df2["B"]
1332+
else:
1333+
expected["B"] = df1["B"]
1334+
tm.assert_frame_equal(result, expected)
1335+
1336+
left_df = pd.DataFrame({"colors": ["blue", "red"]}, index=pd.Index([0, 1]))
1337+
right_df = pd.DataFrame({"hats": ["small", "big"]}, index=pd.Index([1, 0]))
1338+
result = left_df.merge(right_df, left_index=True, right_index=True, how=how)
1339+
if how == "right":
1340+
expected = pd.DataFrame(
1341+
{"colors": ["red", "blue"], "hats": ["small", "big"]}
1342+
)
1343+
else:
1344+
expected = pd.DataFrame(
1345+
{"colors": ["blue", "red"], "hats": ["big", "small"]}
1346+
)
1347+
1348+
left_df = pd.DataFrame({"animal": ["dog", "pig"], "max_speed": [40, 11]})
1349+
right_df = pd.DataFrame({"animal": ["quetzal", "pig"], "max_speed": [80, 11]})
1350+
result = left_df.merge(right_df, on=["animal", "max_speed"], how=how)
1351+
if how == "right":
1352+
expected = pd.DataFrame(
1353+
{"animal": ["quetzal", "pig"], "max_speed": [80, 11]}
1354+
)
1355+
else:
1356+
expected = pd.DataFrame({"animal": ["dog", "pig"], "max_speed": [40, 11]})
1357+
tm.assert_frame_equal(result, expected)
1358+
13211359
def test_merge_take_missing_values_from_index_of_other_dtype(self):
13221360
# GH 24212
13231361
left = pd.DataFrame(
@@ -2167,35 +2205,3 @@ def test_merge_datetime_upcast_dtype():
21672205
}
21682206
)
21692207
tm.assert_frame_equal(result, expected)
2170-
2171-
2172-
@pytest.mark.parametrize("how", ["left", "right"])
2173-
def test_merge_preserves_row_order(how):
2174-
# GH 27453
2175-
population_df = DataFrame(
2176-
{
2177-
"name": ["Jenn", "Beth", "Carl"],
2178-
"country": ["Jamaica", "Bulgaria", "Canada"],
2179-
"population": [3, 7, 30],
2180-
}
2181-
)
2182-
2183-
people_df = DataFrame(
2184-
{"name": ["Abe", "Beth", "Carl"], "country": ["America", "Bulgaria", "Canada"]}
2185-
)
2186-
2187-
expected = DataFrame(
2188-
{
2189-
"name": ["Abe", "Beth", "Carl"],
2190-
"country": ["America", "Bulgaria", "Canada"],
2191-
"population": [np.nan, 7, 30],
2192-
}
2193-
)
2194-
2195-
if how == "right":
2196-
left_df, right_df = population_df, people_df
2197-
elif how == "left":
2198-
left_df, right_df = people_df, population_df
2199-
2200-
result = left_df.merge(right_df, on=("name", "country"), how=how)
2201-
tm.assert_frame_equal(expected, result)

0 commit comments

Comments
 (0)