Skip to content

Commit 6be14dc

Browse files
lukemanleypmhatre1
authored andcommitted
REGR/DOC: pd.concat should special case DatetimeIndex to sort even when sort=False (pandas-dev#57250)
1 parent b00689e commit 6be14dc

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- Fixed memory leak in :func:`read_csv` (:issue:`57039`)
1717
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
18+
- Fixed regression in :func:`concat` changing long-standing behavior that always sorted the non-concatenation axis when the axis was a :class:`DatetimeIndex` (:issue:`57006`)
1819
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
1920
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
2021
- Fixed regression in :meth:`.DataFrameGroupBy.idxmin`, :meth:`.DataFrameGroupBy.idxmax`, :meth:`.SeriesGroupBy.idxmin`, :meth:`.SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)

pandas/core/indexes/api.py

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ def _find_common_index_dtype(inds):
295295
raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex")
296296

297297
if len(dtis) == len(indexes):
298+
sort = True
298299
result = indexes[0]
299300

300301
elif len(dtis) > 1:

pandas/core/reshape/concat.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ def concat(
205205
Check whether the new concatenated axis contains duplicates. This can
206206
be very expensive relative to the actual data concatenation.
207207
sort : bool, default False
208-
Sort non-concatenation axis if it is not already aligned.
209-
208+
Sort non-concatenation axis if it is not already aligned. One exception to
209+
this is when the non-concatentation axis is a DatetimeIndex and join='outer'
210+
and the axis is not already aligned. In that case, the non-concatenation
211+
axis is always sorted lexicographically.
210212
copy : bool, default True
211213
If False, do not copy data unnecessarily.
212214

pandas/tests/reshape/concat/test_datetimes.py

+6-6
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 23:00:00+00:00",
77-
"2011-01-01 00:00:00+00:00",
78-
"2011-01-01 01:00:00+00:00",
7976
"2010-12-31 15:00:00+00:00",
8077
"2010-12-31 16:00:00+00:00",
8178
"2010-12-31 17:00:00+00:00",
79+
"2010-12-31 23:00:00+00:00",
80+
"2011-01-01 00:00:00+00:00",
81+
"2011-01-01 01:00:00+00:00",
8282
]
8383
).as_unit("ns")
8484

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

0 commit comments

Comments
 (0)