Skip to content

BUG: Different colors for NA in df.style.background_gradient() #47695

Open
@janosh

Description

@janosh

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

import itertools

import pandas as pd


df_rand = pd.DataFrame(index=range(5), columns=range(5), dtype=float)

for idx1, idx2 in itertools.combinations_with_replacement(df_rand.index, 2):
    df_rand.loc[idx1, idx2] = np.random.randint(0, 100)

df_rand.style.format("{:.0f}", na_rep="").background_gradient()

Issue Description

Code above outputs HTML for a styled df with 1st column showing different colors (white) for nan values than the others (black).

Screen Shot 2022-07-13 at 08 07 40

Expected Behavior

I'd like all nan cells to be black.

Installed Versions

INSTALLED VERSIONS

commit : e8093ba
python : 3.10.5.final.0
python-bits : 64
OS : Darwin
OS-release : 21.5.0
Version : Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.4.3
numpy : 1.23.1
pytz : 2021.3
dateutil : 2.8.2
setuptools : 59.5.0
pip : 22.1
Cython : 0.29.26
pytest : 7.1.2
hypothesis : 6.42.0
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.30.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
snappy : None
sqlalchemy : 1.4.31
tables : None
tabulate : 0.8.9
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions