Skip to content

BUG: Inconsistent handling of short lines #49175

Closed
@joelsonoda

Description

@joelsonoda

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas
import io

col_names = ["col_1", "col_2", "col_3"]

multi_line_csv = io.StringIO("1\n1")
pandas.read_csv(
    multi_line_csv,
    names=col_names,
    header=None
)

# With the Python engine, it will parse successfully
single_line_csv = io.StringIO("1")
pandas.read_csv(
    single_line_csv,
    engine='python',
    names=col_names,
    header=None
)

# With the default C engine, it will fail to parse
single_line_csv = io.StringIO("1")
df = pandas.read_csv(
    single_line_csv,
    names=col_names,
    header=None
)

Issue Description

We import jagged csv files by specifying the column names in the read_csv and have typically used the c engine for performance reasons.

When there happens to be only one line in the csv file, the import will fail with pandas.errors.ParserError: Too many columns specified.

If there is more than one row, the read_csv works as we expected.

If we use the python engine, it will work as expected even with a single row.

Expected Behavior

I expected that given fewer columns in rows than the specified column names, we should be able to read a csv file even if there is only one row.

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-48-generic
Version : #54-Ubuntu SMP Fri Aug 26 13:26:29 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.0
numpy : 1.23.4
pytz : 2022.5
dateutil : 2.8.2
setuptools : 65.3.0
pip : 22.2.2
Cython : 0.29.32
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
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions