<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<p dir="auto">Execute this code in a terminal with width &lt;= 90 cols.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
val = [1017.56, 88.0502014, 114.90493799999999, 98.52555079999999, -4.35584164,
 0.13409058699999998, -3.28222513, 0.137214884, -3.23924017, 0.08030553160000001,
 25.4253311, 4.71156263, 4.71156263, 4.71148443, 0.725787282, 0.861985624,
 0.488319486, 0.221756965, 0.491590142, 0.174637929, 0.14585168699999998,
 0.8847839829999999, pd.Timestamp('2018-05-08 20:07:23.560000')]
col_id = pd.MultiIndex(levels=[['A', 'B', 'C', 'D', 'E', 'F'],
	['0', '1', '2', '3', '4', '5', '6', '7', 'a', 'act', 'axl', 'b', 'c', 'top', ''], 
        ['a', 'b', 'c', '']],
        labels=[[4, 0, 0, 0, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 5],
	[-1, 8, 11, 12, 10, 13, 10, 13, 10, 13, -1, 9, 9, 9, 0, 1, 2, 3, 4, 5, 6, 7, 14],
	[-1, -1, -1, -1, 0, 0, 1, 1, 2, 2, -1, 0, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, 3]])
row_id = pd.MultiIndex.from_tuples(
        [('rotation', '0', pd.Timedelta('0 days 01:20:01.807984'))])
df = pd.DataFrame(data=val).transpose()
df.index = row_id
df.columns = col_id
print(df)
"><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-s1">val</span> <span class="pl-c1">=</span> [<span class="pl-c1">1017.56</span>, <span class="pl-c1">88.0502014</span>, <span class="pl-c1">114.90493799999999</span>, <span class="pl-c1">98.52555079999999</span>, <span class="pl-c1">-</span><span class="pl-c1">4.35584164</span>,
 <span class="pl-c1">0.13409058699999998</span>, <span class="pl-c1">-</span><span class="pl-c1">3.28222513</span>, <span class="pl-c1">0.137214884</span>, <span class="pl-c1">-</span><span class="pl-c1">3.23924017</span>, <span class="pl-c1">0.08030553160000001</span>,
 <span class="pl-c1">25.4253311</span>, <span class="pl-c1">4.71156263</span>, <span class="pl-c1">4.71156263</span>, <span class="pl-c1">4.71148443</span>, <span class="pl-c1">0.725787282</span>, <span class="pl-c1">0.861985624</span>,
 <span class="pl-c1">0.488319486</span>, <span class="pl-c1">0.221756965</span>, <span class="pl-c1">0.491590142</span>, <span class="pl-c1">0.174637929</span>, <span class="pl-c1">0.14585168699999998</span>,
 <span class="pl-c1">0.8847839829999999</span>, <span class="pl-s1">pd</span>.<span class="pl-v">Timestamp</span>(<span class="pl-s">'2018-05-08 20:07:23.560000'</span>)]
<span class="pl-s1">col_id</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">MultiIndex</span>(<span class="pl-s1">levels</span><span class="pl-c1">=</span>[[<span class="pl-s">'A'</span>, <span class="pl-s">'B'</span>, <span class="pl-s">'C'</span>, <span class="pl-s">'D'</span>, <span class="pl-s">'E'</span>, <span class="pl-s">'F'</span>],
	[<span class="pl-s">'0'</span>, <span class="pl-s">'1'</span>, <span class="pl-s">'2'</span>, <span class="pl-s">'3'</span>, <span class="pl-s">'4'</span>, <span class="pl-s">'5'</span>, <span class="pl-s">'6'</span>, <span class="pl-s">'7'</span>, <span class="pl-s">'a'</span>, <span class="pl-s">'act'</span>, <span class="pl-s">'axl'</span>, <span class="pl-s">'b'</span>, <span class="pl-s">'c'</span>, <span class="pl-s">'top'</span>, <span class="pl-s">''</span>], 
        [<span class="pl-s">'a'</span>, <span class="pl-s">'b'</span>, <span class="pl-s">'c'</span>, <span class="pl-s">''</span>]],
        <span class="pl-s1">labels</span><span class="pl-c1">=</span>[[<span class="pl-c1">4</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">3</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">5</span>],
	[<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">8</span>, <span class="pl-c1">11</span>, <span class="pl-c1">12</span>, <span class="pl-c1">10</span>, <span class="pl-c1">13</span>, <span class="pl-c1">10</span>, <span class="pl-c1">13</span>, <span class="pl-c1">10</span>, <span class="pl-c1">13</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">9</span>, <span class="pl-c1">9</span>, <span class="pl-c1">9</span>, <span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">4</span>, <span class="pl-c1">5</span>, <span class="pl-c1">6</span>, <span class="pl-c1">7</span>, <span class="pl-c1">14</span>],
	[<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">3</span>]])
<span class="pl-s1">row_id</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">MultiIndex</span>.<span class="pl-en">from_tuples</span>(
        [(<span class="pl-s">'rotation'</span>, <span class="pl-s">'0'</span>, <span class="pl-s1">pd</span>.<span class="pl-v">Timedelta</span>(<span class="pl-s">'0 days 01:20:01.807984'</span>))])
<span class="pl-s1">df</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">val</span>).<span class="pl-en">transpose</span>()
<span class="pl-s1">df</span>.<span class="pl-s1">index</span> <span class="pl-c1">=</span> <span class="pl-s1">row_id</span>
<span class="pl-s1">df</span>.<span class="pl-s1">columns</span> <span class="pl-c1">=</span> <span class="pl-s1">col_id</span>
<span class="pl-en">print</span>(<span class="pl-s1">df</span>)</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">When trying to print a dataframe, if the values contained in the indexes and/or in the first/last column are too long and the terminal too narrow, an IndexError is raised:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/local/lib/python3.6/dist-packages/pandas/core/base.py&quot;, line 61, in __str__
    return self.__unicode__()
  File &quot;/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py&quot;, line 663, in __unicode__
    line_width=width, show_dimensions=show_dimensions)
  File &quot;/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py&quot;, line 1968, in to_string
    formatter.to_string()
  File &quot;/usr/local/lib/python3.6/dist-packages/pandas/io/formats/format.py&quot;, line 648, in to_string
    strcols = self._to_str_columns()
  File &quot;/usr/local/lib/python3.6/dist-packages/pandas/io/formats/format.py&quot;, line 539, in _to_str_columns
    str_columns = self._get_formatted_column_labels(frame)
  File &quot;/usr/local/lib/python3.6/dist-packages/pandas/io/formats/format.py&quot;, line 782, in _get_formatted_column_labels
    str_columns = _sparsify(str_columns)
  File &quot;/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/multi.py&quot;, line 2962, in _sparsify
    prev = pivoted[start]
IndexError: list index out of range"><pre class="notranslate"><span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python3.6/dist-packages/pandas/core/base.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">61</span>, <span class="pl-s1">in</span> <span class="pl-s1">__str__</span>
    <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-en">__unicode__</span>()
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">663</span>, <span class="pl-s1">in</span> <span class="pl-s1">__unicode__</span>
    <span class="pl-s1">line_width</span><span class="pl-c1">=</span><span class="pl-s1">width</span>, <span class="pl-s1">show_dimensions</span><span class="pl-c1">=</span><span class="pl-s1">show_dimensions</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1968</span>, <span class="pl-s1">in</span> <span class="pl-s1">to_string</span>
    <span class="pl-s1">formatter</span>.<span class="pl-en">to_string</span>()
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python3.6/dist-packages/pandas/io/formats/format.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">648</span>, <span class="pl-s1">in</span> <span class="pl-s1">to_string</span>
    <span class="pl-s1">strcols</span> <span class="pl-c1">=</span> <span class="pl-s1">self</span>.<span class="pl-en">_to_str_columns</span>()
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python3.6/dist-packages/pandas/io/formats/format.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">539</span>, <span class="pl-s1">in</span> <span class="pl-s1">_to_str_columns</span>
    <span class="pl-s1">str_columns</span> <span class="pl-c1">=</span> <span class="pl-s1">self</span>.<span class="pl-en">_get_formatted_column_labels</span>(<span class="pl-s1">frame</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python3.6/dist-packages/pandas/io/formats/format.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">782</span>, <span class="pl-s1">in</span> <span class="pl-s1">_get_formatted_column_labels</span>
    <span class="pl-s1">str_columns</span> <span class="pl-c1">=</span> <span class="pl-en">_sparsify</span>(<span class="pl-s1">str_columns</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/multi.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">2962</span>, <span class="pl-s1">in</span> <span class="pl-s1">_sparsify</span>
    <span class="pl-s1">prev</span> <span class="pl-c1">=</span> <span class="pl-s1">pivoted</span>[<span class="pl-s1">start</span>]
<span class="pl-v">IndexError</span>: <span class="pl-s1">list</span> <span class="pl-s1">index</span> <span class="pl-s1">out</span> <span class="pl-s1">of</span> <span class="pl-s1">range</span></pre></div>
<p dir="auto">This happens because <code class="notranslate">pandas.io.formats.format.DataFrameFormatter._chk_truncate()</code> gives an empty frame. Subsequently, an empty <code class="notranslate">str_columns</code> is passed to <code class="notranslate">pandas.core.indexes.multi._sparsify(*args)</code> which raises the <code class="notranslate">IndexError</code> when trying to access the passed variable.</p>
<p dir="auto">This problem seems to arise only when the dataframe and its index contains <code class="notranslate">datetime</code>-like values (see example with long strings in next section).</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">The raised exception is misleading, since the user could think his/her dataframe has something wrong, whereas only the size of the terminal is too narrow. A proper slicing of the string version of datetime formats in order to fit the terminal size would be the expected output.</p>
<p dir="auto">With strings, this works better than with datetime. The following code does not raise any Exception:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
n = 10
rng = pd.date_range('22/12/2018', periods=n, freq='H')
id_1 = [&quot;i am a really long string&quot;]*n
[&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit&quot;+str(i) for i in range(n)]
ids = pd.MultiIndex.from_tuples(list(zip(id_1,id_2,rng)))
df = pd.DataFrame([rng,rng]).transpose()
df.index = ids
print(df)
"><pre class="notranslate"><span class="pl-s1">n</span> <span class="pl-c1">=</span> <span class="pl-c1">10</span>
<span class="pl-s1">rng</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">date_range</span>(<span class="pl-s">'22/12/2018'</span>, <span class="pl-s1">periods</span><span class="pl-c1">=</span><span class="pl-s1">n</span>, <span class="pl-s1">freq</span><span class="pl-c1">=</span><span class="pl-s">'H'</span>)
<span class="pl-s1">id_1</span> <span class="pl-c1">=</span> [<span class="pl-s">"i am a really long string"</span>]<span class="pl-c1">*</span><span class="pl-s1">n</span>
[<span class="pl-s">"Lorem ipsum dolor sit amet, consectetur adipiscing elit"</span><span class="pl-c1">+</span><span class="pl-en">str</span>(<span class="pl-s1">i</span>) <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-s1">n</span>)]
<span class="pl-s1">ids</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">MultiIndex</span>.<span class="pl-en">from_tuples</span>(<span class="pl-en">list</span>(<span class="pl-en">zip</span>(<span class="pl-s1">id_1</span>,<span class="pl-s1">id_2</span>,<span class="pl-s1">rng</span>)))
<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>([<span class="pl-s1">rng</span>,<span class="pl-s1">rng</span>]).<span class="pl-en">transpose</span>()
<span class="pl-s1">df</span>.<span class="pl-s1">index</span> <span class="pl-c1">=</span> <span class="pl-s1">ids</span>
<span class="pl-en">print</span>(<span class="pl-s1">df</span>)</pre></div>
<p dir="auto">Otherwise, a warning with an explanation should be raised if the frame that has to be printed gets truncated to an empty one.</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<br>
python: 3.6.5.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 4.15.0-22-generic<br>
machine: x86_64<br>
processor: x86_64<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: en_US.UTF-8<br>
LOCALE: en_US.UTF-8</p>
<p dir="auto">pandas: 0.23.0<br>
pytest: None<br>
pip: 9.0.1<br>
setuptools: 39.2.0<br>
Cython: 0.28.3<br>
numpy: 1.14.3<br>
scipy: 1.1.0<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.4.0<br>
sphinx: 1.6.7<br>
patsy: None<br>
dateutil: 2.7.3<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: 0.999999999<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.10<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<h4 dir="auto">Code example</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

index = pd.date_range('1970', '2018', freq='A')
data = np.random.randn(len(index))
columns1 = [
    ['This is a long title with &gt; 37 chars.'],
    ['cat'],
]
columns2 = [
    ['This is a loooooonger title with &gt; 43 chars.'],
    ['dog'],
]
df1 = pd.DataFrame(data=data, index=index, columns=columns1)
df2 = pd.DataFrame(data=data, index=index, columns=columns2)
df = pd.concat([df1, df2], axis=1)
df.head()"><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">index</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">date_range</span>(<span class="pl-s">'1970'</span>, <span class="pl-s">'2018'</span>, <span class="pl-s1">freq</span><span class="pl-c1">=</span><span class="pl-s">'A'</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-en">len</span>(<span class="pl-s1">index</span>))
<span class="pl-s1">columns1</span> <span class="pl-c1">=</span> [
    [<span class="pl-s">'This is a long title with &gt; 37 chars.'</span>],
    [<span class="pl-s">'cat'</span>],
]
<span class="pl-s1">columns2</span> <span class="pl-c1">=</span> [
    [<span class="pl-s">'This is a loooooonger title with &gt; 43 chars.'</span>],
    [<span class="pl-s">'dog'</span>],
]
<span class="pl-s1">df1</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">data</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s1">index</span>, <span class="pl-s1">columns</span><span class="pl-c1">=</span><span class="pl-s1">columns1</span>)
<span class="pl-s1">df2</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">data</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s1">index</span>, <span class="pl-s1">columns</span><span class="pl-c1">=</span><span class="pl-s1">columns2</span>)
<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">concat</span>([<span class="pl-s1">df1</span>, <span class="pl-s1">df2</span>], <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)
<span class="pl-s1">df</span>.<span class="pl-en">head</span>()</pre></div>
<h4 dir="auto">Output (using pandas 0.23.0)</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; df.head()
Traceback (most recent call last):
  File &quot;&lt;console&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/base.py&quot;, line 82, in __repr__
    return str(self)
  File &quot;/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/base.py&quot;, line 61, in __str__
    return self.__unicode__()
  File &quot;/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/frame.py&quot;, line 663, in __unicode__
    line_width=width, show_dimensions=show_dimensions)
  File &quot;/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/frame.py&quot;, line 1968, in to_string
    formatter.to_string()
  File &quot;/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/io/formats/format.py&quot;, line 648, in to_string
    strcols = self._to_str_columns()
  File &quot;/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/io/formats/format.py&quot;, line 539, in _to_str_columns
    str_columns = self._get_formatted_column_labels(frame)
  File &quot;/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/io/formats/format.py&quot;, line 782, in _get_formatted_column_labels
    str_columns = _sparsify(str_columns)
  File &quot;/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/indexes/multi.py&quot;, line 2962, in _sparsify
    prev = pivoted[start]
