You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importpandasimportiocol_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 successfullysingle_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 parsesingle_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
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 parsesingle_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
Uh oh!
There was an error while loading. Please reload this page.
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
Issue Description
We import jagged csv files by specifying the column names in the
read_csv
and have typically used thec
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
The text was updated successfully, but these errors were encountered: