Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 18 additions & 8 deletions python/pyarrow/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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)

Expand All @@ -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.

Expand All @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading