Skip to content

Commit 2a17159

Browse files
jbrockmendelproost
authored andcommitted
DEPR: change pd.concat sort=None to sort=False (pandas-dev#29786)
1 parent 1d827ed commit 2a17159

File tree

5 files changed

+26
-74
lines changed

5 files changed

+26
-74
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
445445
- Removed previously deprecated "order" argument from :func:`factorize` (:issue:`19751`)
446446
- Removed previously deprecated "v" argument from :meth:`FrozenNDarray.searchsorted`, use "value" instead (:issue:`22672`)
447447
- :func:`read_stata` and :meth:`DataFrame.to_stata` no longer supports the "encoding" argument (:issue:`21400`)
448+
- In :func:`concat` the default value for ``sort`` has been changed from ``None`` to ``False`` (:issue:`20613`)
448449
- Removed previously deprecated "raise_conflict" argument from :meth:`DataFrame.update`, use "errors" instead (:issue:`23585`)
449450
- Removed previously deprecated keyword "n" from :meth:`DatetimeIndex.shift`, :meth:`TimedeltaIndex.shift`, :meth:`PeriodIndex.shift`, use "periods" instead (:issue:`22458`)
450451
-

pandas/core/indexes/api.py

-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import textwrap
22
from typing import List, Set
3-
import warnings
43

54
from pandas._libs import NaT, lib
65

@@ -211,12 +210,6 @@ def conv(i):
211210
index = indexes[0]
212211
for other in indexes[1:]:
213212
if not index.equals(other):
214-
215-
if sort is None:
216-
# TODO: remove once pd.concat sort default changes
217-
warnings.warn(_sort_msg, FutureWarning, stacklevel=8)
218-
sort = True
219-
220213
return _unique_indices(indexes)
221214

222215
name = get_consensus_names(indexes)[0]

pandas/core/reshape/concat.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def concat(
3737
levels=None,
3838
names=None,
3939
verify_integrity: bool = False,
40-
sort=None,
40+
sort: bool = False,
4141
copy: bool = True,
4242
):
4343
"""
@@ -82,18 +82,16 @@ def concat(
8282
verify_integrity : bool, default False
8383
Check whether the new concatenated axis contains duplicates. This can
8484
be very expensive relative to the actual data concatenation.
85-
sort : bool, default None
85+
sort : bool, default False
8686
Sort non-concatenation axis if it is not already aligned when `join`
87-
is 'outer'. The current default of sorting is deprecated and will
88-
change to not-sorting in a future version of pandas.
89-
90-
Explicitly pass ``sort=True`` to silence the warning and sort.
91-
Explicitly pass ``sort=False`` to silence the warning and not sort.
92-
87+
is 'outer'.
9388
This has no effect when ``join='inner'``, which already preserves
9489
the order of the non-concatenation axis.
9590
9691
.. versionadded:: 0.23.0
92+
.. versionchanged:: 1.0.0
93+
94+
Changed to not sort by default.
9795
9896
copy : bool, default True
9997
If False, do not copy data unnecessarily.

pandas/tests/frame/test_join.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_join_left_sequence_non_unique_index():
195195
tm.assert_frame_equal(joined, expected)
196196

197197

198-
@pytest.mark.parametrize("sort_kw", [True, False, None])
198+
@pytest.mark.parametrize("sort_kw", [True, False])
199199
def test_suppress_future_warning_with_sort_kw(sort_kw):
200200
a = DataFrame({"col1": [1, 2]}, index=["c", "a"])
201201

@@ -213,12 +213,6 @@ def test_suppress_future_warning_with_sort_kw(sort_kw):
213213
if sort_kw is False:
214214
expected = expected.reindex(index=["c", "a", "b"])
215215

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:
216+
with tm.assert_produces_warning(None, check_stacklevel=False):
223217
result = a.join([b, c], how="outer", sort=sort_kw)
224218
tm.assert_frame_equal(result, expected)

pandas/tests/reshape/test_concat.py

+17-51
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ def sort(request):
3737
return request.param
3838

3939

40-
@pytest.fixture(params=[True, False, None])
41-
def sort_with_none(request):
42-
"""Boolean sort keyword for concat and DataFrame.append.
43-
44-
Includes the default of None
45-
"""
46-
# TODO: Replace with sort once keyword changes.
47-
return request.param
48-
49-
5040
class TestConcatAppendCommon:
5141
"""
5242
Test common dtype coercion rules between concat and append.
@@ -775,15 +765,13 @@ def test_concat_join_axes_deprecated(self, axis):
775765
)
776766

777767
expected = pd.concat([one, two], axis=1, sort=False).reindex(index=two.index)
778-
with tm.assert_produces_warning(expected_warning=FutureWarning):
779-
result = pd.concat([one, two], axis=1, sort=False, join_axes=[two.index])
768+
result = pd.concat([one, two], axis=1, sort=False, join_axes=[two.index])
780769
tm.assert_frame_equal(result, expected)
781770

782771
expected = pd.concat([one, two], axis=0, sort=False).reindex(
783772
columns=two.columns
784773
)
785-
with tm.assert_produces_warning(expected_warning=FutureWarning):
786-
result = pd.concat([one, two], axis=0, sort=False, join_axes=[two.columns])
774+
result = pd.concat([one, two], axis=0, sort=False, join_axes=[two.columns])
787775
tm.assert_frame_equal(result, expected)
788776

