<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np
table = pd.DataFrame(data=np.random.randn(5,2), index=[4,1,'foo',3,'bar'])
table = table.sort_index()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<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">table</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randn</span>(<span class="pl-c1">5</span>,<span class="pl-c1">2</span>), <span class="pl-s1">index</span><span class="pl-c1">=</span>[<span class="pl-c1">4</span>,<span class="pl-c1">1</span>,<span class="pl-s">'foo'</span>,<span class="pl-c1">3</span>,<span class="pl-s">'bar'</span>])
<span class="pl-s1">table</span> <span class="pl-c1">=</span> <span class="pl-s1">table</span>.<span class="pl-en">sort_index</span>()</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">The code above works in python2. However, in python3 a <code class="notranslate">TypeError</code> is raised:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TypeError: '&lt;' not supported between instances of 'str' and 'int'"><pre class="notranslate"><code class="notranslate">TypeError: '&lt;' not supported between instances of 'str' and 'int'
</code></pre></div>
<p dir="auto">The reason for this is described <a href="https://stackoverflow.com/questions/50097704" rel="nofollow">here</a>: mixed-type sequencing cannot be sorted anymore just like this.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">No exception when calling <code class="notranslate">sort_index</code> in python3.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None</p>
<p dir="auto">pandas: 0.22.0<br>
pytest: None<br>
pip: 10.0.0<br>
setuptools: 39.0.1<br>
Cython: None<br>
numpy: 1.14.2<br>
scipy: 1.0.1<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.3.1<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.7.2<br>
pytz: 2018.4<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
feather: None<br>
matplotlib: 2.2.2<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: None<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [1]: Index([0, '1']).sort_values()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-1-c2949e5a9d7f&gt; in &lt;module&gt;()
----&gt; 1 Index([0, '1']).sort_values()

/Users/jreback/pandas/pandas/core/indexes/base.py in sort_values(self, return_indexer, ascending)
   2026         Return sorted copy of Index
   2027         &quot;&quot;&quot;
-&gt; 2028         _as = self.argsort()
   2029         if not ascending:
   2030             _as = _as[::-1]

/Users/jreback/pandas/pandas/core/indexes/base.py in argsort(self, *args, **kwargs)
   2089         if result is None:
   2090             result = np.array(self)
-&gt; 2091         return result.argsort(*args, **kwargs)
   2092 
   2093     def __add__(self, other):

TypeError: '&gt;' not supported between instances of 'str' and 'int'"><pre class="notranslate"><code class="notranslate">In [1]: Index([0, '1']).sort_values()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-1-c2949e5a9d7f&gt; in &lt;module&gt;()
----&gt; 1 Index([0, '1']).sort_values()

/Users/jreback/pandas/pandas/core/indexes/base.py in sort_values(self, return_indexer, ascending)
   2026         Return sorted copy of Index
   2027         """
-&gt; 2028         _as = self.argsort()
   2029         if not ascending:
   2030             _as = _as[::-1]

/Users/jreback/pandas/pandas/core/indexes/base.py in argsort(self, *args, **kwargs)
   2089         if result is None:
   2090             result = np.array(self)
-&gt; 2091         return result.argsort(*args, **kwargs)
   2092 
   2093     def __add__(self, other):

TypeError: '&gt;' not supported between instances of 'str' and 'int'
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [7]: Series([0, '1']).sort_values()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/jreback/pandas/pandas/core/series.py in _try_kind_sort(arr)
   1762                 # if kind==mergesort, it can fail for object dtype
-&gt; 1763                 return arr.argsort(kind=kind)
   1764             except TypeError:

TypeError: '&gt;' not supported between instances of 'str' and 'int'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
&lt;ipython-input-7-b0c6ec3dcb93&gt; in &lt;module&gt;()
----&gt; 1 Series([0, '1']).sort_values()

/Users/jreback/pandas/pandas/core/series.py in sort_values(self, axis, ascending, inplace, kind, na_position)
   1775         idx = _default_index(len(self))
   1776 
-&gt; 1777         argsorted = _try_kind_sort(arr[good])
   1778 
   1779         if is_list_like(ascending):

/Users/jreback/pandas/pandas/core/series.py in _try_kind_sort(arr)
   1765                 # stable sort not available for object dtype
   1766                 # uses the argsort default quicksort
-&gt; 1767                 return arr.argsort(kind='quicksort')
   1768 
   1769         arr = self._values

TypeError: '&gt;' not supported between instances of 'str' and 'int'"><pre class="notranslate"><code class="notranslate">In [7]: Series([0, '1']).sort_values()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/jreback/pandas/pandas/core/series.py in _try_kind_sort(arr)
   1762                 # if kind==mergesort, it can fail for object dtype
-&gt; 1763                 return arr.argsort(kind=kind)
   1764             except TypeError:

TypeError: '&gt;' not supported between instances of 'str' and 'int'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
&lt;ipython-input-7-b0c6ec3dcb93&gt; in &lt;module&gt;()
----&gt; 1 Series([0, '1']).sort_values()

/Users/jreback/pandas/pandas/core/series.py in sort_values(self, axis, ascending, inplace, kind, na_position)
   1775         idx = _default_index(len(self))
   1776 
-&gt; 1777         argsorted = _try_kind_sort(arr[good])
   1778 
   1779         if is_list_like(ascending):

/Users/jreback/pandas/pandas/core/series.py in _try_kind_sort(arr)
   1765                 # stable sort not available for object dtype
   1766                 # uses the argsort default quicksort
-&gt; 1767                 return arr.argsort(kind='quicksort')
   1768 
   1769         arr = self._values

TypeError: '&gt;' not supported between instances of 'str' and 'int'
</code></pre></div>
<p dir="auto">These can both be fixed by falling back to using <code class="notranslate">.get_indexer()</code> if these fail (<code class="notranslate">.argsort</code> is faster and handles duplicates for object dtypes, so the default is good).</p>