Skip to content

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

Closed
@semnooij

Description

@semnooij
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions