Skip to content

BUG: Limited available color name list when using style and to_excel #37967

Closed
@GentilsTo

Description

@GentilsTo
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import pandas as pd

df = pd.DataFrame( {'A': [1, 2, 3],
                   'B': [4, 5, 6],})

# /1 -- It Fails ---
df_out = (df.style.
          highlight_max(color='orangered').
          highlight_min(color='dodgerblue')
         )
df_out.to_excel('pandas_to_excel_color_issue.xlsx')

# /2 -- It works ----
df_out = (df.style.
          highlight_max(color='#FF4500'). # orangered
          highlight_min(color='#1E90FF') # dodgerblue)
         )
df_out.to_excel('pandas_to_excel_color_issue.xlsx')

Problem description

When using style, the to_excel function is not able to handle some common color name that otherwise works with an interactive python console.

See with code block /1 returns :

c:\program files\python38\lib\site-packages\pandas\io\formats\excel.py:331: CSSWarning: Unhandled color format: 'dodgerblue'
  warnings.warn(f"Unhandled color format: {repr(val)}", CSSWarning)
c:\program files\python38\lib\site-packages\pandas\io\formats\excel.py:331: CSSWarning: Unhandled color format: 'orangered'
  warnings.warn(f"Unhandled color format: {repr(val)}", CSSWarning)

And in the Excel file :
image

While the /2 block is fine:
image

Expected Output

I understand that the to_excel function only accepts a minimal list of color names (based on the export engine I presume).
It would hence be great if pandas could manage the color Hex conversion under the hood for the user, and hence allow him to use a more large color name list (the one from matplotlib for example with matplotlib.colors.cnames), preventing the usage of unreadable color code.
In addition, I don't know if the current Excel output in case of CSS warning is really appropriate (the 'black render'), maybe simply ignoring the formatting would be better? (one could argue that it's more noticeable in the Excel file like that, that's right).

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 67a3d42
python : 3.8.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en
LOCALE : fr_FR.cp1252

pandas : 1.1.4
numpy : 1.19.3
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 50.3.2
Cython : None
pytest : 6.1.2
hypothesis : None
sphinx : 3.2.0
blosc : None
feather : None
xlsxwriter : 1.3.2
lxml.etree : 4.5.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 1.0.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : 0.16.0
xlrd : 1.2.0
xlwt : 1.3.0
numba : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions