BUG: fix styler cell_ids arg so that blank style is ignored on False by attack68 · Pull Request #35588 · pandas-dev/pandas (original) (raw)
Can you add a test case for the problem you are trying to solve? As reviewers this is typically the first thing we look for
Do you mean in the issue or a test in code?
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()
yields...
('<style type="text/css" >\n'
'#T__row0_col1,#T__row1_col0{\n'
' background-color: yellow;\n'
' color: brown;\n'
' }</style><table id="T__" ><thead> <tr> <th class="blank '
'level0" ></th> <th class="col_heading level0 col0" >0</th> <th '
'class="col_heading level0 col1" >1</th> </tr></thead><tbody>\n'
' <tr>\n'
' <th id="T__level0_row0" class="row_heading level0 '
'row0" >0</th>\n'
' <td id="T__row0_col0" class="data row0 col0" '
'>0</td>\n'
' <td id="T__row0_col1" class="data row0 col1" '
'>1</td>\n'
' </tr>\n'
' <tr>\n'
' <th id="T__level0_row1" class="row_heading level0 '
'row1" >1</th>\n'
' <td id="T__row1_col0" class="data row1 col0" '
'>1</td>\n'
' <td id="T__row1_col1" class="data row1 col1" '
'>0</td>\n'
' </tr>\n'
' </tbody></table>')
This is clearly erroneous since it has added the id tag to every data cell when (0,0) and (1,1) should be ignored.
Note that the correct output was produced using the same code on pandas version 1.0.4.
The bug fix in this PR yields the code:
('<style type="text/css" >\n'
'#T__row0_col1,#T__row1_col0{\n'
' background-color: yellow;\n'
' color: brown;\n'
' }</style><table id="T__" ><thead> <tr> <th class="blank '
'level0" ></th> <th class="col_heading level0 col0" >0</th> <th '
'class="col_heading level0 col1" >1</th> </tr></thead><tbody>\n'
' <tr>\n'
' <th id="T__level0_row0" class="row_heading level0 '
'row0" >0</th>\n'
' <td class="data row0 col0" >0</td>\n'
' <td id="T__row0_col1" class="data row0 col1" '
'>1</td>\n'
' </tr>\n'
' <tr>\n'
' <th id="T__level0_row1" class="row_heading level0 '
'row1" >1</th>\n'
' <td id="T__row1_col0" class="data row1 col0" '
'>1</td>\n'
' <td class="data row1 col1" >0</td>\n'
' </tr>\n'
' </tbody></table>')
which restores correct functionality since the id tags are removed where appropriate.