BUG: styler builtin highlighters and pd.NA interaction · Issue #45804 · pandas-dev/pandas (original) (raw)

Pandas version checks

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

Screen Shot 2022-02-03 at 19 27 25

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

Screen Shot 2022-02-03 at 19 31 25

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

.