<p dir="auto">Getting a <code class="notranslate">TypeError</code> while using <code class="notranslate">np.unique</code> with array of <code class="notranslate">dtype=object</code></p>
<h3 dir="auto">Reproducing code example:</h3>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np

a = np.array([1, 2, 3, 2, 'a', 'b', 'a'], dtype=object)
np.unique(a)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">2</span>, <span class="pl-s">'a'</span>, <span class="pl-s">'b'</span>, <span class="pl-s">'a'</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">object</span>)
<span class="pl-s1">np</span>.<span class="pl-en">unique</span>(<span class="pl-s1">a</span>)</pre></div>
<h3 dir="auto">Error message:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="----&gt; 1 np.unique(a)

&lt;__array_function__ internals&gt; in unique(*args, **kwargs)

~/miniconda3/lib/python3.7/site-packages/numpy/lib/arraysetops.py in unique(ar, return_index, return_inverse, return_counts, axis)
    260     ar = np.asanyarray(ar)
    261     if axis is None:
--&gt; 262         ret = _unique1d(ar, return_index, return_inverse, return_counts)
    263         return _unpack_tuple(ret)
    264 

~/miniconda3/lib/python3.7/site-packages/numpy/lib/arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
    308         aux = ar[perm]
    309     else:
--&gt; 310         ar.sort()
    311         aux = ar
    312     mask = np.empty(aux.shape, dtype=np.bool_)

TypeError: '&lt;' not supported between instances of 'str' and 'int'"><pre class="notranslate"><code class="notranslate">----&gt; 1 np.unique(a)

&lt;__array_function__ internals&gt; in unique(*args, **kwargs)

~/miniconda3/lib/python3.7/site-packages/numpy/lib/arraysetops.py in unique(ar, return_index, return_inverse, return_counts, axis)
    260     ar = np.asanyarray(ar)
    261     if axis is None:
--&gt; 262         ret = _unique1d(ar, return_index, return_inverse, return_counts)
    263         return _unpack_tuple(ret)
    264 

~/miniconda3/lib/python3.7/site-packages/numpy/lib/arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
    308         aux = ar[perm]
    309     else:
--&gt; 310         ar.sort()
    311         aux = ar
    312     mask = np.empty(aux.shape, dtype=np.bool_)

TypeError: '&lt;' not supported between instances of 'str' and 'int'
</code></pre></div>
<h3 dir="auto">Numpy/Python version information:</h3>
<p dir="auto">1.18.0 3.7.5 (default, Oct 25 2019, 10:52:18)<br>
[Clang 4.0.1 (tags/RELEASE_401/final)]</p>
<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/numpy/ticket/2188" rel="nofollow">http://projects.scipy.org/numpy/ticket/2188</a> on 2012-07-23 by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/yarikoptic/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/yarikoptic">@yarikoptic</a>, assigned to unknown.</em></p>
<p dir="auto">tested against current master (present in 1.6.2 as well):</p>
<p dir="auto">If with python2.x series it works ok, without puking:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$&gt; python2.7 -c 'import numpy as np; print repr(repr(np.unique(np.array([1,2, None, &quot;str&quot;]))))' 
'array([None, 1, 2, str], dtype=object)'"><pre class="notranslate"><code class="notranslate">$&gt; python2.7 -c 'import numpy as np; print repr(repr(np.unique(np.array([1,2, None, "str"]))))' 
'array([None, 1, 2, str], dtype=object)'
</code></pre></div>
<p dir="auto">NB I will report a bug on <strong>repr</strong> here separately if not yet filed</p>
<p dir="auto">it fails with python3.x altogether:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$&gt; python3.2 -c 'import numpy as np; print(repr(repr(np.unique(np.array([1,2,None, &quot;str&quot;])))))'
Traceback (most recent call last):
  File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/local/lib/python3.2/dist-packages/numpy/lib/arraysetops.py&quot;, line 194, in unique
    ar.sort()
TypeError: unorderable types: int() &gt; NoneType()"><pre class="notranslate"><code class="notranslate">$&gt; python3.2 -c 'import numpy as np; print(repr(repr(np.unique(np.array([1,2,None, "str"])))))'
Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
  File "/usr/local/lib/python3.2/dist-packages/numpy/lib/arraysetops.py", line 194, in unique
    ar.sort()
TypeError: unorderable types: int() &gt; NoneType()
</code></pre></div>
<p dir="auto">whenever IMHO it must operate correctly -- semantic of unique() action should not imply ability to sort the elements</p>