Skip to content

BUG: read_html parses nested table incorrectly when using html5lib or bs4 flavors #64524

@fumoboy007

Description

@fumoboy007

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
from io import StringIO

result = pd.read_html(
   StringIO("""
       <table>
           <thead>
               <tr>
                   <th></th>
               </tr>
           </thead>
           <tbody>
               <tr>
                   <td>
                       <table id="descendant">
                           <thead>
                               <tr>
                                   <th>A</th>
                                   <th>B</th>
                               </tr>
                           </thead>
                           <tbody>
                               <tr>
                                   <td>1</td>
                                   <td>2</td>
                               </tr>
                           </tbody>
                       </table>
                   </td>
               </tr>
           </tbody>
       </table>
   """),
   flavor="bs4",
   attrs={
       "id": "descendant"
   }
)[0]

print(result)

Issue Description

A web page can have a nested table. For example, the outer table could be used for layout while the inner table contains actual data. (Granted, using tables for layout is discouraged but developers may still choose to do so.)

The example above prints the following:

   A  B
0  A  B
1  1  2

Notice how the header row is incorrectly added as a regular row.

Expected Behavior

The example above should print the following:

   A  B
0  1  2

Installed Versions

Details

INSTALLED VERSIONS

commit : 2d4966b
python : 3.11.15
python-bits : 64
OS : Darwin
OS-release : 24.6.0
Version : Darwin Kernel Version 24.6.0: Wed Nov 5 21:30:23 PST 2025; root:xnu-11417.140.69.705.2~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 3.1.0.dev0+278.g2d4966b114
numpy : 2.4.2
dateutil : 2.9.0.post0
pip : 26.0.1
Cython : 3.2.4
sphinx : 9.0.4
IPython : 9.10.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.14.3
bottleneck : 1.6.0
fastparquet : None
fsspec : 2026.2.0
html5lib : 1.1
hypothesis : 6.151.9
gcsfs : 2026.2.0
jinja2 : 3.1.6
lxml.etree : 6.0.2
matplotlib : 3.10.8
numba : 0.64.0
numexpr : 2.14.1
odfpy : None
openpyxl : 3.1.5
psycopg2 : 2.9.11
pymysql : 1.4.6
pyarrow : 23.0.1
pyiceberg : 0.11.1
pyreadstat : 1.3.3
pytest : 9.0.2
python-calamine : None
pytz : 2025.2
pyxlsb : 1.0.10
s3fs : 2026.2.0
scipy : 1.17.1
sqlalchemy : 2.0.48
tables : 3.11.1
tabulate : 0.10.0
xarray : None
xlrd : 2.0.2
xlsxwriter : 3.2.9
zstandard : 0.25.0
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO HTMLread_html, to_html, Styler.apply, Styler.applymap

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions