Skip to content

Stop raising in read_csv when header row contains only empty cells #44657

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 7 commits into from
Dec 19, 2021
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/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ Other API changes
- :meth:`Index.get_indexer_for` no longer accepts keyword arguments (other than 'target'); in the past these would be silently ignored if the index was not unique (:issue:`42310`)
- Change in the position of the ``min_rows`` argument in :meth:`DataFrame.to_string` due to change in the docstring (:issue:`44304`)
- Reduction operations for :class:`DataFrame` or :class:`Series` now raising a ``ValueError`` when ``None`` is passed for ``skipna`` (:issue:`44178`)
- :func:`read_csv` and :func:`read_html` no longer raising an error when one of the header rows consists only of ``Unnamed:`` columns (:issue:`13054`)
- Changed the ``name`` attribute of several holidays in
``USFederalHolidayCalendar`` to match `official federal holiday
names <https://www.opm.gov/policy-data-oversight/pay-leave/federal-holidays/>`_
Expand Down
11 changes: 0 additions & 11 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
from pandas.core.dtypes.cast import astype_nansafe
from pandas.core.dtypes.common import (
ensure_object,
ensure_str,
is_bool_dtype,
is_categorical_dtype,
is_dict_like,
Expand Down Expand Up @@ -395,16 +394,6 @@ def extract(r):
for single_ic in sorted(ic):
names.insert(single_ic, single_ic)

# If we find unnamed columns all in a single
# level, then our header was too long.
for n in range(len(columns[0])):
if all(ensure_str(col[n]) in self.unnamed_cols for col in columns):
header = ",".join([str(x) for x in self.header])
raise ParserError(
f"Passed header=[{header}] are too many rows "
"for this multi_index of columns"
)

# Clean the column names (if we have an index_col).
if len(ic):
col_names = [
Expand Down
27 changes: 11 additions & 16 deletions pandas/tests/io/parser/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,26 +557,21 @@ def test_multi_index_unnamed(all_parsers, index_col, columns):
else:
data = ",".join([""] + (columns or ["", ""])) + "\n,0,1\n0,2,3\n1,4,5\n"

result = parser.read_csv(StringIO(data), header=header, index_col=index_col)
exp_columns = []

if columns is None:
msg = (
r"Passed header=\[0,1\] are too "
r"many rows for this multi_index of columns"
)
with pytest.raises(ParserError, match=msg):
parser.read_csv(StringIO(data), header=header, index_col=index_col)
else:
result = parser.read_csv(StringIO(data), header=header, index_col=index_col)
exp_columns = []
columns = ["", "", ""]

for i, col in enumerate(columns):
if not col: # Unnamed.
col = f"Unnamed: {i if index_col is None else i + 1}_level_0"
for i, col in enumerate(columns):
if not col: # Unnamed.
col = f"Unnamed: {i if index_col is None else i + 1}_level_0"

exp_columns.append(col)
exp_columns.append(col)

columns = MultiIndex.from_tuples(zip(exp_columns, ["0", "1"]))
expected = DataFrame([[2, 3], [4, 5]], columns=columns)
tm.assert_frame_equal(result, expected)
columns = MultiIndex.from_tuples(zip(exp_columns, ["0", "1"]))
expected = DataFrame([[2, 3], [4, 5]], columns=columns)
tm.assert_frame_equal(result, expected)


@skip_pyarrow
Expand Down
21 changes: 11 additions & 10 deletions pandas/tests/io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import pytest

from pandas.compat import is_platform_windows
from pandas.errors import ParserError
import pandas.util._test_decorators as td

from pandas import (
Expand Down Expand Up @@ -918,13 +917,8 @@ def test_wikipedia_states_multiindex(self, datapath):
assert np.allclose(result.loc["Alaska", ("Total area[2]", "sq mi")], 665384.04)

def test_parser_error_on_empty_header_row(self):
msg = (
r"Passed header=\[0,1\] are too many "
r"rows for this multi_index of columns"
)
with pytest.raises(ParserError, match=msg):
self.read_html(
"""
result = self.read_html(
"""
<table>
<thead>
<tr><th></th><th></tr>
Expand All @@ -935,8 +929,15 @@ def test_parser_error_on_empty_header_row(self):
</tbody>
</table>
""",
header=[0, 1],
)
header=[0, 1],
)
expected = DataFrame(
[["a", "b"]],
columns=MultiIndex.from_tuples(
[("Unnamed: 0_level_0", "A"), ("Unnamed: 1_level_0", "B")]
),
)
tm.assert_frame_equal(result[0], expected)

def test_decimal_rows(self):
# GH 12907
Expand Down