Skip to content

Commit 01b8d2a

Browse files
authored
update fix ignored sort in api.py and add test (#43833)
1 parent b49ff3e commit 01b8d2a

File tree

8 files changed

+37
-23
lines changed

8 files changed

+37
-23
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ Reshaping
513513
- Bug in :func:`concat` of ``bool`` and ``boolean`` dtypes resulting in ``object`` dtype instead of ``boolean`` dtype (:issue:`42800`)
514514
- Bug in :func:`crosstab` when inputs are are categorical Series, there are categories that are not present in one or both of the Series, and ``margins=True``. Previously the margin value for missing categories was ``NaN``. It is now correctly reported as 0 (:issue:`43505`)
515515
- Bug in :func:`concat` would fail when the ``objs`` argument all had the same index and the ``keys`` argument contained duplicates (:issue:`43595`)
516+
- Bug in :func:`concat` which ignored the ``sort`` parameter (:issue:`43375`)
516517

517518
Sparse
518519
^^^^^^

pandas/_libs/lib.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def is_integer_array(values: np.ndarray, skipna: bool = ...): ...
5555
def is_bool_array(values: np.ndarray, skipna: bool = ...): ...
5656
def fast_multiget(mapping: dict, keys: np.ndarray, default=...) -> np.ndarray: ...
5757
def fast_unique_multiple_list_gen(gen: Generator, sort: bool = ...) -> list: ...
58-
def fast_unique_multiple_list(lists: list, sort: bool = ...) -> list: ...
58+
def fast_unique_multiple_list(lists: list, sort: bool | None = ...) -> list: ...
5959
def fast_unique_multiple(arrays: list, sort: bool = ...) -> list: ...
6060
def map_infer(
6161
arr: np.ndarray,

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def fast_unique_multiple(list arrays, sort: bool = True):
350350

351351
@cython.wraparound(False)
352352
@cython.boundscheck(False)
353-
def fast_unique_multiple_list(lists: list, sort: bool = True) -> list:
353+
def fast_unique_multiple_list(lists: list, sort: bool | None = True) -> list:
354354
cdef:
355355
list buf
356356
Py_ssize_t k = len(lists)

pandas/core/indexes/api.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def _get_combined_index(
147147
for other in indexes[1:]:
148148
index = index.intersection(other)
149149
else:
150-
index = union_indexes(indexes, sort=sort)
150+
index = union_indexes(indexes, sort=False)
151151
index = ensure_index(index)
152152

153153
if sort:
@@ -163,7 +163,7 @@ def _get_combined_index(
163163
return index
164164

165165

166-
def union_indexes(indexes, sort: bool = True) -> Index:
166+
def union_indexes(indexes, sort: bool | None = True) -> Index:
167167
"""
168168
Return the union of indexes.
169169
@@ -219,7 +219,7 @@ def conv(i):
219219
return result.union_many(indexes[1:])
220220
else:
221221
for other in indexes[1:]:
222-
result = result.union(other)
222+
result = result.union(other, sort=None if sort else False)
223223
return result
224224
elif kind == "array":
225225
index = indexes[0]

pandas/tests/frame/test_constructors.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2354,10 +2354,8 @@ def test_construct_with_two_categoricalindex_series(self):
23542354
)
23552355
result = DataFrame([s1, s2])
23562356
expected = DataFrame(
2357-
np.array(
2358-
[[np.nan, 39.0, np.nan, 6.0, 4.0], [2.0, 152.0, 2.0, 242.0, 150.0]]
2359-
),
2360-
columns=["f", "female", "m", "male", "unknown"],
2357+
np.array([[39, 6, 4, np.nan, np.nan], [152.0, 242.0, 150.0, 2.0, 2.0]]),
2358+
columns=["female", "male", "unknown", "f", "m"],
23612359
)
23622360
tm.assert_frame_equal(result, expected)
23632361

pandas/tests/reshape/concat/test_sort.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import numpy as np
2+
13
import pandas as pd
24
from pandas import DataFrame
35
import pandas._testing as tm
@@ -81,3 +83,12 @@ def test_concat_aligned_sort_does_not_raise(self):
8183
expected = DataFrame({1: [1, 2, 1, 2], "a": [3, 4, 3, 4]}, columns=[1, "a"])
8284
result = pd.concat([df, df], ignore_index=True, sort=True)
8385
tm.assert_frame_equal(result, expected)
86+
87+
def test_concat_frame_with_sort_false(self):
88+
# GH 43375
89+
result = pd.concat(
90+
[DataFrame({i: i}, index=[i]) for i in range(2, 0, -1)], sort=False
91+
)
92+
expected = DataFrame([[2, np.nan], [np.nan, 1]], index=[2, 1], columns=[2, 1])
93+
94+
tm.assert_frame_equal(result, expected)

pandas/tests/reshape/test_melt.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,11 @@ def test_unbalanced(self):
746746
)
747747
df["id"] = df.index
748748
exp_data = {
749-
"X": ["X1", "X1", "X2", "X2"],
750-
"A": [1.0, 3.0, 2.0, 4.0],
751-
"B": [5.0, np.nan, 6.0, np.nan],
752-
"id": [0, 0, 1, 1],
753-
"year": [2010, 2011, 2010, 2011],
749+
"X": ["X1", "X2", "X1", "X2"],
750+
"A": [1.0, 2.0, 3.0, 4.0],
751+
"B": [5.0, 6.0, np.nan, np.nan],
752+
"id": [0, 1, 0, 1],
753+
"year": [2010, 2010, 2011, 2011],
754754
}
755755
expected = DataFrame(exp_data)
756756
expected = expected.set_index(["id", "year"])[["X", "A", "B"]]
@@ -993,10 +993,10 @@ def test_nonnumeric_suffix(self):
993993
)
994994
expected = DataFrame(
995995
{
996-
"A": ["X1", "X1", "X2", "X2"],
997-
"colname": ["placebo", "test", "placebo", "test"],
998-
"result": [5.0, np.nan, 6.0, np.nan],
999-
"treatment": [1.0, 3.0, 2.0, 4.0],
996+
"A": ["X1", "X2", "X1", "X2"],
997+
"colname": ["placebo", "placebo", "test", "test"],
998+
"result": [5.0, 6.0, np.nan, np.nan],
999+
"treatment": [1.0, 2.0, 3.0, 4.0],
10001000
}
10011001
)
10021002
expected = expected.set_index(["A", "colname"])
@@ -1040,10 +1040,10 @@ def test_float_suffix(self):
10401040
)
10411041
expected = DataFrame(
10421042
{
1043-
"A": ["X1", "X1", "X1", "X1", "X2", "X2", "X2", "X2"],
1044-
"colname": [1, 1.1, 1.2, 2.1, 1, 1.1, 1.2, 2.1],
1045-
"result": [0.0, np.nan, 5.0, np.nan, 9.0, np.nan, 6.0, np.nan],
1046-
"treatment": [np.nan, 1.0, np.nan, 3.0, np.nan, 2.0, np.nan, 4.0],
1043+
"A": ["X1", "X2", "X1", "X2", "X1", "X2", "X1", "X2"],
1044+
"colname": [1.2, 1.2, 1.0, 1.0, 1.1, 1.1, 2.1, 2.1],
1045+
"result": [5.0, 6.0, 0.0, 9.0, np.nan, np.nan, np.nan, np.nan],
1046+
"treatment": [np.nan, np.nan, np.nan, np.nan, 1.0, 2.0, 3.0, 4.0],
10471047
}
10481048
)
10491049
expected = expected.set_index(["A", "colname"])

pandas/tests/strings/test_cat.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ def test_str_cat_align_mixed_inputs(join):
278278
expected_outer = Series(["aaA", "bbB", "c-C", "ddD", "-e-"])
279279
# joint index of rhs [t, u]; u will be forced have index of s
280280
rhs_idx = (
281-
t.index.intersection(s.index) if join == "inner" else t.index.union(s.index)
281+
t.index.intersection(s.index)
282+
if join == "inner"
283+
else t.index.union(s.index)
284+
if join == "outer"
285+
else t.index.append(s.index.difference(t.index))
282286
)
283287

284288
expected = expected_outer.loc[s.index.join(rhs_idx, how=join)]

0 commit comments

Comments
 (0)