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: Inconsistent handling of short lines #49175

Closed
3 tasks done
joelsonoda opened this issue Oct 18, 2022 · 3 comments
Closed
3 tasks done

BUG: Inconsistent handling of short lines #49175

joelsonoda opened this issue Oct 18, 2022 · 3 comments
Labels
Bug Duplicate Report Duplicate issue or pull request IO CSV read_csv, to_csv

Comments

@joelsonoda
Copy link
Contributor

joelsonoda commented Oct 18, 2022

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

@joelsonoda joelsonoda added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 18, 2022
@phofl
Copy link
Member

phofl commented Oct 19, 2022

Hi, thanks for your report.

we have a couple of open issues about this. Could you please search the tracker and link the related once’s so that we can close here?

@olsgaard
Copy link

I would have expected all scenarios to give a ParseError. The documentation doesn't say anything about how such a scenario is expected to be handled. Which ever way we go, it should be added to the documentation.

Your example code needs to either rewind the single_line_csv object or set it again before calling the .read_csv() method a second time.

# 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
)
File ~/miniconda3/envs/gds/lib/python3.9/site-packages/pandas/io/parsers/c_parser_wrapper.py:243, in CParserWrapper.read(self, nrows)
    241 try:
    242     if self.low_memory:
--> 243         chunks = self._reader.read_low_memory(nrows)
    244         # destructive to chunks
    245         data = _concatenate_chunks(chunks)

File ~/miniconda3/envs/gds/lib/python3.9/site-packages/pandas/_libs/parsers.pyx:808, in pandas._libs.parsers.TextReader.read_low_memory()

File ~/miniconda3/envs/gds/lib/python3.9/site-packages/pandas/_libs/parsers.pyx:890, in pandas._libs.parsers.TextReader._read_rows()

File ~/miniconda3/envs/gds/lib/python3.9/site-packages/pandas/_libs/parsers.pyx:952, in pandas._libs.parsers.TextReader._convert_column_data()

ParserError: Too many columns specified: expected 3 and found 1

@phofl
Copy link
Member

phofl commented Oct 19, 2022

#47566 and #40587

@phofl phofl closed this as completed Oct 19, 2022
@phofl phofl added Duplicate Report Duplicate issue or pull request IO CSV read_csv, to_csv and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request IO CSV read_csv, to_csv
Projects
None yet
Development

No branches or pull requests

3 participants