diff --git a/python/pyarrow/compute.py b/python/pyarrow/compute.py index 09de66c901b..f287cde79f9 100644 --- a/python/pyarrow/compute.py +++ b/python/pyarrow/compute.py @@ -567,6 +567,35 @@ def fill_null(values, fill_value): return call_function("coalesce", [values, fill_value]) +def _select_k_sort_keys(values, sort_keys, null_placements, order): + if isinstance(sort_keys, str) or isinstance(null_placements, str): + raise TypeError( + "sort_keys and null_placements must be list-like, not str") + + if isinstance(values, (pa.Array, pa.ChunkedArray)): + if sort_keys is not None: + raise ValueError( + "sort_keys is only supported for table-like input") + if null_placements is None: + return [("dummy", order, "at_end")] + else: + null_placements = list(null_placements) + if len(null_placements) != 1: + raise ValueError( + "null_placements must have one element for array input") + return [("dummy", order, null_placements[0])] + + sort_keys = list(sort_keys) if sort_keys is not None else [] + if null_placements is None: + null_placements = ["at_end"] * len(sort_keys) + else: + null_placements = list(null_placements) + if len(null_placements) != len(sort_keys): + raise ValueError( + "sort_keys and null_placements must have the same length") + return [(key, order, null_placements[i]) for i, key in enumerate(sort_keys)] + + def top_k_unstable( values, k, sort_keys=None, null_placements=None, *, memory_pool=None): """ @@ -584,9 +613,11 @@ def top_k_unstable( The number of `k` elements to keep. sort_keys : List-like Column key names to order by when input is table-like data. - null_placements : A list of "at_start" or "at_end" - Whether nulls and NaNs are placed at the start or at the end. - Accepted values are "at_end", "at_start". + Not supported for array-like input. + null_placements : List-like of "at_start" or "at_end", optional + Whether nulls and NaNs are placed at the start or at the end, + for each key in `sort_keys` or single element for array input. + If not passed, "at_end" is used for every key. memory_pool : MemoryPool, optional If not passed, will allocate memory from the default memory pool. @@ -608,13 +639,8 @@ def top_k_unstable( 2 ] """ - if sort_keys is None: - sort_keys = [] - if isinstance(values, (pa.Array, pa.ChunkedArray)): - sort_keys.append(("dummy", "descending", "at_end")) - else: - sort_keys = [(sort_key, "descending", null_placement) - for sort_key, null_placement in zip(sort_keys, null_placements)] + sort_keys = _select_k_sort_keys( + values, sort_keys, null_placements, "descending") options = SelectKOptions(k, sort_keys) return call_function("select_k_unstable", [values], options, memory_pool) @@ -636,9 +662,11 @@ def bottom_k_unstable( The number of `k` elements to keep. sort_keys : List-like Column key names to order by when input is table-like data. - null_placements : A list of "at_start" or "at_end" - Whether nulls and NaNs are placed at the start or at the end. - Accepted values are "at_end", "at_start". + Not supported for array-like input. + null_placements : List-like of "at_start" or "at_end", optional + Whether nulls and NaNs are placed at the start or at the end, + for each key in `sort_keys` or single element for array input. + If not passed, "at_end" is used for every key. memory_pool : MemoryPool, optional If not passed, will allocate memory from the default memory pool. @@ -660,14 +688,8 @@ def bottom_k_unstable( 2 ] """ - if sort_keys is None: - sort_keys = [] - if isinstance(values, (pa.Array, pa.ChunkedArray)): - sort_keys.append(("dummy", "ascending", "at_end")) - else: - sort_keys = [(sort_key, "ascending", null_placement) - for sort_key, null_placement in zip(sort_keys, null_placements)] - + sort_keys = _select_k_sort_keys( + values, sort_keys, null_placements, "ascending") options = SelectKOptions(k, sort_keys) return call_function("select_k_unstable", [values], options, memory_pool) diff --git a/python/pyarrow/tests/test_compute.py b/python/pyarrow/tests/test_compute.py index 0e0260808d6..cade7f62270 100644 --- a/python/pyarrow/tests/test_compute.py +++ b/python/pyarrow/tests/test_compute.py @@ -3251,6 +3251,40 @@ def validate_select_k(select_k_indices, tbl, sort_keys, stable_sort=False): pc.select_k_unstable(table, k=k, sort_keys=[("unknown", "ascending", "at_end")]) +def test_top_k_bottom_k_(): + arr = pa.array([1, 2, None, 0]) + table = pa.table({"a": [1, 2, None], "b": [None, 0, 1]}) + + # null_placements defaults to "at_end" for every key + assert pc.top_k_unstable(table, k=2, sort_keys=["a"]).to_pylist() == [1, 0] + assert pc.bottom_k_unstable( + table, k=2, sort_keys=("a", "b")).to_pylist() == [0, 1] + + # null_placements "at_start" is not ignored for array input + assert pc.top_k_unstable(arr, k=2, null_placements=[ + "at_start"]).to_pylist() == [2, 1] + assert pc.bottom_k_unstable(arr, k=2, null_placements=[ + "at_start"]).to_pylist() == [2, 3] + + for func in [pc.top_k_unstable, pc.bottom_k_unstable]: + sort_keys = ["a"] + with pytest.raises(ValueError, match="only supported for table-like"): + func(arr, k=2, sort_keys=sort_keys) + with pytest.raises(ValueError, match="must have one element"): + func(arr, k=2, null_placements=["at_start", "at_end"]) + with pytest.raises(TypeError, match="must be list-like, not str"): + func(arr, k=2, null_placements="at_start") + with pytest.raises(TypeError, match="'int' object is not iterable"): + func(arr, k=2, null_placements=1) + assert sort_keys == ["a"] # list is not mutated + with pytest.raises(TypeError, match="must be list-like"): + func(table, k=2, sort_keys="a") + with pytest.raises(TypeError, match="must be list-like"): + func(table, k=2, sort_keys=["a"], null_placements="at_end") + with pytest.raises(ValueError, match="same length"): + func(table, k=2, sort_keys=["a", "b"], null_placements=["at_end"]) + + def test_array_sort_indices(): arr = pa.array([1, 2, None, 0]) result = pc.array_sort_indices(arr)