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

ENH: Styler display of NaN / null Values #21527

Closed
psychemedia opened this issue Jun 18, 2018 · 2 comments · Fixed by #29118
Closed

ENH: Styler display of NaN / null Values #21527

psychemedia opened this issue Jun 18, 2018 · 2 comments · Fixed by #29118
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string Visualization plotting

Comments

@psychemedia
Copy link
Contributor

The Styler allows for null cells to be highlighted using .style.highlight_null() but a nan text string will also be displayed.

If a column is coloured using .background_gradient() it must be a numeric type otherwise numerical comparisons fail. This means that using a construction of the form ..fillna('') on the column will create an error as the column is cast to an object type.

nan values can be removed in the rendered table using a hack of the form .render().replace('nan','') but it would be cleaner to use something along the lines of .style.replace_null(value) that could be used to tidy up the display of nan cells.

@WillAyd WillAyd added Visualization plotting Needs Info Clarification about behavior needed to assess issue labels Jun 18, 2018
@WillAyd
Copy link
Member

WillAyd commented Jun 18, 2018

Please be sure to checkout out the contributing guide on how to open bug reports:

https://pandas.pydata.org/pandas-docs/stable/contributing.html#bug-reports-and-enhancement-requests

This is missing version information and a minimally reproducible example. You can find more information on how to construct one of those here:

http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

It's difficult to infer from what you have here alone exactly what the problem is and what you are looking for, so try going through the above to see if we can get to something more explicit

@psychemedia
Copy link
Contributor Author

In the following example (pandas 0.23.0), a columns is rendered with a gradient styler, and nan cells are displayed with nan text dispayed explicity.

import pandas as pd
import numpy as np

df=pd.DataFrame({'x1':list(np.random.randint(-10,10,size=10))+[None] })

df.style.background_gradient()

It would be useful to be able to display such cells as empty cells. If the NA values are filled with an empty string, the column is cast to an object type and the background gradient is not displayed:

df.fillna('').style.background_gradient()

In a Jupyter notebook, a hacky workaround is to render the styled table and then replace nan strings with an empty string:

from IPython.core.display import HTML 
HTML(df.style.background_gradient().render().replace('nan',''))

It would be neater to be able to say something like:

df.style.replace_null('')

#simpler - use empty string as default and call:
df.style.replace_null()

The empty cell can then be highlighted as such using .highlight_null():

HTML(df.style.background_gradient().highlight_null().render().replace('nan',''))

However, it might be more appropriate if a fill colour for null cells could be passed along with a replacement string: df.style.replace_null(color='lightgrey')

@TomAugspurger TomAugspurger added Output-Formatting __repr__ of pandas objects, to_string IO HTML read_html, to_html, Styler.apply, Styler.applymap and removed Needs Info Clarification about behavior needed to assess issue labels Jun 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string Visualization plotting
Projects
None yet
3 participants