IndexError: list index out of range"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; df.head()
Traceback (most recent call last):
  File "&lt;console&gt;", line 1, in &lt;module&gt;
  File "/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/base.py", line 82, in __repr__
    return str(self)
  File "/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/base.py", line 61, in __str__
    return self.__unicode__()
  File "/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/frame.py", line 663, in __unicode__
    line_width=width, show_dimensions=show_dimensions)
  File "/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/frame.py", line 1968, in to_string
    formatter.to_string()
  File "/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/io/formats/format.py", line 648, in to_string
    strcols = self._to_str_columns()
  File "/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/io/formats/format.py", line 539, in _to_str_columns
    str_columns = self._get_formatted_column_labels(frame)
  File "/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/io/formats/format.py", line 782, in _get_formatted_column_labels
    str_columns = _sparsify(str_columns)
  File "/home/david/.virtualenvs/thegrid-py3-venv/lib/python3.5/site-packages/pandas/core/indexes/multi.py", line 2962, in _sparsify
    prev = pivoted[start]
IndexError: list index out of range
</code></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">After upgrading Pandas 0.22.0 to 0.23.0 I have experienced the above error. I have noticed that it is the length of the column values, <code class="notranslate">This is a long title with &gt; 37 chars.</code> and <code class="notranslate">This is a loooooonger title with &gt; 43 chars.</code>, that makes the difference. If I tweak the combined length of these to be &lt;= 80 characters, there is no error, and output is as expected.</p>
<h4 dir="auto">Expected Output (using pandas 0.22.0)</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; df.head()
           This is a long title with &gt; 37 chars.  \
                                             cat   
1970-12-31                             -1.448415   
1971-12-31                              0.081324   
1972-12-31                             -0.018105   
1973-12-31                              0.902790   
1974-12-31                              0.668474   

           This is a loooooonger title with &gt; 43 chars.  
                                                    dog  
1970-12-31                                    -1.448415  
1971-12-31                                     0.081324  
1972-12-31                                    -0.018105  
1973-12-31                                     0.902790  
1974-12-31                                     0.668474"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; df.head()
           This is a long title with &gt; 37 chars.  \
                                             cat   
1970-12-31                             -1.448415   
1971-12-31                              0.081324   
1972-12-31                             -0.018105   
1973-12-31                              0.902790   
1974-12-31                              0.668474   

           This is a loooooonger title with &gt; 43 chars.  
                                                    dog  
1970-12-31                                    -1.448415  
1971-12-31                                     0.081324  
1972-12-31                                    -0.018105  
1973-12-31                                     0.902790  
1974-12-31                                     0.668474
</code></pre></div>
<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<br>
python: 3.5.2.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 4.4.0-124-generic<br>
machine: x86_64<br>
processor: x86_64<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: en_ZA.UTF-8<br>
LOCALE: en_ZA.UTF-8</p>
<p dir="auto">pandas: 0.23.0<br>
pytest: None<br>
pip: 10.0.1<br>
setuptools: 32.3.1<br>
Cython: None<br>
numpy: 1.14.0<br>
scipy: None<br>
pyarrow: None<br>
xarray: None<br>
IPython: None<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.6.1<br>
pytz: 2018.3<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
feather: None<br>
matplotlib: None<br>
openpyxl: 2.5.3<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: 1.0.4<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: 2.7.4 (dt dec pq3 ext lo64)<br>
jinja2: None<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>