BUG: styler
builtin highlighters and pd.NA
interaction · Issue #45804 · pandas-dev/pandas (original) (raw)
Pandas version checks
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of pandas.
- I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
Styler has some defaults for display of ints and float. When including missing data this becomes a bit unintuitive:
When using None
or np.nan
the int
column is cast to float
and precision takes over:
df = DataFrame([[1, 2], [3, None]]) df.style.highlight_max().highlight_null()
This can be solved here by using pd.NA
but this has the problem of casting the int
col to object
which has other implications, and dataframes probably don't want to be created like this in future:
df = DataFrame([[1, 2], [3, pd.NA]]) df.style.highlight_max().highlight_null()
If instead we use the extension dtypes the column dtypes are consistently int
but then the styling functions fail:
df = DataFrame([[1, 2], [3, pd.NA]], dtype="Int64") df.style.highlight_max().highlight_null()
<TypeError: boolean value of NA is ambiguous>
Expected Behavior
It is reasonably simple to code a check for pd.NA
in booleans and reallow the styling behaviour.
Might this be an appropriate solution or will pandas change the way pd.NA
is interpreted in future?
Installed Versions
.