Skip to content

ENH: Improve error message when names and usecols do not match for read_csv with engine=c #38452

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 3 commits into from
Dec 13, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Other enhancements
^^^^^^^^^^^^^^^^^^

- Added :meth:`MultiIndex.dtypes` (:issue:`37062`)
-
- Improve error message when ``usecols`` and ``names`` do not match for :func:`read_csv` and ``engine="c"`` (:issue:`29042`)

.. ---------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,8 @@ cdef class TextReader:
elif self.names is None and nuse < passed_count:
self.leading_cols = field_count - passed_count
elif passed_count != field_count:
raise ValueError('Passed header names '
'mismatches usecols')
raise ValueError('Number of passed names did not match number of '
'header fields in the file')
# oh boy, #2442, #2981
elif self.allow_leading_cols and passed_count < field_count:
self.leading_cols = field_count - passed_count
Expand Down
6 changes: 1 addition & 5 deletions pandas/tests/io/parser/test_usecols.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ def test_usecols_name_length_conflict(all_parsers):
7,8,9
10,11,12"""
parser = all_parsers
msg = (
"Number of passed names did not match number of header fields in the file"
if parser.engine == "python"
else "Passed header names mismatches usecols"
)
msg = "Number of passed names did not match number of header fields in the file"

with pytest.raises(ValueError, match=msg):
parser.read_csv(StringIO(data), names=["a", "b"], header=None, usecols=[1])
Expand Down