MCPcopy Create free account
hub / github.com/apache/arrow / test_select_k_table

Function test_select_k_table

python/pyarrow/tests/test_compute.py:3206–3259  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3204
3205
3206def test_select_k_table():
3207 def validate_select_k(select_k_indices, tbl, sort_keys, stable_sort=False):
3208 sorted_indices = pc.sort_indices(tbl, sort_keys=sort_keys)
3209 head_k_indices = sorted_indices.slice(0, len(select_k_indices))
3210 if stable_sort:
3211 assert select_k_indices == head_k_indices
3212 else:
3213 expected = pc.take(tbl, head_k_indices)
3214 actual = pc.take(tbl, select_k_indices)
3215 assert actual == expected
3216
3217 table = pa.table({"a": [1, 2, 0], "b": [1, 0, 1]})
3218 for k in [0, 2, 4]:
3219 result = pc.select_k_unstable(
3220 table, k=k, sort_keys=[("a", "ascending", "at_end")])
3221 validate_select_k(result, table, sort_keys=[("a", "ascending", "at_end")])
3222
3223 result = pc.select_k_unstable(
3224 table, k=k, sort_keys=[(pc.field("a"), "ascending", "at_end"),
3225 ("b", "ascending", "at_end")])
3226 validate_select_k(
3227 result, table, sort_keys=[("a", "ascending", "at_end"),
3228 ("b", "ascending", "at_end")])
3229
3230 result = pc.top_k_unstable(table, k=k, sort_keys=[
3231 "a"], null_placements=["at_end"])
3232 validate_select_k(result, table, sort_keys=[("a", "descending", "at_end")])
3233
3234 result = pc.bottom_k_unstable(
3235 table, k=k, sort_keys=["a", "b"], null_placements=["at_end", "at_start"])
3236 validate_select_k(
3237 result, table, sort_keys=[("a", "ascending", "at_end"), ("b", "ascending",
3238 "at_start")])
3239
3240 with pytest.raises(
3241 ValueError,
3242 match="'select_k_unstable' cannot be called without options"):
3243 pc.select_k_unstable(table)
3244
3245 with pytest.raises(ValueError,
3246 match="select_k_unstable requires a nonnegative `k`"):
3247 pc.select_k_unstable(table, k=-1, sort_keys=[("a", "ascending", "at_end")])
3248
3249 with pytest.raises(ValueError,
3250 match="select_k_unstable requires a "
3251 "non-empty `sort_keys`"):
3252 pc.select_k_unstable(table, k=2, sort_keys=[])
3253
3254 with pytest.raises(ValueError, match="not a valid sort order"):
3255 pc.select_k_unstable(table, k=k, sort_keys=[("a", "nonscending", "at_end")])
3256
3257 with pytest.raises(ValueError,
3258 match="Invalid sort key column: No match for.*unknown"):
3259 pc.select_k_unstable(table, k=k, sort_keys=[("unknown", "ascending", "at_end")])
3260
3261
3262def test_array_sort_indices():

Callers

nothing calls this directly

Calls 2

validate_select_kFunction · 0.85
fieldMethod · 0.45

Tested by

no test coverage detected