Skip to content

BUG: read_csv accepts bad lines when option usecols is used #40049

@lodo1995

Description

@lodo1995
  • I have checked that this issue has not already been reported (couldn't find it)

  • I have confirmed this bug exists on the latest version of pandas (current version is 1.2.2 on conda)

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

Bad CSV file:

A,B,C
foo,1,2
bar,bar,3,4
baz,5,6

Python code:

import pandas as pd

df_all = pd.read_csv('bad.csv', error_bad_lines = False, warn_bad_lines = False)
print(df_all)
#      A  B  C
# 0  foo  1  2
# 1  baz  5  6

df_sub = pd.read_csv('bad.csv', error_bad_lines = False, warn_bad_lines = False, usecols = ['A','B'])
print(df_sub)
#      A    B
# 0  foo    1
# 1  bar  bar
# 2  baz    5

Problem description

As can be seen in the example above, when not using usecols, read_csv correctly ignores the line that is too long.
However, this is not the case when usecols is used.

Expected Output

Using usecols should not affect which lines are discarded due to being too long.

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : 7d32926db8f7541c356066dcadabf854487738de
python           : 3.8.6.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.3.0-64-generic
Version          : #58-Ubuntu SMP Fri Jul 10 19:33:51 UTC 2020
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.2.2
numpy            : 1.20.1
pytz             : 2021.1
dateutil         : 2.8.1
pip              : 21.0.1
setuptools       : 49.6.0.post20210108
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : None
pandas_datareader: None
bs4              : None
bottleneck       : None
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : 3.3.4
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyxlsb           : None
s3fs             : None
scipy            : 1.6.0
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
numba            : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeprecateFunctionality to remove in pandasIO CSVread_csv, to_csvNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions