summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornjsmith <njs@pobox.com>2012-12-05 05:21:07 -0800
committernjsmith <njs@pobox.com>2012-12-05 05:21:07 -0800
commit3f5862193a6085a4d59f4dde8d006670a24cae04 (patch)
tree8fca3d00161c50dac04acceee91c70931ef6888a
parent023e559eda106cc48f82408a5958c7444d3769b2 (diff)
parent46e3b4cc9d9101ceaafffc6bb8faafee1bdc5fc9 (diff)
downloadnumpy-maintenance/1.6.x.tar.gz
Merge pull request #2787 from bfroehle/unique_argsortmaintenance/1.6.x
np.unique: TypeError: requested sort not available for type
-rw-r--r--numpy/core/tests/test_regression.py21
-rw-r--r--numpy/lib/arraysetops.py9
2 files changed, 15 insertions, 15 deletions
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index 7952cbb37..efc055b84 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -1611,19 +1611,22 @@ class TestRegression(TestCase):
a[...] = [[1,2]]
assert_equal(a, [[1,2], [1,2]])
- def test_unique_stable(self):
- # Ticket #2063 must always choose stable sort for argsort to
- # get consistent results
- v=np.array([0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2]*4)
- w=np.array([0,0,0,0,0,1,1,1,1,1,1,2,2,2,2])
- resv = np.unique(v,return_index=True)
- resw = np.unique(w,return_index=True)
- assert_equal(resv, resw)
-
def test_search_sorted_invalid_arguments(self):
# Ticket #2021, should not segfault.
x = np.arange(0, 4, dtype='datetime64[D]')
assert_raises(TypeError, x.searchsorted, 1)
+ def test_unique_special_comparators(self):
+ # gh-2785
+ # np.unique should not raise an exception for dtypes with
+ # special comparators.
+
+ # Extract the unique rows of the matrix
+ A = np.array([[1, 2], [1, 3], [1, 2]], dtype='i')
+ B, I, J = np.unique(A.view([('', A.dtype)]*A.shape[1]), True, True)
+ B = B.view(A.dtype).reshape((-1, A.shape[1]))
+ assert_equal(A, B[J])
+ assert_equal(A[I], B)
+
if __name__ == "__main__":
run_module_suite()
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py
index 47e94bc4d..721039238 100644
--- a/numpy/lib/arraysetops.py
+++ b/numpy/lib/arraysetops.py
@@ -112,8 +112,8 @@ def unique(ar, return_index=False, return_inverse=False):
unique : ndarray
The sorted unique values.
unique_indices : ndarray, optional
- The indices of the first occurrences of the unique values in the
- (flattened) original array. Only provided if `return_index` is True.
+ The indices of the unique values in the (flattened) original array.
+ Only provided if `return_index` is True.
unique_inverse : ndarray, optional
The indices to reconstruct the (flattened) original array from the
unique array. Only provided if `return_inverse` is True.
@@ -174,10 +174,7 @@ def unique(ar, return_index=False, return_inverse=False):
return ar
if return_inverse or return_index:
- if return_index:
- perm = ar.argsort(kind='mergesort')
- else:
- perm = ar.argsort()
+ perm = ar.argsort()
aux = ar[perm]
flag = np.concatenate(([True], aux[1:] != aux[:-1]))
if return_inverse: