Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
GentilsTo opened this issue Nov 20, 2020 · 2 comments · Fixed by #38247
Closed
2 of 3 tasks

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

GentilsTo opened this issue Nov 20, 2020 · 2 comments · Fixed by #38247
Labels
Milestone

Comments

@GentilsTo
Copy link

  • 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

@GentilsTo GentilsTo added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 20, 2020
@jreback
Copy link
Contributor

jreback commented Dec 3, 2020

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.

and how do you figure this out?

@GentilsTo
Copy link
Author

GentilsTo commented Dec 3, 2020

hum what do you mean? When I say "I understand" I should have say "I assumed". I'have no idea how it really works. I just found out that the style that was working in jupyter when using the color name give me the warning I provide above when exporting to excel, while using the associated Hex code works just fine.
If you are asking for a solution, sorry but i have no clue how to implement it. For now, as mentioned, I'm just using the dictionary from matplotlib (as cdict), and instead of setting color = 'Blue', I used color = cdict['Blue']

@jreback jreback added Visualization plotting and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 4, 2020
@jreback jreback added this to the 1.2 milestone Dec 4, 2020
@jorisvandenbossche jorisvandenbossche modified the milestones: 1.2, 1.3 Dec 8, 2020
@jreback jreback modified the milestones: 1.3, 1.2 Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants