Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update fix ignored sort in api.py and add test #43833

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
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
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def is_integer_array(values: np.ndarray, skipna: bool = ...): ...
def is_bool_array(values: np.ndarray, skipna: bool = ...): ...
def fast_multiget(mapping: dict, keys: np.ndarray, default=...) -> np.ndarray: ...
def fast_unique_multiple_list_gen(gen: Generator, sort: bool = ...) -> list: ...
def fast_unique_multiple_list(lists: list, sort: bool = ...) -> list: ...
def fast_unique_multiple_list(lists: list, sort: bool | None = ...) -> list: ...
def fast_unique_multiple(arrays: list, sort: bool = ...) -> list: ...
def map_infer(
arr: np.ndarray,
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def fast_unique_multiple(list arrays, sort: bool = True):

@cython.wraparound(False)
@cython.boundscheck(False)
def fast_unique_multiple_list(lists: list, sort: bool = True) -> list:
def fast_unique_multiple_list(lists: list, sort: bool | None = True) -> list:
cdef:
list buf
Py_ssize_t k = len(lists)
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import textwrap
from typing import Optional

from pandas._libs import (
NaT,
Expand Down Expand Up @@ -147,7 +148,7 @@ def _get_combined_index(
for other in indexes[1:]:
index = index.intersection(other)
else:
index = union_indexes(indexes, sort=sort)
index = union_indexes(indexes, sort=False)
index = ensure_index(index)

if sort:
Expand All @@ -163,7 +164,7 @@ def _get_combined_index(
return index


def union_indexes(indexes, sort: bool = True) -> Index:
def union_indexes(indexes, sort: bool | None = True) -> Index:
"""
Return the union of indexes.

Expand Down Expand Up @@ -219,7 +220,7 @@ def conv(i):
return result.union_many(indexes[1:])
else:
for other in indexes[1:]:
result = result.union(other)
result = result.union(other, sort=None if sort else False)
return result
elif kind == "array":
index = indexes[0]
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2354,10 +2354,8 @@ def test_construct_with_two_categoricalindex_series(self):
)
result = DataFrame([s1, s2])
expected = DataFrame(
np.array(
[[np.nan, 39.0, np.nan, 6.0, 4.0], [2.0, 152.0, 2.0, 242.0, 150.0]]
),
columns=["f", "female", "m", "male", "unknown"],
np.array([[39, 6, 4, np.nan, np.nan], [152.0, 242.0, 150.0, 2.0, 2.0]]),
columns=["female", "male", "unknown", "f", "m"],
)
tm.assert_frame_equal(result, expected)

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/reshape/concat/test_sort.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy as np

import pandas as pd
from pandas import DataFrame
import pandas._testing as tm
Expand Down Expand Up @@ -81,3 +83,12 @@ def test_concat_aligned_sort_does_not_raise(self):
expected = DataFrame({1: [1, 2, 1, 2], "a": [3, 4, 3, 4]}, columns=[1, "a"])
result = pd.concat([df, df], ignore_index=True, sort=True)
tm.assert_frame_equal(result, expected)

def test_concat_frame_with_sort_false(self):
# GH 43375
result = pd.concat(
[pd.DataFrame({i: i}, index=[i]) for i in range(2, 0, -1)], sort=False
)
expected = DataFrame([[2, np.nan], [np.nan, 1]], index=[2, 1], columns=[2, 1])

tm.assert_frame_equal(result, expected)
26 changes: 13 additions & 13 deletions pandas/tests/reshape/test_melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,11 +746,11 @@ def test_unbalanced(self):
)
df["id"] = df.index
exp_data = {
"X": ["X1", "X1", "X2", "X2"],
"A": [1.0, 3.0, 2.0, 4.0],
"B": [5.0, np.nan, 6.0, np.nan],
"id": [0, 0, 1, 1],
"year": [2010, 2011, 2010, 2011],
"X": ["X1", "X2", "X1", "X2"],
"A": [1.0, 2.0, 3.0, 4.0],
"B": [5.0, 6.0, np.nan, np.nan],
"id": [0, 1, 0, 1],
"year": [2010, 2010, 2011, 2011],
}
expected = DataFrame(exp_data)
expected = expected.set_index(["id", "year"])[["X", "A", "B"]]
Expand Down Expand Up @@ -993,10 +993,10 @@ def test_nonnumeric_suffix(self):
)
expected = DataFrame(
{
"A": ["X1", "X1", "X2", "X2"],
"colname": ["placebo", "test", "placebo", "test"],
"result": [5.0, np.nan, 6.0, np.nan],
"treatment": [1.0, 3.0, 2.0, 4.0],
"A": ["X1", "X2", "X1", "X2"],
"colname": ["placebo", "placebo", "test", "test"],
"result": [5.0, 6.0, np.nan, np.nan],
"treatment": [1.0, 2.0, 3.0, 4.0],
}
)
expected = expected.set_index(["A", "colname"])
Expand Down Expand Up @@ -1040,10 +1040,10 @@ def test_float_suffix(self):
)
expected = DataFrame(
{
"A": ["X1", "X1", "X1", "X1", "X2", "X2", "X2", "X2"],
"colname": [1, 1.1, 1.2, 2.1, 1, 1.1, 1.2, 2.1],
"result": [0.0, np.nan, 5.0, np.nan, 9.0, np.nan, 6.0, np.nan],
"treatment": [np.nan, 1.0, np.nan, 3.0, np.nan, 2.0, np.nan, 4.0],
"A": ["X1", "X2", "X1", "X2", "X1", "X2", "X1", "X2"],
"colname": [1.2, 1.2, 1.0, 1.0, 1.1, 1.1, 2.1, 2.1],
"result": [5.0, 6.0, 0.0, 9.0, np.nan, np.nan, np.nan, np.nan],
"treatment": [np.nan, np.nan, np.nan, np.nan, 1.0, 2.0, 3.0, 4.0],
}
)
expected = expected.set_index(["A", "colname"])
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/strings/test_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,11 @@ def test_str_cat_align_mixed_inputs(join):
expected_outer = Series(["aaA", "bbB", "c-C", "ddD", "-e-"])
# joint index of rhs [t, u]; u will be forced have index of s
rhs_idx = (
t.index.intersection(s.index) if join == "inner" else t.index.union(s.index)
t.index.intersection(s.index)
if join == "inner"
else t.index.union(s.index)
if join == "outer"
else t.index.append(s.index.difference(t.index))
)

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