10000 Merge pull request #10608 from charris/backport-10588 · numpy/numpy@a35a040 · GitHub
[go: up one dir, main page]

Skip to content

Commit a35a040

Browse files
authored
Merge pull request #10608 from charris/backport-10588
BUG: Revert sort optimization in np.unique.
2 parents c5bdcd5 + c9d4e6e commit a35a040

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

numpy/lib/arraysetops.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,18 @@ def unique(ar, return_index=False, return_inverse=False,
135135
return_counts : bool, optional
136136
If True, also return the number of times each unique item appears
137137
in `ar`.
138+
138139
.. versionadded:: 1.9.0
139-
axis : int or None, optional
140-
The axis to operate on. If None, `ar` will be flattened beforehand.
141-
Otherwise, duplicate items will be removed along the provided axis,
142-
with all the other axes belonging to the each of the unique elements.
143-
Object arrays or structured arrays that contain objects are not
144-
supported if the `axis` kwarg is used.
145-
.. versionadded:: 1.13.0
146140
141+
axis : int or None, optional
142+
The axis to operate on. If None, `ar` will be flattened. If an integer,
143+
the subarrays indexed by the given axis will be flattened and treated
144+
as the elements of a 1-D array with the dimension of the given axis,
145+
see the notes for more details. Object arrays or structured arrays
146+
that contain objects are not supported if the `axis` kwarg is used. The
147+
default is None.
147148
149+
.. versionadded:: 1.13.0
148150
149151
Returns
150152
-------
@@ -166,6 +168,17 @@ def unique(ar, return_index=False, return_inverse=False,
166168
numpy.lib.arraysetops : Module with a number of other functions for
167169
performing set operations on arrays.
168170
171+
Notes
172+
-----
173+
When an axis is specified the subarrays indexed by the axis are sorted.
174+
This is done by making the specified axis the first dimension of the array
175+
and then flattening the subarrays in C order. The flattened subarrays are
176+
then viewed as a structured type with each element given a label, with the
177+
effect that we end up with a 1-D array of structured types that can be
178+
treated in the same way as any other 1-D array. The result is that the
179+
flattened subarrays are sorted in lexicographic order starting with the
180+
first element.
181+
169182
Examples
170183
--------
171184
>>> np.unique([1, 1, 2, 2, 3, 3])
@@ -217,14 +230,7 @@ def unique(ar, return_index=False, return_inverse=False,
217230
ar = ar.reshape(orig_shape[0], -1)
218231
ar = np.ascontiguousarray(ar)
219232

220-
if ar.dtype.char in (np.typecodes['AllInteger'] +
221-
np.typecodes['Datetime'] + 'S'):
222-
# Optimization: Creating a view of your data with a np.void data type of
223-
# size the number of bytes in a full row. Handles any type where items
224-
# have a unique binary representation, i.e. 0 is only 0, not +0 and -0.
225-
dtype = np.dtype((np.void, ar.dtype.itemsize * ar.shape[1]))
226-
else:
227-
dtype = [('f{i}'.format(i=i), ar.dtype) for i in range(ar.shape[1])]
233+
dtype = [('f{i}'.format(i=i), ar.dtype) for i in range(ar.shape[1])]
228234

229235
try:
230236
consolidated = ar.view(dtype)

numpy/lib/tests/test_arraysetops.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,15 @@ def test_unique_masked(self):
453453
assert_array_equal(v.data, v2.data, msg)
454454
assert_array_equal(v.mask, v2.mask, msg)
455455

456+
def test_unique_sort_order_with_axis(self):
457+
# These tests fail if sorting along axis is done by treating subarrays
458+
# as unsigned byte strings. See gh-10495.
459+
fmt = "sort order incorrect for integer type '%s'"
460+
for dt in 'bhilq':
461+
a = np.array([[-1],[0]], dt)
462+
b = np.unique(a, axis=0)
463+
assert_array_equal(a, b, fmt % dt)
464+
456465
def _run_axis_tests(self, dtype):
457466
data = np.array([[0, 1, 0, 0],
458467
[1, 0, 0, 0],

0 commit comments

Comments
 (0)
0