Description
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 as pd
with open("csv_with_extra_first_line.csv", encoding="utf-8") as f:
f.readline()
print(pd.read_csv(f))
Issue Description
Using a csv file which has an extra first line (e.g. the attached file "csv_with_extra_first_line.csv"), the above code raises an EmptyDataError
in pandas 1.5.0, but behaves as expected in 1.4.4 and earlier. See stacktrace:
Traceback (most recent call last):
File "test_read_csv.py", line 5, in <module>
print(pd.read_csv(f))
File "lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
return func(*args, **kwargs)
File "lib\site-packages\pandas\util\_decorators.py", line 317, in wrapper
return func(*args, **kwargs)
File "lib\site-packages\pandas\io\parsers\readers.py", line 950, in read_csv
return _read(filepath_or_buffer, kwds)
File "lib\site-packages\pandas\io\parsers\readers.py", line 605, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "lib\site-packages\pandas\io\parsers\readers.py", line 1442, in __init__
self._engine = self._make_engine(f, self.engine)
File "lib\site-packages\pandas\io\parsers\readers.py", line 1747, in _make_engine
return mapping[engine](f, **self.options)
File "lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 92, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 554, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file
The following conditions have to be met for the issue to manifest:
- call to
open()
withencoding="utf-8"
(behaves as expected without encoding specified) - There have to be prior calls to the file handle:
f.readline()
or similar (file handle with encoding, but no call tof.readline()
and optionskiprows=1
toread_csv
is also fine) - The default engine has to be used.
engine="python"
also works as expected.
In pandas 1.4.4 and earlier, the code behaves as expected (see below).
The issue is present both on Windows (see version below) and linux (latest and 3.8 python docker image).
Content of the attached file:
This line will be read first by f.readline()
1,2
1,2
1,2
Expected Behavior
The csv file should parse as it would with pd.read_csv("csv_with_extra_first_line.csv", skiprows=1)
, which yields the following:
1 2
0 1 2
1 1 2
Installed Versions
pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 60.2.0
pip : 21.3.1
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
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