Skip to content

BUG: styler builtin highlighters and pd.NA interaction #45804

@attack68

Description

@attack68

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

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

.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions