Skip to content

Commit d44fb07

Browse files
Nico Cernekjreback
Nico Cernek
authored andcommitted
BUG: Concatenation warning still appears with sort=False (#27702)
1 parent 4056ded commit d44fb07

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

doc/source/whatsnew/v0.25.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Reshaping
125125
^^^^^^^^^
126126

127127
- A ``KeyError`` is now raised if ``.unstack()`` is called on a :class:`Series` or :class:`DataFrame` with a flat :class:`Index` passing a name which is not the correct one (:issue:`18303`)
128-
-
128+
- :meth:`DataFrame.join` now suppresses the ``FutureWarning`` when the sort parameter is specified (:issue:`21952`)
129129
-
130130

131131
Sparse

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -7210,10 +7210,14 @@ def _join_compat(
72107210
# join indexes only using concat
72117211
if can_concat:
72127212
if how == "left":
7213-
res = concat(frames, axis=1, join="outer", verify_integrity=True)
7213+
res = concat(
7214+
frames, axis=1, join="outer", verify_integrity=True, sort=sort
7215+
)
72147216
return res.reindex(self.index, copy=False)
72157217
else:
7216-
return concat(frames, axis=1, join=how, verify_integrity=True)
7218+
return concat(
7219+
frames, axis=1, join=how, verify_integrity=True, sort=sort
7220+
)
72177221

72187222
joined = frames[0]
72197223

pandas/tests/frame/test_join.py

+29
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,32 @@ def test_join_left_sequence_non_unique_index():
193193
)
194194

195195
tm.assert_frame_equal(joined, expected)
196+
197+
198+
@pytest.mark.parametrize("sort_kw", [True, False, None])
199+
def test_suppress_future_warning_with_sort_kw(sort_kw):
200+
a = DataFrame({"col1": [1, 2]}, index=["c", "a"])
201+
202+
b = DataFrame({"col2": [4, 5]}, index=["b", "a"])
203+
204+
c = DataFrame({"col3": [7, 8]}, index=["a", "b"])
205+
206+
expected = DataFrame(
207+
{
208+
"col1": {"a": 2.0, "b": float("nan"), "c": 1.0},
209+
"col2": {"a": 5.0, "b": 4.0, "c": float("nan")},
210+
"col3": {"a": 7.0, "b": 8.0, "c": float("nan")},
211+
}
212+
)
213+
if sort_kw is False:
214+
expected = expected.reindex(index=["c", "a", "b"])
215+
216+
if sort_kw is None:
217+
# only warn if not explicitly specified
218+
ctx = tm.assert_produces_warning(FutureWarning, check_stacklevel=False)
219+
else:
220+
ctx = tm.assert_produces_warning(None, check_stacklevel=False)
221+
222+
with ctx:
223+
result = a.join([b, c], how="outer", sort=sort_kw)
224+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)