BUG: NaN in multi-index and to_latex() · Issue #14249 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

I was trying to find a small reproducible code for a larger issue that I wanted to report. On the way there, I stumbled across this (probably related) issue:

# test data frame:
>>> test
Out[36]: 
  robustness  value  change
0   baseline    NaN   -14.5
# as expected: 
>>> test.set_index(['robustness', 'value']).to_csv()
Out[34]: 'robustness,value,change\nbaseline,,-14.5\n'

And here's what to_latex() makes out of it:

>>> test.set_index(['robustness', 'value']).to_latex()
Traceback (most recent call last):
  File "/usr/local/anaconda2/envs/myenv2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-35-901bbd119ed6>", line 1, in <module>
    test.set_index(['robustness', 'value']).to_latex()
  File "/usr/local/anaconda2/envs/myenv2/lib/python2.7/site-packages/pandas/core/frame.py", line 1601, in to_latex
    encoding=encoding)
  File "/usr/local/anaconda2/envs/myenv2/lib/python2.7/site-packages/pandas/core/format.py", line 638, in to_latex
    latex_renderer.write_result(self.buf)
  File "/usr/local/anaconda2/envs/myenv2/lib/python2.7/site-packages/pandas/core/format.py", line 824, in write_result
    blank = ' ' * len(lev2[0])
IndexError: list index out of range

Expected Output

output of pd.show_versions()

pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.11.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-36-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.0
nose: None
pip: 8.1.1
setuptools: 21.2.1
Cython: None
numpy: 1.11.0
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.1.2
sphinx: None
patsy: 0.4.1
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: None
numexpr: 2.5.2
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None