|
46 | 46 | get_objs_combined_axis, |
47 | 47 | get_unanimous_names, |
48 | 48 | ) |
| 49 | +from pandas.core.indexes.datetimes import DatetimeIndex |
49 | 50 | from pandas.core.internals import concatenate_managers |
50 | 51 |
|
51 | 52 | if TYPE_CHECKING: |
@@ -162,7 +163,7 @@ def concat( |
162 | 163 | levels=None, |
163 | 164 | names: list[HashableT] | None = None, |
164 | 165 | verify_integrity: bool = False, |
165 | | - sort: bool = False, |
| 166 | + sort: bool | lib.NoDefault = lib.no_default, |
166 | 167 | copy: bool | lib.NoDefault = lib.no_default, |
167 | 168 | ) -> DataFrame | Series: |
168 | 169 | """ |
@@ -405,14 +406,39 @@ def concat( |
405 | 406 | "Only can inner (intersect) or outer (union) join the other axis" |
406 | 407 | ) |
407 | 408 |
|
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 | | - |
414 | 409 | objs, keys, ndims = _clean_keys_and_objs(objs, keys) |
415 | 410 |
|
| 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 | + |
416 | 442 | # select an object to be our result reference |
417 | 443 | sample, objs = _get_sample_object(objs, ndims, keys, names, levels, intersect) |
418 | 444 |
|
|
0 commit comments