From 431e7f2135002a78b36d52ebe1568926a32b9a37 Mon Sep 17 00:00:00 2001 From: Rok Mihevc Date: Fri, 13 Nov 2026 12:24:26 +0100 Subject: [PATCH] [Python] Default null_placements to "at_end" in top_k_unstable/bottom_k_unstable Passing sort_keys without null_placements for table-like input raised TypeError since GH-38558 (#46926) added the null_placements parameter. Default every sort key to "at_end", as the array branch does, and use zip(strict=True) so mismatched lengths raise instead of silently dropping sort keys. --- python/pyarrow/compute.py | 26 ++++++++++++++++++-------- python/pyarrow/tests/test_compute.py | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/python/pyarrow/compute.py b/python/pyarrow/compute.py index 09de66c901b3..be99cbfd4727 100644 --- a/python/pyarrow/compute.py +++ b/python/pyarrow/compute.py @@ -584,9 +584,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". + null_placements : List-like of str, optional + Whether nulls and NaNs are placed at the start or at the end, one + entry per sort key when input is table-like data. + Accepted values are "at_end", "at_start". Defaults to "at_end" + for every sort key. memory_pool : MemoryPool, optional If not passed, will allocate memory from the default memory pool. @@ -613,8 +615,11 @@ def top_k_unstable( if isinstance(values, (pa.Array, pa.ChunkedArray)): sort_keys.append(("dummy", "descending", "at_end")) else: + if null_placements is None: + null_placements = ["at_end"] * len(sort_keys) sort_keys = [(sort_key, "descending", null_placement) - for sort_key, null_placement in zip(sort_keys, null_placements)] + for sort_key, null_placement + in zip(sort_keys, null_placements, strict=True)] options = SelectKOptions(k, sort_keys) return call_function("select_k_unstable", [values], options, memory_pool) @@ -636,9 +641,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". + null_placements : List-like of str, optional + Whether nulls and NaNs are placed at the start or at the end, one + entry per sort key when input is table-like data. + Accepted values are "at_end", "at_start". Defaults to "at_end" + for every sort key. memory_pool : MemoryPool, optional If not passed, will allocate memory from the default memory pool. @@ -665,8 +672,11 @@ def bottom_k_unstable( if isinstance(values, (pa.Array, pa.ChunkedArray)): sort_keys.append(("dummy", "ascending", "at_end")) else: + if null_placements is None: + null_placements = ["at_end"] * len(sort_keys) sort_keys = [(sort_key, "ascending", null_placement) - for sort_key, null_placement in zip(sort_keys, null_placements)] + for sort_key, null_placement + in zip(sort_keys, null_placements, strict=True)] 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 691d2ec98f33..a94b21ab1f92 100644 --- a/python/pyarrow/tests/test_compute.py +++ b/python/pyarrow/tests/test_compute.py @@ -3251,6 +3251,29 @@ 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")]) +@pytest.mark.parametrize("make_data", [pa.table, pa.record_batch]) +def test_top_bottom_k_unstable_default_null_placements(make_data): + # null_placements should default to "at_end" for every sort key + data = make_data({"a": [3, None, 1, 2], "b": [0, 1, 0, 1]}) + + result = pc.top_k_unstable(data, 2, ["a"]) + assert result == pc.top_k_unstable(data, 2, ["a"], ["at_end"]) + assert pc.take(data, result)["a"].to_pylist() == [3, 2] + + result = pc.bottom_k_unstable(data, 2, ["a"]) + assert result == pc.bottom_k_unstable(data, 2, ["a"], ["at_end"]) + assert pc.take(data, result)["a"].to_pylist() == [1, 2] + + # Nulls end up last with the default placement + result = pc.bottom_k_unstable(data, 4, ["a", "b"]) + assert pc.take(data, result)["a"].to_pylist() == [1, 2, 3, None] + + with pytest.raises(ValueError, match="zip"): + pc.top_k_unstable(data, 2, ["a", "b"], ["at_end"]) + with pytest.raises(ValueError, match="zip"): + pc.bottom_k_unstable(data, 2, ["a"], ["at_end", "at_start"]) + + def test_array_sort_indices(): arr = pa.array([1, 2, None, 0]) result = pc.array_sort_indices(arr)