Skip to content

BUG: Pandas Styler cell_ids Arg #35586

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
attack68 opened this issue Aug 6, 2020 · 0 comments · Fixed by #35588
Closed

BUG: Pandas Styler cell_ids Arg #35586

attack68 opened this issue Aug 6, 2020 · 0 comments · Fixed by #35588
Labels
Bug IO HTML read_html, to_html, Styler.apply, Styler.applymap
Milestone

Comments

@attack68
Copy link
Contributor

attack68 commented Aug 6, 2020

Pandas Styler has the following argument:

cell_ids: bool, default True
If True, each cell will have an id attribute in their HTML tag. The id takes the form T__row<num_row>_col<num_col> where is the unique identifier, <num_row> is the row number and <num_col> is the column number.

This doesn't seem to work.

Consider the example in the docs:

import pandas as pd  # NOTE VERSION 1.1.0
from pandas.io.formats.style import Styler
def highlight_max(s):
    x = s == s.max()
    x = x.replace(False, '')
    x = x.replace(True, 'background-color: yellow; color: brown')
    return x
df = pd.DataFrame(data=[[0,1], [1,0]])
s = Styler(df, uuid='_', cell_ids=False)
s.apply(highlight_max)
s.render()

This will still render id css tags for all cells even though it should have been ignored on some.

Reason and Solution

Upon render, within the code a ctx defaultdict object contains the properties for each cell:

ctx = defaultdict(<class 'list'>, {(1,0): ['background-color: yellow', 'color: brown'], (0,1): ['background-color: yellow', 'color: brown']})

On line 393 we have the condition:

if self.cell_ids or not (len(ctx[r, c]) == 1 and ctx[r, c][0] == ""):
    row_dict["id"] = "_".join(cs[1:])

This fails because the _update_ctx function handles empty string by not adding to the ctx - previously I suspect it performed differently..

We should really change line 393 to:

if self.cell_ids or (r,c) in ctx:

I added a PR for this..

@attack68 attack68 changed the title Pandas Styler Bugs BUG: Pandas Styler cell_ids Arg Aug 6, 2020
@jreback jreback added Bug IO HTML read_html, to_html, Styler.apply, Styler.applymap labels Aug 6, 2020
@jreback jreback added this to the 1.1.1 milestone Aug 6, 2020
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants