BUG: fix to_latex() when using MultiIndex with NaN in (#14249)
State: closed

- [x] closes #14249
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

This attempt to close #14249 which actually reports two related issues (one in the issue one in the SO link) with to_latex() with MultiIndex when the MultiIndex contains a NaN. The output is now more consistent with what you'd get from to_csv() or to_html().

Labels:
name: Bug
name: IO LaTeX
description: to_latex

Comments:
@tomneep are you testing both issues in the issue?
cc @toobaz if any comments
@jreback yes, I only added one test but parametrized it to cover both of the issues
> cc @toobaz if any comments

Sorry for the delay. I think the patch works as intended, but I also think that replacing ```lev2 = lev.format()``` and all the new ``blank`` definition with

```python
lev2 = self.frame.index.get_level_values(i).format()
```

would be cleaner.
Hi @toobaz, 

Thanks for checking the patch, I wanted to keep the changes as minimal as possible as it is my first contribution. 
I agree that this isn't so clean so I had a go at refactoring that section of code- see
d63e88fa92617a6ba9121297e8a3a8447d3dfe7e. 

The tests all pass so if you prefer this approach (I certainly do) I can make a PR for that commit instead (maybe after a bit of tidying).

Hi @jreback @toobaz 
Since I see that https://github.com/pandas-dev/pandas/pull/20032 is also addressing the same area of code to this, I'd like to get some clarification to see if we can move this forward. 

The rewrite I mention in the comment above closes the issue this PR initially aimed to close as well as #19981, and an (as far as I can tell unreported) issue that `to_latex(sparsify=False)` is still sparsifying regardless in the master. My impression is that this would be a good place for #20032 to start making changes but I would like you opinion. 

The main thing I don't know, is whether I should create a new PR since the changes are now more substantial than this initial one, or if I can just keep using this one and commit the changes to this branch?
can you rebase. we moved some files around.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19910?src=pr&el=h1) Report
> Merging [#19910](https://codecov.io/gh/pandas-dev/pandas/pull/19910?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/cad6dc7afb9e3a2571cba9112632a963cc63ade6?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/19910/graphs/tree.svg?token=eZ4WkYLtcO&width=650&src=pr&height=150)](https://codecov.io/gh/pandas-dev/pandas/pull/19910?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #19910      +/-   ##
==========================================
+ Coverage   91.77%   91.78%   +<.01%     
==========================================
  Files         152      152              
  Lines       49185    49186       +1     
==========================================
+ Hits        45140    45143       +3     
+ Misses       4045     4043       -2
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.16% <100%> (ø)` | :arrow_up: |
| #single | `41.85% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/19910?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/formats/latex.py](https://codecov.io/gh/pandas-dev/pandas/pull/19910/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvbGF0ZXgucHk=) | `100% <100%> (ø)` | :arrow_up: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/19910/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `83.95% <0%> (+0.2%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19910?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19910?src=pr&el=footer). Last update [cad6dc7...2a72b60](https://codecov.io/gh/pandas-dev/pandas/pull/19910?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Ok, rebased. I also rebased my alternative branch that tidies this portion of the code up: c0daa15d06ba9d2c63b34a1148cc65d433c6027a

My preference would be to merge that branch instead (in which case a couple more tests could be added) but let me know which you prefer.

@tomneep again sorry for the delay. I also think the alternative branch is much better: please feel free to open another PR.

(Please also clarify whether that branch is incompatible with/alternative to #20032, or just requires some merge work)
I've opened #20797 for the improved branch. 

Pehaps it will mean some of #20032 won't be needed but I don't think this is completely incompatible.