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

Function top_k_unstable

python/pyarrow/compute.py:571–620  ·  view source on GitHub ↗

Select the indices of the top-k ordered elements from array- or table-like data. This is a specialization for :func:`select_k_unstable`. Output is not guaranteed to be stable. Parameters ---------- values : Array, ChunkedArray, RecordBatch, or Table Data to sor

(
        values, k, sort_keys=None, null_placements=None, *, memory_pool=None)

Source from the content-addressed store, hash-verified

569
570
571def top_k_unstable(
572 values, k, sort_keys=None, null_placements=None, *, memory_pool=None):
573 """
574 Select the indices of the top-k ordered elements from array- or table-like
575 data.
576
577 This is a specialization for :func:`select_k_unstable`. Output is not
578 guaranteed to be stable.
579
580 Parameters
581 ----------
582 values : Array, ChunkedArray, RecordBatch, or Table
583 Data to sort and get top indices from.
584 k : int
585 The number of `k` elements to keep.
586 sort_keys : List-like
587 Column key names to order by when input is table-like data.
588 null_placements : A list of "at_start" or "at_end"
589 Whether nulls and NaNs are placed at the start or at the end.
590 Accepted values are "at_end", "at_start".
591 memory_pool : MemoryPool, optional
592 If not passed, will allocate memory from the default memory pool.
593
594 Returns
595 -------
596 result : Array
597 Indices of the top-k ordered elements
598
599 Examples
600 --------
601 >>> import pyarrow as pa
602 >>> import pyarrow.compute as pc
603 >>> arr = pa.array(["a", "b", "c", None, "e", "f"])
604 >>> pc.top_k_unstable(arr, k=3)
605 <pyarrow.lib.UInt64Array object at ...>
606 [
607 5,
608 4,
609 2
610 ]
611 """
612 if sort_keys is None:
613 sort_keys = []
614 if isinstance(values, (pa.Array, pa.ChunkedArray)):
615 sort_keys.append(("dummy", "descending", "at_end"))
616 else:
617 sort_keys = [(sort_key, "descending", null_placement)
618 for sort_key, null_placement in zip(sort_keys, null_placements)]
619 options = SelectKOptions(k, sort_keys)
620 return call_function("select_k_unstable", [values], options, memory_pool)
621
622
623def bottom_k_unstable(

Callers

nothing calls this directly

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected