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: CSV C engine raises an error on single line CSV with no header when passing extra names #47566

Closed
2 of 3 tasks
alexifm opened this issue Jul 1, 2022 · 5 comments · Fixed by #49182
Closed
2 of 3 tasks
Labels
Bug Duplicate Report Duplicate issue or pull request IO CSV read_csv, to_csv

Comments

@alexifm
Copy link

alexifm commented Jul 1, 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 io
import pandas as pd

stream_without_header = io.StringIO("1,2")

pd.read_csv(stream_without_header, header=None, names=["a", "b"])
stream_without_header.seek(0)

pd.read_csv(
    stream_without_header,
    header=None,
    names=["a", "b", "c"],
    engine="python",
)
stream_without_header.seek(0)

# this will raise an error
pd.read_csv(
    stream_without_header,
    header=None,
    names=["a", "b", "c"],
    engine="c",
)

# add another line and read_csv works fine
stream_with_2lines_without_header = io.StringIO("1,2\n3,4")
pd.read_csv(
    stream_with_2lines_without_header,
    header=None,
    names=["a", "b", "c"],
    engine="c",
)

Issue Description

The C engine for parsing CSV does not behave the same as the Python engine when it comes to reading single line CSV files without a header row. The example shows how the Python engine behaves as expected (the extra specified row is returned withNaN) while the C engine raises an error.

Expected Behavior

The two engines should behave the same and given the behavior for 2+ lines, the 1 line example should work correctly.

Installed Versions

In [2]: pd.show_versions()

INSTALLED VERSIONS

commit : e8093ba
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 21.5.0
Version : Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.3
numpy : 1.22.4
pytz : 2022.1
dateutil : 2.8.2
setuptools : 47.1.0
pip : 22.1.2
Cython : 0.29.30
pytest : 7.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.0
html5lib : None
pymysql : 1.0.2
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : 2022.5.0
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : 2022.5.0
scipy : 1.8.1
snappy : None
sqlalchemy : 1.4.37
tables : None
tabulate : 0.8.9
xarray : 2022.3.0
xlrd : 2.0.1
xlwt : None
zstandard : None

@alexifm alexifm added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 1, 2022
@alexifm
Copy link
Author

alexifm commented Jul 1, 2022

This issue was sort of similar but it's not the same since the example no longer raises an error. #26218

@phofl
Copy link
Member

phofl commented Jul 1, 2022

Hi, thanks for the report.

this is the same cause as #40587

@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 Jul 1, 2022
@alexifm
Copy link
Author

alexifm commented Jul 1, 2022

Okay, thanks.

Not sure how it relates to that other issue especially since the C engine works as expected for 2 rows. I think I'm documenting an explicit case with 1 row where it has incorrect behavior.

@phofl
Copy link
Member

phofl commented Jul 1, 2022

Files are read in chunks, and if the first line of any chunk is off, we get an error.

@alexifm
Copy link
Author

alexifm commented Jul 1, 2022

Okay. I will close the issue since it's a duplicate.

@alexifm alexifm closed this as completed Jul 1, 2022
joelsonoda pushed a commit to joelsonoda/pandas that referenced this issue Oct 19, 2022
…hen passing extra names (pandas-dev#47566)

* Expect the provided number of columns when the names property is set

* Add tests to demonstrate handling of files with a single row with
  fewer columns.
joelsonoda pushed a commit to joelsonoda/pandas that referenced this issue Oct 19, 2022
…hen passing extra names (pandas-dev#47566)

* Expect the provided number of columns when the names property is set

* Add tests to demonstrate handling of files with a single row with
  fewer columns.
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

Successfully merging a pull request may close this issue.

2 participants