Skip to content

BUG: using a list in "selector" for set_table_styles produces wrong rules #34061

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
levitsky opened this issue May 8, 2020 · 2 comments · Fixed by #39942
Closed
2 of 3 tasks

BUG: using a list in "selector" for set_table_styles produces wrong rules #34061

levitsky opened this issue May 8, 2020 · 2 comments · Fixed by #39942
Labels
Bug IO HTML read_html, to_html, Styler.apply, Styler.applymap Needs Info Clarification about behavior needed to assess issue Styler conditional formatting using DataFrame.style
Milestone

Comments

@levitsky
Copy link

levitsky commented May 8, 2020

  • 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.


Problem description

Example: I want to apply a style like this:

df.style.set_table_styles([{'selector': 'th.col_heading,th.blank', 'props': [('display', 'none')]}])

Note a list (th.col_heading,th.blank) as selector. This produces wrong CSS, because UUID of the table is only prepended once to this selector:

#T_table_UUID th.col_heading, th.blank {
      display: none;
}

i.e. all th.blank elements in all tables on the page are hidden. This is not exactly intuitive, although the workaround is simple: you need to write

set_table_styles([{'selector': 'th.col_heading', 'props': [('display', 'none')]}, {'selector': 'th.blank', 'props': [('display', 'none')]}])

INSTALLED VERSIONS

commit : None
python : 3.8.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.6.10-arch1-1
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.4
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : 3.0.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.14.0
pandas_datareader: None
bs4 : 4.9.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.16
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None

@levitsky levitsky added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 8, 2020
@TomAugspurger
Copy link
Contributor

@levitsky Thanks for the report. can you provide a minimal exmaple? http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

@TomAugspurger TomAugspurger added IO HTML read_html, to_html, Styler.apply, Styler.applymap Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 4, 2020
@levitsky
Copy link
Author

levitsky commented Sep 4, 2020

It's enough to create an empty DataFrame and apply the style above, then render the resulting HTML:

import pandas as pd
df = pd.DataFrame()
df.style.set_table_styles([{'selector': 'th.col_heading,th.blank', 'props': [('display', 'none')]}]).render()

The resulting HTML is something like this:

<style  type="text/css" >
    #T_cd059770_eed6_11ea_b183_645a04675490 th.col_heading,th.blank {
          display: none;
    }</style><table id="T_cd059770_eed6_11ea_b183_645a04675490" ><thead></thead><tbody>
        </tbody></table>

As you can see, the rule #T_cd059770_eed6_11ea_b183_645a04675490 th.col_heading,th.blank will lead to unexpected results in case the page containing this code also has other tables, because the th.blank part will apply universally rather than just the current DataFrame.

I think this can be addressed by parsing the selector passed to set_table_styles (to produce the correct rule, or at least throw a warning/exception) or maybe at least documenting this limitation.

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

Successfully merging a pull request may close this issue.

4 participants