On Wed, 2026-06-17 at 14:55 -0400, Maanas Arora via NumPy-Discussion
wrote:
> Hello all,
>
> Following several discussions (linked below) about the proposal, it
> was
> decided to add a top_k function to NumPy in issue #15128
> <https://github.com/numpy/numpy/issues/15128>. I have recently made a
> PR (
> https://github.com/numpy/numpy/pull/31659) to revive this feature
> which is
> now ready to review, and wanted to ping here for awareness and if
> anyone
> has comments.
Thanks for this, we'll probably merge this very soon.
I think there was long an agreement around a `top_k` addition. But I
don't think the final API proposal hit the list so here is a very brief
summary:
top_k(a, k, /, *, axis=-1, mode="largest"|"smallest", sorted=True)
returning a tuple: `(topk_values, topk_indices)`
Besides from the above, one detail is that the the result would
generally omit NaNs so that the result only contains NaNs if there are
fewer than `k` non-NaN values. Omitting NaNs seemed more useful and
matching to sorting but of course differs from other reduction defaults
including min/max as these have `nanmin/nanmax`.
Happy to hear final thoughts even if the above is the default by now.
Cheers,
Sebastian
>
> Thank you,
> Maanas Arora
>
> ---
>
> Links:
>
> - Mailing list discussions:
> -
>
> https://mail.python.org/archives/list/[email protected]/thread/F4P5UVTAKRJJ3OORI6UOWFSUEE5CNTSC/#UWULPEEVC4SRHVW37MSXJJKZL6YUSUFU
> -
>
> https://mail.python.org/archives/list/[email protected]/thread/TCRBG6VOHHFMV3CWR3AFTVZK4JTVAG2K/#2IVDQ3AFZLGL6C3WJBHQXSHUOSQKTUP2
> - NumPy:
> - https://github.com/numpy/numpy/issues/15128
> - https://github.com/numpy/numpy/pull/26666
> - https://github.com/numpy/numpy/pull/31659
> - Array API:
> - https://github.com/data-apis/array-api/issues/629
> - https://github.com/data-apis/array-api/pull/722
> _______________________________________________
> NumPy-Discussion mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3//lists/numpy-discussion.python.org
> Member address: [email protected]
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/numpy-discussion.python.org
Member address: [email protected]