789777

@@ -875,27 +863,19 @@ def test_append_records(self):
875863
tm.assert_frame_equal(result, expected)
876864

877865
# rewrite sort fixture, since we also want to test default of None
878-
def test_append_sorts(self, sort_with_none):
866+
def test_append_sorts(self, sort):
879867
df1 = pd.DataFrame({"a": [1, 2], "b": [1, 2]}, columns=["b", "a"])
880868
df2 = pd.DataFrame({"a": [1, 2], "c": [3, 4]}, index=[2, 3])
881869

882-
if sort_with_none is None:
883-
# only warn if not explicitly specified
884-
# don't check stacklevel since its set for concat, and append
885-
# has an extra stack.
886-
ctx = tm.assert_produces_warning(FutureWarning, check_stacklevel=False)
887-
else:
888-
ctx = tm.assert_produces_warning(None)
889-
890-
with ctx:
891-
result = df1.append(df2, sort=sort_with_none)
870+
with tm.assert_produces_warning(None):
871+
result = df1.append(df2, sort=sort)
892872

893873
# for None / True
894874
expected = pd.DataFrame(
895875
{"b": [1, 2, None, None], "a": [1, 2, 1, 2], "c": [None, None, 3, 4]},
896876
columns=["a", "b", "c"],
897877
)
898-
if sort_with_none is False:
878+
if sort is False:
899879
expected = expected[["b", "a", "c"]]
900880
tm.assert_frame_equal(result, expected)
901881

@@ -2629,7 +2609,7 @@ def test_concat_empty_and_non_empty_series_regression():
26292609
tm.assert_series_equal(result, expected)
26302610

26312611

2632-
def test_concat_sorts_columns(sort_with_none):
2612+
def test_concat_sorts_columns(sort):
26332613
# GH-4588
26342614
df1 = pd.DataFrame({"a": [1, 2], "b": [1, 2]}, columns=["b", "a"])
26352615
df2 = pd.DataFrame({"a": [3, 4], "c": [5, 6]})
@@ -2640,58 +2620,44 @@ def test_concat_sorts_columns(sort_with_none):
26402620
columns=["a", "b", "c"],
26412621
)
26422622

2643-
if sort_with_none is False:
2623+
if sort is False:
26442624
expected = expected[["b", "a", "c"]]
26452625

2646-
if sort_with_none is None:
2647-
# only warn if not explicitly specified
2648-
ctx = tm.assert_produces_warning(FutureWarning)
2649-
else:
2650-
ctx = tm.assert_produces_warning(None)
2651-
26522626
# default
2653-
with ctx:
2654-
result = pd.concat([df1, df2], ignore_index=True, sort=sort_with_none)
2627+
with tm.assert_produces_warning(None):
2628+
result = pd.concat([df1, df2], ignore_index=True, sort=sort)
26552629
tm.assert_frame_equal(result, expected)
26562630

26572631

2658-
def test_concat_sorts_index(sort_with_none):
2632+
def test_concat_sorts_index(sort):
26592633
df1 = pd.DataFrame({"a": [1, 2, 3]}, index=["c", "a", "b"])
26602634
df2 = pd.DataFrame({"b": [1, 2]}, index=["a", "b"])
26612635

26622636
# For True/None
26632637
expected = pd.DataFrame(
26642638
{"a": [2, 3, 1], "b": [1, 2, None]}, index=["a", "b", "c"], columns=["a", "b"]
26652639
)
2666-
if sort_with_none is False:
2640+
if sort is False:
26672641
expected = expected.loc[["c", "a", "b"]]
26682642

2669-
if sort_with_none is None:
2670-
# only warn if not explicitly specified
2671-
ctx = tm.assert_produces_warning(FutureWarning)
2672-
else:
2673-
ctx = tm.assert_produces_warning(None)
2674-
26752643
# Warn and sort by default
2676-
with ctx:
2677-
result = pd.concat([df1, df2], axis=1, sort=sort_with_none)
2644+
with tm.assert_produces_warning(None):
2645+
result = pd.concat([df1, df2], axis=1, sort=sort)
26782646
tm.assert_frame_equal(result, expected)
26792647

26802648

2681-
def test_concat_inner_sort(sort_with_none):
2649+
def test_concat_inner_sort(sort):
26822650
# https://github.com/pandas-dev/pandas/pull/20613
26832651
df1 = pd.DataFrame({"a": [1, 2], "b": [1, 2], "c": [1, 2]}, columns=["b", "a", "c"])
26842652
df2 = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=[3, 4])
26852653

26862654
with tm.assert_produces_warning(None):
26872655
# unset sort should *not* warn for inner join
26882656
# since that never sorted
2689-
result = pd.concat(
2690-
[df1, df2], sort=sort_with_none, join="inner", ignore_index=True
2691-
)
2657+
result = pd.concat([df1, df2], sort=sort, join="inner", ignore_index=True)
26922658

26932659
expected = pd.DataFrame({"b": [1, 2, 3, 4], "a": [1, 2, 1, 2]}, columns=["b", "a"])
2694-
if sort_with_none is True:
2660+
if sort is True:
26952661
expected = expected[["a", "b"]]
26962662
tm.assert_frame_equal(result, expected)
26972663

0 commit comments

Comments
 (0)