summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/tests/test_regression.py8
-rw-r--r--numpy/lib/arraysetops.py9
2 files changed, 14 insertions, 3 deletions
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index fc530987e..090498a63 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -1673,6 +1673,14 @@ class TestRegression(TestCase):
a = np.array([u'abcd'])
assert_equal(a.dtype.itemsize, 16)
+ 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)
if __name__ == "__main__":
run_module_suite()
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py
index c4441d8a5..91dd96f9c 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 unique values in the (flattened) original array.
- Only provided if `return_index` is True.
+ The indices of the first occurrences 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,7 +174,10 @@ def unique(ar, return_index=False, return_inverse=False):
return ar
if return_inverse or return_index:
- perm = ar.argsort()
+ if return_index:
+ perm = ar.argsort(kind='mergesort')
+ else:
+ perm = ar.argsort()
aux = ar[perm]
flag = np.concatenate(([True], aux[1:] != aux[:-1]))
if return_inverse: