Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from

Conversation

tomneep
Copy link
Contributor

@tomneep tomneep commented Feb 26, 2018

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().

@jreback jreback added Bug IO LaTeX to_latex labels Feb 27, 2018
@jreback
Copy link
Contributor

jreback commented Feb 27, 2018

@tomneep are you testing both issues in the issue?

@jreback
Copy link
Contributor

jreback commented Feb 27, 2018

cc @toobaz if any comments

@tomneep
Copy link
Contributor Author

tomneep commented Feb 27, 2018

@jreback yes, I only added one test but parametrized it to cover both of the issues

@toobaz
Copy link
Member

toobaz commented Mar 4, 2018

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

lev2 = self.frame.index.get_level_values(i).format()

would be cleaner.

@@ -864,6 +864,7 @@ I/O
- Bug in :func:`read_csv` where missing values were not being handled properly when ``keep_default_na=False`` with dictionary ``na_values`` (:issue:`19227`)
- Bug in :func:`read_sas` where a file with 0 variables gave an ``AttributeError`` incorrectly. Now it gives an ``EmptyDataError`` (:issue:`18184`)
- Bug in :func:`DataFrame.to_latex()` where pairs of braces meant to serve as invisible placeholders were escaped (:issue:`18667`)
- Bug in :func:`DataFrame.to_latex()` where a ``NaN`` in a ``MultiIndex`` would cause an ``IndexError`` or incorrect output (:issue:`14249`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where an all-NaN level in a MultiIndex

(I think)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the SO link in #14249 there is a case where NaNs are being silently replaced with other non-NaN values in the level and this fixes both that issue and the one actually reported

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right!

@tomneep
Copy link
Contributor Author

tomneep commented Mar 6, 2018

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
d63e88f.

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).

@tomneep tomneep mentioned this pull request Mar 8, 2018
4 tasks
@tomneep
Copy link
Contributor Author

tomneep commented Mar 8, 2018

Hi @jreback @toobaz
Since I see that #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?

@jreback
Copy link
Contributor

jreback commented Mar 14, 2018

can you rebase. we moved some files around.

@codecov
Copy link

codecov bot commented Mar 14, 2018

Codecov Report

Merging #19910 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            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%> (ø) ⬆️
#single 41.85% <0%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/io/formats/latex.py 100% <100%> (ø) ⬆️
pandas/util/testing.py 83.95% <0%> (+0.2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cad6dc7...2a72b60. Read the comment docs.

@tomneep
Copy link
Contributor Author

tomneep commented Mar 14, 2018

Ok, rebased. I also rebased my alternative branch that tidies this portion of the code up: c0daa15

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.

@toobaz
Copy link
Member

toobaz commented Apr 18, 2018

@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)

@tomneep
Copy link
Contributor Author

tomneep commented Apr 23, 2018

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.

@tomneep tomneep closed this Apr 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: NaN in multi-index and to_latex()
3 participants