Skip to content

Avoid empty lines with spaces to be transformed to empty string #59155

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
wants to merge 9 commits into from
Closed
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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ I/O
- Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)
- Bug in :meth:`read_csv` raising ``TypeError`` when ``nrows`` and ``iterator`` are specified without specifying a ``chunksize``. (:issue:`59079`)
- Bug in :meth:`read_excel` raising ``ValueError`` when passing array of boolean values when ``dtype="boolean"``. (:issue:`58159`)
- Bug in :meth:`read_html` would return an incorrect result when parsing a table with a space character in a ``<td>`` tag. (:issue:`12345`)
- Bug in :meth:`read_json` not validating the ``typ`` argument to not be exactly ``"frame"`` or ``"series"`` (:issue:`59124`)
- Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`)

Expand Down
6 changes: 4 additions & 2 deletions pandas/io/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ def _expand_colspan_rowspan(

# Append the text from this <td>, colspan times
text = _remove_whitespace(self._text_getter(td))
if len(text) == 0:
text = self._text_getter(td)

if self.extract_links in ("all", section):
href = self._href_getter(td)
text = (text, href)
Expand Down Expand Up @@ -1201,9 +1204,7 @@ def read_html(

validate_header_arg(header)
check_dtype_backend(dtype_backend)

io = stringify_path(io)

return _parse(
flavor=flavor,
io=io,
Expand All @@ -1223,4 +1224,5 @@ def read_html(
extract_links=extract_links,
dtype_backend=dtype_backend,
storage_options=storage_options,
skip_blank_lines=False,
)
42 changes: 42 additions & 0 deletions pandas/tests/io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,48 @@ def test_preserve_empty_rows(self, flavor_read_html):

tm.assert_frame_equal(result, expected)

def test_preserve_rows_with_spaces(self, flavor_read_html):
result = flavor_read_html(
StringIO(
"""
<table>
<tr>
<th>A</th>
<th>B</th>
</tr>
<tr>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
"""
)
)[0]
expected = DataFrame(data=[["a", "b"], [" ", " "]], columns=["A", "B"])

tm.assert_frame_equal(result, expected)

def test_preserve_table_with_just_spaces(self, flavor_read_html):
result = flavor_read_html(
StringIO(
"""
<table>
<tr>
<td> </td>
</tr>
</table>
"""
)
)[0]

expected = DataFrame(data=[" "])
assert len(result) != 0
tm.assert_frame_equal(result, expected)

def test_ignore_empty_rows_when_inferring_header(self, flavor_read_html):
result = flavor_read_html(
StringIO(
Expand Down
Loading