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

Function top_k_unstable

python/pyarrow/compute.py:570–619  ·  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

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

Callers

nothing calls this directly

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected