-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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: KeyError when loading csv with NaNs #56929
Comments
Thanks for the report! Confirmed on main, further investigations and PRs to fix are welcome! |
I narrowed it down as far as possible within the python part of the codebase. The faulty example gives the following when running within a debug session: You can see the difference in the Index, in the faulty example it takes 0 as name instead of the value. From here onwards it calls cython code and I'm not sure how to debug it properly, are there any guidelines for this? Just from looking at the parsers I assume the error occurs somewhere here: self.allow_leading_cols = allow_leading_cols
self.leading_cols = 0 # updated in _get_header
# TODO: no header vs. header is not the first row
self.has_mi_columns = 0
self.orig_header = header
if header is None:
# sentinel value
self.parser.header_start = -1
self.parser.header_end = -1
self.parser.header = -1
self.parser_start = 0
prelim_header = []
else:
if isinstance(header, list):
if len(header) > 1:
# need to artificially skip the final line
# which is still a header line
header = list(header)
header.append(header[-1] + 1)
self.parser.header_end = header[-1]
self.has_mi_columns = 1
else:
self.parser.header_end = header[0]
self.parser_start = header[-1] + 1
self.parser.header_start = header[0]
self.parser.header = header[0]
prelim_header = header
else:
self.parser.header_start = header
self.parser.header_end = header
self.parser_start = header + 1
self.parser.header = header
prelim_header = [header] Edit: More precisely, the TextReader class seems to think the first empty line is part of the header: Edit 2: pandas/pandas/_libs/parsers.pyx Lines 694 to 698 in f459437
where an empty header can not be differentiated from an empty first value. I am still wondering why we even look at the third row, given header is specified as [0,1]. This seems to be enforced here: pandas/pandas/_libs/parsers.pyx Lines 553 to 556 in f459437
Without this line, it is a trivial fix as we would not try to read the third line as a header, but I assume this is there for a reason @rhshadrach? |
Thanks for investigating here!
As a next step I would try removing it and see if any tests in |
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
When loading back a CSV file previously saved using pandas, the first row is omitted if it contains NaN values. The problem seems to be the default behaviour for replacing NaN with ''. If we force replacement to be 'NaN' the same example works, see below:
Maybe related Issue: #55803
Expected Behavior
Installed Versions
INSTALLED VERSIONS
commit : 0f43794
python : 3.8.16.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-91-generic
Version : #101~20.04.1-Ubuntu SMP Thu Nov 16 14:22:28 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.0.3
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.8.0
pip : 23.1.2
Cython : 3.0.0
pytest : 7.4.0
hypothesis : None
sphinx : 7.1.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.2
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : 3.8.0
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : 2.4.1
pyqt5 : None
The text was updated successfully, but these errors were encountered: