Skip to content

Commit 9b2c476

Browse files
committed
BUG: Do not ignore sort in concat for DatetimeIndex
1 parent a329dc3 commit 9b2c476

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,45 @@ In cases with mixed-resolution inputs, the highest resolution is used:
370370
In [2]: pd.to_datetime([pd.Timestamp("2024-03-22 11:43:01"), "2024-03-22 11:43:01.002"]).dtype
371371
Out[2]: dtype('<M8[ns]')
372372
373+
.. _whatsnew_300.api_breaking.concat_datetime_sorting:
374+
375+
:func:`concat` no longer ignores ``sort`` when all objects have a :class:`DatetimeIndex`
376+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
377+
378+
When all objects passed to :func:`concat` have a :class:`DatetimeIndex`, the ``sort``
379+
argument is no longer ignored. Previously, the result would always be sorted along
380+
the non-concatenation axis even when ``sort=False`` (the default).
381+
382+
.. ipython:: python
383+
384+
idx1 = pd.date_range("2025-01-02", periods=3, freq="h")
385+
df1 = pd.DataFrame({"a": [1, 2, 3]}, index=idx1)
386+
df1
387+
388+
idx2 = pd.date_range("2025-01-01", periods=3, freq="h")
389+
df2 = pd.DataFrame({"b": [1, 2, 3]}, index=idx2)
390+
df2
391+
392+
*Old behavior*
393+
394+
.. code-block:: ipython
395+
396+
In [3]: pd.concat([df1, df2], axis=1, sort=False)
397+
Out[3]:
398+
a b
399+
2025-01-01 00:00:00 NaN 1.0
400+
2025-01-01 01:00:00 NaN 2.0
401+
2025-01-01 02:00:00 NaN 3.0
402+
2025-01-02 00:00:00 1.0 NaN
403+
2025-01-02 01:00:00 2.0 NaN
404+
2025-01-02 02:00:00 3.0 NaN
405+
406+
*New behavior*
407+
408+
.. ipython:: python
409+
410+
pd.concat([df1, df2], axis=1, sort=False)
411+
373412
.. _whatsnew_300.api_breaking.value_counts_sorting:
374413

375414
Changed behavior in :meth:`DataFrame.value_counts` and :meth:`DataFrameGroupBy.value_counts` when ``sort=False``

pandas/core/indexes/api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ 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
231230
result = indexes[0]
232231

233232
elif num_dtis > 1:

pandas/tests/reshape/concat/test_datetimes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ def test_concat_datetime_timezone(self):
7373

7474
exp_idx = DatetimeIndex(
7575
[
76-
"2010-12-31 15:00:00+00:00",
77-
"2010-12-31 16:00:00+00:00",
78-
"2010-12-31 17:00:00+00:00",
7976
"2010-12-31 23:00:00+00:00",
8077
"2011-01-01 00:00:00+00:00",
8178
"2011-01-01 01:00:00+00:00",
79+
"2010-12-31 15:00:00+00:00",
80+
"2010-12-31 16:00:00+00:00",
81+
"2010-12-31 17:00:00+00:00",
8282
]
8383
).as_unit("ns")
8484

8585
expected = DataFrame(
8686
[
87-
[np.nan, 1],
88-
[np.nan, 2],
89-
[np.nan, 3],
9087
[1, np.nan],
9188
[2, np.nan],
9289
[3, np.nan],
90+
[np.nan, 1],
91+
[np.nan, 2],
92+
[np.nan, 3],
9393
],
9494
index=exp_idx,
9595
columns=["a", "b"],

0 commit comments

Comments
 (0)