Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

API: Always allow sorted=False and make a note about it #28503

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 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 18 additions & 10 deletions 28 numpy/lib/_arraysetops_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ def unique(ar, return_index=False, return_inverse=False,
.. versionadded:: 1.24

sorted : bool, optional
If True, the unique elements are sorted.
If True, the unique elements are sorted. Elements may be sorted in
practice even if ``sorted=False``, but this could change without
notice.

.. versionadded:: 2.3

Expand Down Expand Up @@ -361,12 +363,6 @@ def _unique1d(ar, return_index=False, return_inverse=False,

optional_indices = return_index or return_inverse

if (optional_indices or return_counts) and not sorted:
raise ValueError(
"Currently, `sorted` can only be False if `return_index`, "
"`return_inverse`, and `return_counts` are all False."
)

# masked arrays are not supported yet.
if not optional_indices and not return_counts and not np.ma.is_masked(ar):
# First we convert the array to a numpy array, later we wrap it back
Expand Down Expand Up @@ -448,10 +444,14 @@ def unique_all(x):
This function is an Array API compatible alternative to::

np.unique(x, return_index=True, return_inverse=True,
return_counts=True, equal_nan=False)
return_counts=True, equal_nan=False, sorted=False)

but returns a namedtuple for easier access to each output.

.. note::
This function currently always returns a sorted result, however,
this could change in any NumPy minor release.

Parameters
----------
x : array_like
Expand Down Expand Up @@ -507,10 +507,14 @@ def unique_counts(x):

This function is an Array API compatible alternative to::

np.unique(x, return_counts=True, equal_nan=False)
np.unique(x, return_counts=True, equal_nan=False, sorted=False)

but returns a namedtuple for easier access to each output.

.. note::
This function currently always returns a sorted result, however,
this could change in any NumPy minor release.

Parameters
----------
x : array_like
Expand Down Expand Up @@ -559,10 +563,14 @@ def unique_inverse(x):

This function is an Array API compatible alternative to::

np.unique(x, return_inverse=True, equal_nan=False)
np.unique(x, return_inverse=True, equal_nan=False, sorted=False)

but returns a namedtuple for easier access to each output.

.. note::
This function currently always returns a sorted result, however,
this could change in any NumPy minor release.

Parameters
----------
x : array_like
Expand Down
17 changes: 11 additions & 6 deletions 17 numpy/lib/tests/test_arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,14 +838,19 @@ class Subclass(np.ndarray):

@pytest.mark.parametrize("arg", ["return_index", "return_inverse", "return_counts"])
def test_unsupported_hash_based(self, arg):
"""Test that hash based unique is not supported when either of
return_index, return_inverse, or return_counts is True.
"""These currently never use the hash-based solution. However,
it seems easier to just allow it.

This is WIP and the above will gradually be supported in the future.
When the hash-based solution is added, this test should fail and be
replaced with something more comprehensive.
"""
msg = "Currently, `sorted` can only be False"
with pytest.raises(ValueError, match=msg):
np.unique([1, 1], sorted=False, **{arg: True})
a = np.array([1, 5, 2, 3, 4, 8, 199, 1, 3, 5])

res_not_sorted = np.unique([1, 1], sorted=False, **{arg: True})
res_sorted = np.unique([1, 1], sorted=True, **{arg: True})
# The following should fail without first sorting `res_not_sorted`.
for arr, expected in zip(res_not_sorted, res_sorted):
assert_array_equal(arr, expected)

def test_unique_axis_errors(self):
assert_raises(TypeError, self._run_axis_tests, object)
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.