Skip to content

BUG: Regression on pandas 1.5.0 when using a file handle with utf-8-encoding which has been read from before for read_csv #48783

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

Closed
2 of 3 tasks
mawwinius opened this issue Sep 26, 2022 · 2 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@mawwinius
Copy link

mawwinius commented Sep 26, 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 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() with encoding="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 to f.readline() and option skiprows=1 to read_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

INSTALLED VERSIONS ------------------ commit : 87cfe4e python : 3.10.7.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19044 machine : AMD64 processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : de_DE.cp1252

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

@mawwinius mawwinius added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 26, 2022
@twoertwein
Copy link
Member

This is already fixed on main #48646 and will be included in 1.5.1.

@mawwinius
Copy link
Author

Ah, thanks, I missed it when searching the known issues.

@mawwinius mawwinius closed this as not planned Won't fix, can't repro, duplicate, stale Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants