Skip to content

Commit 32128eb

Browse files
committed
Deprecate with union_indexes
1 parent 9b2c476 commit 32128eb

File tree

6 files changed

+54
-17
lines changed

6 files changed

+54
-17
lines changed

pandas/core/apply.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ def wrap_results_dict_like(
637637
results,
638638
axis=axis,
639639
keys=keys_to_use,
640+
sort=False,
640641
)
641642
elif any(is_ndframe):
642643
# There is a mix of NDFrames and scalars

pandas/core/indexes/api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def union_indexes(indexes, sort: bool | None = True) -> Index:
227227
raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex")
228228

229229
if num_dtis == len(indexes):
230+
# sort = True
230231
result = indexes[0]
231232

232233
elif num_dtis > 1:

pandas/core/reshape/concat.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
get_objs_combined_axis,
4747
get_unanimous_names,
4848
)
49+
from pandas.core.indexes.datetimes import DatetimeIndex
4950
from pandas.core.internals import concatenate_managers
5051

5152
if TYPE_CHECKING:
@@ -162,7 +163,7 @@ def concat(
162163
levels=None,
163164
names: list[HashableT] | None = None,
164165
verify_integrity: bool = False,
165-
sort: bool = False,
166+
sort: bool | lib.NoDefault = lib.no_default,
166167
copy: bool | lib.NoDefault = lib.no_default,
167168
) -> DataFrame | Series:
168169
"""
@@ -405,14 +406,39 @@ def concat(
405406
"Only can inner (intersect) or outer (union) join the other axis"
406407
)
407408

408-
if not is_bool(sort):
409-
raise ValueError(
410-
f"The 'sort' keyword only accepts boolean values; {sort} was passed."
411-
)
412-
sort = bool(sort)
413-
414409
objs, keys, ndims = _clean_keys_and_objs(objs, keys)
415410

411+
if sort is lib.no_default:
412+
if axis == 0:
413+
non_concat_axis = [
414+
obj.columns if isinstance(obj, ABCDataFrame) else Index([obj.name])
415+
for obj in objs
416+
]
417+
else:
418+
non_concat_axis = [obj.index for obj in objs]
419+
420+
if all(isinstance(index, DatetimeIndex) for index in non_concat_axis):
421+
from pandas.core.indexes.api import union_indexes
422+
423+
no_sort_result_index = union_indexes(non_concat_axis, sort=False)
424+
if not no_sort_result_index.is_monotonic_increasing:
425+
msg = (
426+
"Sorting by default when concatenating all DatetimeIndex is "
427+
"deprecated. In the future, pandas will respect the default "
428+
"of `sort=False`. Specify `sort=True` or `sort=False` to "
429+
"silence this message."
430+
)
431+
warnings.warn(msg, Pandas4Warning, stacklevel=find_stack_level())
432+
sort = True
433+
else:
434+
sort = False
435+
else:
436+
if not is_bool(sort):
437+
raise ValueError(
438+
f"The 'sort' keyword only accepts boolean values; {sort} was passed."
439+
)
440+
sort = bool(sort)
441+
416442
# select an object to be our result reference
417443
sample, objs = _get_sample_object(objs, ndims, keys, names, levels, intersect)
418444

pandas/core/reshape/merge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ def _reindex_and_concat(
11251125

11261126
left.columns = llabels
11271127
right.columns = rlabels
1128-
result = concat([left, right], axis=1)
1128+
result = concat([left, right], axis=1, sort=False)
11291129
return result
11301130

11311131
def get_result(self) -> DataFrame:

pandas/tests/reshape/concat/test_concat.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
import numpy as np
1111
import pytest
1212

13-
from pandas.errors import InvalidIndexError
13+
from pandas.errors import (
14+
InvalidIndexError,
15+
Pandas4Warning,
16+
)
1417

1518
import pandas as pd
1619
from pandas import (
@@ -434,7 +437,9 @@ def test_concat_bug_1719(self):
434437
# to join with union
435438
# these two are of different length!
436439
left = concat([ts1, ts2], join="outer", axis=1)
437-
right = concat([ts2, ts1], join="outer", axis=1)
440+
msg = "Sorting by default when concatenating all DatetimeIndex"
441+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
442+
right = concat([ts2, ts1], join="outer", axis=1)
438443

439444
assert len(left) == len(right)
440445

pandas/tests/reshape/concat/test_datetimes.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import numpy as np
66
import pytest
77

8+
from pandas.errors import Pandas4Warning
9+
810
import pandas as pd
911
from pandas import (
1012
DataFrame,
@@ -69,27 +71,29 @@ def test_concat_datetime_timezone(self):
6971

7072
idx3 = date_range("2011-01-01", periods=3, freq="h", tz="Asia/Tokyo")
7173
df3 = DataFrame({"b": [1, 2, 3]}, index=idx3)
72-
result = concat([df1, df3], axis=1)
74+
msg = "Sorting by default when concatenating all DatetimeIndex"
75+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
76+
result = concat([df1, df3], axis=1)
7377

7478
exp_idx = DatetimeIndex(
7579
[
76-
"2010-12-31 23:00:00+00:00",
77-
"2011-01-01 00:00:00+00:00",
78-
"2011-01-01 01:00:00+00:00",
7980
"2010-12-31 15:00:00+00:00",
8081
"2010-12-31 16:00:00+00:00",
8182
"2010-12-31 17:00:00+00:00",
83+
"2010-12-31 23:00:00+00:00",
84+
"2011-01-01 00:00:00+00:00",
85+
"2011-01-01 01:00:00+00:00",
8286
]
8387
).as_unit("ns")
8488

8589
expected = DataFrame(
8690
[
87-
[1, np.nan],
88-
[2, np.nan],
89-
[3, np.nan],
9091
[np.nan, 1],
9192
[np.nan, 2],
9293
[np.nan, 3],
94+
[1, np.nan],
95+
[2, np.nan],
96+
[3, np.nan],
9397
],
9498
index=exp_idx,
9599
columns=["a", "b"],

0 commit comments

Comments
 (0)