Skip to content

BUG: Styler.apply and applymap duplicate CSS on re-render. #39395

Closed
@attack68

Description

@attack68

Code Sample, a copy-pastable example

df = pd.DataFrame((np.random.rand(1, 1)*100))
s = df.style\
    .applymap(lambda x: 'background-color: black;')\
    .apply(lambda x: pd.DataFrame([['color: red;']]), axis=None)
print(s.render())
print(s.ctx)
print(s.render())
print(s.ctx)

...

<style  type="text/css" >
#T_fda50_row0_col0{
            background-color:  black;
            color:  red;
        }</style>

defaultdict(<class 'list'>, {(0, 0): ['background-color: black', 'color: red']})

<style  type="text/css" >
#T_fda50_row0_col0{
            background-color:  black;
            color:  red;
            background-color:  black;
            color:  red;
        }</style>

defaultdict(<class 'list'>, {(0, 0): ['background-color: black', 'color: red', 'background-color: black', 'color: red']})

Problem description

The private _todo list is not cleared after initial execution so repeats the execution on second render.
There does not seem to be any reason any reason to maintain the _todo list after it has been done and the ctx object has been updated with necessary css-styles.

Expected Output

Same on each render.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions