Skip to content

BUG: pandas dataframe style render produces incorrect html with multiindex #35706

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
semnooij opened this issue Aug 13, 2020 · 2 comments · Fixed by #36223
Closed
2 of 3 tasks

BUG: pandas dataframe style render produces incorrect html with multiindex #35706

semnooij opened this issue Aug 13, 2020 · 2 comments · Fixed by #36223
Labels
Bug Styler conditional formatting using DataFrame.style

Comments

@semnooij
Copy link

semnooij commented Aug 13, 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import pandas as pd
import datetime

df = pd.DataFrame(
    data={
        ('level0', 'level1a',): [1234565.23424],
        ('level0', 'level1b',): [1234565.23424],
        ('level0', 'level1c',): [1234565.23424],
        ('level0', 'level1d',): [1234565.23424],
        ('level0', 'level1e',): [1234565.23424],
    },
    columns=pd.MultiIndex.from_tuples(
        [
            ('level0', 'level1a',),
            ('level0', 'level1b',),
            ('level0', 'level1c',),
            ('level0', 'level1d',),
            ('level0', 'level1e',),
        ]
    ),
    index=['test']
)

df.style.format('{:,.2f}').render()

Problem description

The render() of the styler produces incorrect html in case the columns are a MultiIndex.

Expected Output

re.sub(r'colspan=(\d)', r'colspan="\1"', df.style.format('{:,.2f}').render())

The current implementation returns something like

while it should be

See also https://www.w3schools.com/tags/att_td_colspan.asp

Same goes for MultiIndex indices where it concerns rowspan instead of colspan.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff27
python : 3.6.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.14.111-1.el7.centos.x86_64
Version : #1 SMP Wed Apr 17 17:45:41 CEST 2019
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.0
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 9.0.1
setuptools : 28.8.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.5 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.14.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 1.0.0
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@semnooij semnooij added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 13, 2020
@semnooij semnooij changed the title BUG: BUG: pandas dataframe style render produces incorrect html with multiindex Aug 14, 2020
@jbrockmendel jbrockmendel added Styler conditional formatting using DataFrame.style and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 2, 2020
@attack68
Copy link
Contributor

attack68 commented Sep 8, 2020

I don't see the problem here:
On latest master the colspan attribute is added where colspan is greater than 1.
Isn't this what you think it should be doing?

df = pd.DataFrame(
    data={
        ('level0', 'level1a',): [1234565.23424],
        ('level0', 'level1b',): [1234565.23424],
    },
    columns=pd.MultiIndex.from_tuples(
        [
            ('level0', 'level1a',),
            ('level0', 'level1b',),
        ]
    ),
    index=['test']
)

s = Styler(df, uuid="_", cell_ids=False)
print(s.format('{:,.2f}').render())
<style  type="text/css" >
</style>
<table id="T__" >
  <thead>    
    <tr>        
       <th class="blank level0" ></th>        
       <th class="col_heading level0 col0" colspan=2>level0</th>    
     </tr>    
     <tr>       
         <th class="blank level1" ></th>        
         <th class="col_heading level1 col0" >level1a</th>        
         <th class="col_heading level1 col1" >level1b</th>    
     </tr>
</thead>
<tbody>
            <tr>
                        <th id="T__level0_row0" class="row_heading level0 row0" >test</th>
                        <td  class="data row0 col0" >1,234,565.23</td>
                        <td  class="data row0 col1" >1,234,565.23</td>
            </tr>
    </tbody>
</table>

@semnooij
Copy link
Author

semnooij commented Sep 8, 2020

Hi. Thanks for taking the time to look at this bug. The colspan is indeed added but should contain "" around the number as is detailed in the w3schools link. The colspan=2 should be colspan="2".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants