8000 DOC: Add clarifications np.argpartition by JuliaPoo · Pull Request #26716 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DOC: Add clarifications np.argpartition #26716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
DOC+TST: Specify np.nan sort order in partition functions
  • Loading branch information
JuliaPoo committed Jun 28, 2024
commit 2398d9cbc4210b20666c2469d1b1cc94bcbad2d3
4 changes: 3 additions & 1 deletion numpy/_core/fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,8 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
the real parts except when they are equal, in which case the order
is determined by the imaginary parts.

The sort order of ``np.nan`` is bigger than ``np.inf``.

Examples
--------
>>> a = np.array([7, 1, 7, 7, 1, 5, 7, 2, 3, 2, 6, 2, 3, 0])
Expand Down Expand Up @@ -923,7 +925,7 @@ def argpartition(a, kth, axis=-1, kind='introselect', order=None):
is unstable, and hence the returned indices are not guaranteed
to be the earliest/latest occurrence of the element.

The treatment of ``np.nan`` in the input array is undefined.
The sort order of ``np.nan`` is bigger than ``np.inf``.

See `partition` for notes on the different selection algorithms.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment. Aren't we in the partition function docstring? Does this docstring show up in a different function as well?

Copy link
Contributor Author
@JuliaPoo JuliaPoo Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sort order is defined in the docstring for np.sort, but since np.partition uses a different implementation, I thought it would be helpful to also define the sort order of np.partition.

I've removed notes regarding the sort order from np.argpartition and instead linked it to the docs for np.partition in the latest commit, this follows what np.sort and np.argsort did.


Expand Down
10 changes: 10 additions & 0 deletions numpy/_core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -10211,6 +10211,16 @@ def test_partition_fp(N, dtype):
assert_arr_partitioned(np.sort(arr)[k], k,
arr[np.argpartition(arr, k, kind='introselect')])

# Check that `np.inf < np.nan`
# This follows np.sort
arr[0] = np.nan
arr[1] = np.inf
o1 = np.partition(arr, -2, kind='introselect')
o2 = arr[np.argpartition(arr, -2, kind='introselect')]
for out in [o1,o2]:
assert_(np.isnan(out[-1]))
assert_equal(out[-2], np.inf)

def test_cannot_assign_data():
a = np.arange(10)
b = np.linspace(0, 1, 10)
Expand Down
Loading
0