Skip to content
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

BUG:read_csv misleading handling of csv files containing rows with different number of delimiters #36069

Open
2 of 3 tasks
saleepeppe opened this issue Sep 2, 2020 · 2 comments
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas IO CSV read_csv, to_csv Needs Discussion Requires discussion from core team before further action

Comments

@saleepeppe
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

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


Code Sample, a copy-pastable example

import pandas as pd
pd.read_csv('fake.csv')

fake.csv file

col1,col2,col3,col4,col5
a,a,a,a,a
a,a,a

Problem description

Pandas read_csv function should throw an error, or at least a warning, when a csv contains a row with a number of delimiters less than the number of delimiters contained in the header row (the first raw?).
For example, if the number of delimiters is greater an exception is thrown.

Expected Output

Error or warning.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2ca0a2
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-42-generic
Version : #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.1.1
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.6.0.post20200814
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 : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@saleepeppe saleepeppe added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 2, 2020
@smithto1
Copy link
Member

smithto1 commented Sep 3, 2020

Thanks for raising.

It looks like this behaviour was deliberately added to make it easier to read ragged csv's: #2981

Looking at Pythons csv library it appears to behave the same (see csv.DictReader).

So perhaps it would be useful to have a flag to raise an error on a ragged csv, ragged='warn'/'raise'/None, since it doesn't seem to be available anywhere in Python. It may be hard to make it interact with the header row(s) that the user can specify dynamically, but it may be possible to enforce that all rows have the same number of columns.

For now , the simplest work-around I can see would be using the csv library actually.

import csv
from io import StringIO

data = """col1,col2,col3,col4,col5
1,1,1,1,1
2,2,2
3,NA,,NA,3"""

reader = csv.reader(StringIO(data))
n_cols = len(next(reader))
for row in reader:
    assert len(row) == n_cols

Only using pandas, you could manipulate na_values and keep_default_na so that you don't read blanks as NaNs, '', these will then come through as empty strings and you could check for them in the last column.

df = pd.read_csv(StringIO(data), na_values='NA', keep_default_na=False)

assert not df.iloc[:,-1].eq('').any()

@smithto1 smithto1 added Error Reporting Incorrect or improved errors from pandas Needs Discussion Requires discussion from core team before further action Enhancement and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 3, 2020
@mroeschke mroeschke added the IO CSV read_csv, to_csv label Aug 13, 2021
@ivasve
Copy link

ivasve commented Jul 11, 2022

Hi, @saleepeppe any news on this issue? I saw that @smithto1 commented quite thoroughly on this issue and also offered some workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas IO CSV read_csv, to_csv Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

4 participants