Skip to content

Read from multiple <tbody> within a <table> #20891

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

Merged
merged 2 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ Other Enhancements
- :meth:`DataFrame.to_sql` now performs a multivalue insert if the underlying connection supports itk rather than inserting row by row.
``SQLAlchemy`` dialects supporting multivalue inserts include: ``mysql``, ``postgresql``, ``sqlite`` and any dialect with ``supports_multivalues_insert``. (:issue:`14315`, :issue:`8953`)
- :func:`read_html` now accepts a ``displayed_only`` keyword argument to controls whether or not hidden elements are parsed (``True`` by default) (:issue:`20027`)
- :func:`read_html` now reads all ``<tbody>`` elements in a ``<table>``, not just the first. (:issue:`20690`)
- :meth:`~pandas.core.window.Rolling.quantile` and :meth:`~pandas.core.window.Expanding.quantile` now accept the ``interpolation`` keyword, ``linear`` by default (:issue:`20497`)
- zip compression is supported via ``compression=zip`` in :func:`DataFrame.to_pickle`, :func:`Series.to_pickle`, :func:`DataFrame.to_csv`, :func:`Series.to_csv`, :func:`DataFrame.to_json`, :func:`Series.to_json`. (:issue:`17778`)
- :class:`pandas.tseries.api.offsets.WeekOfMonth` constructor now supports ``n=0`` (:issue:`20517`).
Expand Down
22 changes: 13 additions & 9 deletions pandas/io/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def _parse_thead(self, table):
raise com.AbstractMethodError(self)

def _parse_tbody(self, table):
"""Return the body of the table.
"""Return the list of tbody elements from the parsed table element.

Parameters
----------
Expand All @@ -333,8 +333,8 @@ def _parse_tbody(self, table):

Returns
-------
tbody : node-like
A <tbody>...</tbody> element.
tbodys : list of node-like
A list of <tbody>...</tbody> elements
"""
raise com.AbstractMethodError(self)

Expand Down Expand Up @@ -388,13 +388,17 @@ def _parse_raw_tfoot(self, table):
np.array(res).squeeze()) if res and len(res) == 1 else res

def _parse_raw_tbody(self, table):
tbody = self._parse_tbody(table)
tbodies = self._parse_tbody(table)

try:
res = self._parse_tr(tbody[0])
except IndexError:
res = self._parse_tr(table)
return self._parse_raw_data(res)
raw_data = []

if tbodies:
for tbody in tbodies:
raw_data.extend(self._parse_tr(tbody))
else:
raw_data.extend(self._parse_tr(table))

return self._parse_raw_data(raw_data)

def _handle_hidden_tables(self, tbl_list, attr_name):
"""Returns list of tables, potentially removing hidden elements
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,33 @@ def test_empty_tables(self):
res2 = self.read_html(StringIO(data2))
assert_framelist_equal(res1, res2)

def test_multiple_tbody(self):
# GH-20690
# Read all tbody tags within a single table.
data = '''<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
<tbody>
<tr>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>'''
expected = DataFrame({'A': [1, 3], 'B': [2, 4]})
result = self.read_html(StringIO(data))[0]
tm.assert_frame_equal(result, expected)

def test_header_and_one_column(self):
"""
Don't fail with bs4 when there is a header and only one column
Expand Down