Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,45 @@ In cases with mixed-resolution inputs, the highest resolution is used:
In [2]: pd.to_datetime([pd.Timestamp("2024-03-22 11:43:01"), "2024-03-22 11:43:01.002"]).dtype
Out[2]: dtype('<M8[ns]')

.. _whatsnew_300.api_breaking.concat_datetime_sorting:

:func:`concat` no longer ignores ``sort`` when all objects have a :class:`DatetimeIndex`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

When all objects passed to :func:`concat` have a :class:`DatetimeIndex`, the ``sort``
argument is no longer ignored. Previously, the result would always be sorted along
the non-concatenation axis even when ``sort=False`` (the default).

.. ipython:: python

idx1 = pd.date_range("2025-01-02", periods=3, freq="h")
df1 = pd.DataFrame({"a": [1, 2, 3]}, index=idx1)
df1

idx2 = pd.date_range("2025-01-01", periods=3, freq="h")
df2 = pd.DataFrame({"b": [1, 2, 3]}, index=idx2)
df2

*Old behavior*

.. code-block:: ipython

In [3]: pd.concat([df1, df2], axis=1, sort=False)
Out[3]:
a b
2025-01-01 00:00:00 NaN 1.0
2025-01-01 01:00:00 NaN 2.0
2025-01-01 02:00:00 NaN 3.0
2025-01-02 00:00:00 1.0 NaN
2025-01-02 01:00:00 2.0 NaN
2025-01-02 02:00:00 3.0 NaN

*New behavior*

.. ipython:: python

pd.concat([df1, df2], axis=1, sort=False)

.. _whatsnew_300.api_breaking.value_counts_sorting:

Changed behavior in :meth:`DataFrame.value_counts` and :meth:`DataFrameGroupBy.value_counts` when ``sort=False``
Expand Down
1 change: 0 additions & 1 deletion pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ def union_indexes(indexes, sort: bool | None = True) -> Index:
raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex")

if num_dtis == len(indexes):
sort = True
result = indexes[0]

elif num_dtis > 1:
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/reshape/concat/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ def test_concat_datetime_timezone(self):

exp_idx = DatetimeIndex(
[
"2010-12-31 15:00:00+00:00",
"2010-12-31 16:00:00+00:00",
"2010-12-31 17:00:00+00:00",
"2010-12-31 23:00:00+00:00",
"2011-01-01 00:00:00+00:00",
"2011-01-01 01:00:00+00:00",
"2010-12-31 15:00:00+00:00",
"2010-12-31 16:00:00+00:00",
"2010-12-31 17:00:00+00:00",
]
).as_unit("ns")

expected = DataFrame(
[
[np.nan, 1],
[np.nan, 2],
[np.nan, 3],
[1, np.nan],
[2, np.nan],
[3, np.nan],
[np.nan, 1],
[np.nan, 2],
[np.nan, 3],
],
index=exp_idx,
columns=["a", "b"],
Expand Down
Loading