Skip to content

ENH: Raise ParserWarning when length of names does not match length of data #38587

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 27 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d98c6fd
ENH: Raise ParserWarning when length of names does not match length o…
phofl Dec 19, 2020
26b07b2
Fix bugs from strg+z
phofl Dec 19, 2020
7dd3f1b
Refactor code
phofl Dec 19, 2020
70d5c1c
Refactor if else
phofl Dec 19, 2020
76abd33
Add okwarning
phofl Dec 19, 2020
31929f4
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl Dec 23, 2020
3813435
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl Jan 3, 2021
5b688f7
Allow trailing commas
phofl Jan 3, 2021
56cdd18
Fix dtype bug
phofl Jan 4, 2021
ac15a30
Fix npdev bug
phofl Jan 4, 2021
4b08ab6
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl Jan 4, 2021
387b5fa
Add missing init file
phofl Jan 4, 2021
53cac93
Remove empty file
phofl Jan 4, 2021
5d142fe
Add warning
phofl Jan 4, 2021
764e002
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl Jan 17, 2021
8bd631a
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl Feb 19, 2021
b21b795
Merge master
phofl Feb 19, 2021
5c19c9f
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl Mar 2, 2021
928ad4f
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl Apr 20, 2021
eb77157
Fix typing
phofl Apr 20, 2021
9dce995
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl May 14, 2021
16faf35
Change test
phofl May 14, 2021
4b3f63a
Remove warning
phofl May 14, 2021
ca2f026
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl May 23, 2021
fa6fed0
Adress comments
phofl May 23, 2021
afb023f
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl Jun 3, 2021
95770d1
Merge branch 'master' of https://github.com/pandas-dev/pandas into 21768
phofl Jun 12, 2021
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.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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`)
- :func:`read_csv` now raising ``ParserWarning`` if length of header or given names does not match length of data when usecols is not specified (:issue:`21768`)

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

Expand Down
9 changes: 8 additions & 1 deletion pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,9 @@ cdef class TextReader:
field_count = max(field_count, len(self.names))

passed_count = len(header[0])

print(self.allow_leading_cols)
print(passed_count)
print(field_count)
if (self.has_usecols and self.allow_leading_cols and
not callable(self.usecols)):
nuse = len(self.usecols)
Expand All @@ -743,6 +745,11 @@ cdef class TextReader:
# oh boy, #2442, #2981
elif self.allow_leading_cols and passed_count < field_count:
self.leading_cols = field_count - passed_count
elif not self.allow_leading_cols and passed_count < field_count:
warnings.warn(
"Length of header or names does not match length of data. This leads "
"to a loss of data with index_col=False.", ParserWarning, stacklevel=6,
)

return header, field_count, unnamed_cols

Expand Down
8 changes: 8 additions & 0 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,14 @@ def _exclude_implicit_index(self, alldata):
if self._col_indices is not None and len(names) != len(self._col_indices):
names = [names[i] for i in sorted(self._col_indices)]

if not self.index_col and len(names) != len(alldata) and names:
warnings.warn(
"Length of header or names does not match length of data. This leads "
"to a loss of data with index_col=False.",
ParserWarning,
stacklevel=6,
)

return {name: alldata[i + offset] for i, name in enumerate(names)}, names

# legacy
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pytest

from pandas._libs.tslib import Timestamp
from pandas.errors import DtypeWarning, EmptyDataError, ParserError
from pandas.errors import DtypeWarning, EmptyDataError, ParserError, ParserWarning
import pandas.util._test_decorators as td

from pandas import DataFrame, Index, MultiIndex, Series, compat, concat, option_context
Expand Down Expand Up @@ -1062,14 +1062,17 @@ def test_skip_initial_space(all_parsers):
tm.assert_frame_equal(result, expected)


@pytest.mark.filterwarnings("ignore:Lenght of header:pandas.errors.ParserWarning")
def test_trailing_delimiters(all_parsers):
# see gh-2442
data = """A,B,C
1,2,3,
4,5,6,
7,8,9,"""
parser = all_parsers
result = parser.read_csv(StringIO(data), index_col=False)

with tm.assert_produces_warning(ParserWarning):
result = parser.read_csv(StringIO(data), index_col=False)

expected = DataFrame({"A": [1, 4, 7], "B": [2, 5, 8], "C": [3, 6, 9]})
tm.assert_frame_equal(result, expected)
Expand Down Expand Up @@ -2178,7 +2181,8 @@ def test_no_header_two_extra_columns(all_parsers):
ref = DataFrame([["foo", "bar", "baz"]], columns=column_names)
stream = StringIO("foo,bar,baz,bam,blah")
parser = all_parsers
df = parser.read_csv(stream, header=None, names=column_names, index_col=False)
with tm.assert_produces_warning(ParserWarning):
df = parser.read_csv(stream, header=None, names=column_names, index_col=False)
tm.assert_frame_equal(df, ref)


Expand Down