Skip to content

BUG: Multiple CSS selectors using set_table_styles and dict form of table_styles #44011

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
3 tasks done
AndrewLister-STFC opened this issue Oct 13, 2021 · 3 comments · Fixed by #44023
Closed
3 tasks done
Labels
Bug good first issue IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style

Comments

@AndrewLister-STFC
Copy link

AndrewLister-STFC commented Oct 13, 2021

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame([range(5) for _ in range(5)], columns=list('abcde'))
styled = df.style.set_table_styles({
    'b': [{'selector': 'th, td', 'props': [('border-left', '2px solid black')]}]
})
# styled = df.style.set_table_styles({
#     'b': [{'selector': 'th', 'props': [('border-left', '2px solid black')]},
#           {'selector': 'td', 'props': [('border-left', '2px solid black')]}]
# })
html = styled.render()
with open('css_style_bug.html', 'w') as f:
    f.write(html)

Issue Description

This is a minor issue when styling a table for html display. When setting a table style for a specified column using set_table_styles, the column is only applied to the final selector tag if they are named in a comma seperated list.

This seems to be similar to issue #34061.

Expected Behavior

I have commented out, in the above examples, a workaround which does the expected behaviour.

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.72-microsoft-standard-WSL2
Version : #1 SMP Wed Oct 28 23:40:43 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 20.0.2
setuptools : 44.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@AndrewLister-STFC AndrewLister-STFC added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 13, 2021
@attack68
Copy link
Contributor

attack68 commented Oct 13, 2021

Yes this seems to be true in master also.

If anyone wants to do a fix this is quite easy:

  1. import _format_table_styles from styler_render in style.py (actually change from private '_' method to a regular non-underscored methods)
  2. change line 2052 from for s in styles to for s in _format_table_styles(styles)
  3. Add relavent bug fix whats new and add a relevant test, probably in the same place as the fix for BUG: using a list in "selector" for set_table_styles produces wrong rules #34061.

@attack68 attack68 added IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 13, 2021
@attack68 attack68 added this to the Contributions Welcome milestone Oct 13, 2021
@mabittar
Copy link
Contributor

mabittar commented Oct 13, 2021

hello can I help with this one??

I did the changes needs in steps 1 and 2, but need some advice with step 3.
Where to insert the bug_fix information and where would be the most suitable place to insert a new style test? I put it next to the other tests regarding borders and colors, near to line 818 in test_style
and how i run tests.io? ->already find: make test-scripts

@attack68
Copy link
Contributor

@mabittar I just realised the test you did for this PR didn't actually test the bug: which was that this failed when using the dict input form, and the test doesn't use the dict input form.

Any chance you want to tidy it up? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug good first issue IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants