Skip to content

Commit ed78f3d

Browse files
committed
BUG: Fix sorting behavior of DataFrame.join on list arguments
1 parent c3bace8 commit ed78f3d

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

pandas/core/frame.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11389,16 +11389,21 @@ def join(
1138911389
# "Iterable[Union[DataFrame, Series]]" due to the if statements
1139011390
frames = [cast("DataFrame | Series", self)] + list(other)
1139111391

11392-
can_concat = all(df.index.is_unique for df in frames)
11392+
can_concat = (how != "right") and all(df.index.is_unique for df in frames)
1139311393

1139411394
# join indexes only using concat
1139511395
if can_concat:
1139611396
if how == "left":
1139711397
res = concat(
1139811398
frames, axis=1, join="outer", verify_integrity=True, sort=sort
1139911399
)
11400-
return res.reindex(self.index)
11400+
result = res.reindex(self.index)
11401+
if sort:
11402+
result = result.sort_index()
11403+
return result
1140111404
else:
11405+
if how == "outer":
11406+
sort = True
1140211407
return concat(
1140311408
frames, axis=1, join=how, verify_integrity=True, sort=sort
1140411409
)
@@ -11409,6 +11414,7 @@ def join(
1140911414
joined = merge(
1141011415
joined,
1141111416
frame,
11417+
sort=sort,
1141211418
how=how,
1141311419
left_index=True,
1141411420
right_index=True,

pandas/tests/frame/methods/test_join.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,8 @@ def test_join_list_series(float_frame):
396396

397397

398398
def test_suppress_future_warning_with_sort_kw(sort):
399-
sort_kw = sort
400399
a = DataFrame({"col1": [1, 2]}, index=["c", "a"])
401-
402400
b = DataFrame({"col2": [4, 5]}, index=["b", "a"])
403-
404401
c = DataFrame({"col3": [7, 8]}, index=["a", "b"])
405402

406403
expected = DataFrame(
@@ -410,11 +407,7 @@ def test_suppress_future_warning_with_sort_kw(sort):
410407
"col3": {"a": 7.0, "b": 8.0, "c": float("nan")},
411408
}
412409
)
413-
if sort_kw is False:
414-
expected = expected.reindex(index=["c", "a", "b"])
415-
416-
with tm.assert_produces_warning(None):
417-
result = a.join([b, c], how="outer", sort=sort_kw)
410+
result = a.join([b, c], how="outer", sort=sort)
418411
tm.assert_frame_equal(result, expected)
419412

420413

pandas/tests/reshape/merge/test_join.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,20 @@ def _check_diff_index(df_list, result, exp_index):
671671
with pytest.raises(ValueError, match=msg):
672672
df_list[0].join(df_list[1:], on="a")
673673

674+
@pytest.mark.parametrize("how", ["left", "right", "inner", "outer"])
675+
def test_join_many_sort(self, how, sort):
676+
df = DataFrame({"a": [1, 2, 3]}, index=[1, 0, 2])
677+
df2 = DataFrame({"b": [4, 5, 6]}, index=[2, 0, 1])
678+
if how == "right":
679+
expected = DataFrame({"a": [3, 2, 1], "b": [4, 5, 6]}, index=[2, 0, 1])
680+
else:
681+
expected = DataFrame({"a": [1, 2, 3], "b": [6, 5, 4]}, index=[1, 0, 2])
682+
if how == "outer" or sort:
683+
# outer always sorts.
684+
expected = expected.sort_index()
685+
result = df.join([df2], how=how, sort=sort)
686+
tm.assert_frame_equal(result, expected)
687+
674688
def test_join_many_mixed(self):
675689
df = DataFrame(
676690
np.random.default_rng(2).standard_normal((8, 4)),

0 commit comments

Comments
 (0)