Skip to content

Commit 8f81d0e

Browse files
authored
ENH: Improve error message when names and usecols do not match for read_csv with engine=c (#38452)
1 parent 241f0ff commit 8f81d0e

File tree

3 files changed

+4
-8
lines changed

3 files changed

+4
-8
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Other enhancements
2020
^^^^^^^^^^^^^^^^^^
2121

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

2525
.. ---------------------------------------------------------------------------
2626

pandas/_libs/parsers.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,8 @@ cdef class TextReader:
738738
elif self.names is None and nuse < passed_count:
739739
self.leading_cols = field_count - passed_count
740740
elif passed_count != field_count:
741-
raise ValueError('Passed header names '
742-
'mismatches usecols')
741+
raise ValueError('Number of passed names did not match number of '
742+
'header fields in the file')
743743
# oh boy, #2442, #2981
744744
elif self.allow_leading_cols and passed_count < field_count:
745745
self.leading_cols = field_count - passed_count

pandas/tests/io/parser/test_usecols.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ def test_usecols_name_length_conflict(all_parsers):
104104
7,8,9
105105
10,11,12"""
106106
parser = all_parsers
107-
msg = (
108-
"Number of passed names did not match number of header fields in the file"
109-
if parser.engine == "python"
110-
else "Passed header names mismatches usecols"
111-
)
107+
msg = "Number of passed names did not match number of header fields in the file"
112108

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

0 commit comments

Comments
 (0)