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