Description
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
from io import StringIO
import pandas as pd
data = """Date,Time,Value date,Product,ISIN,Description,FX,Change,,Balance,,Order Id
08-07-2021,04:33,07-07-2021,AAPL,,Dividend,,USD,0.97,USD,0.82,
09-07-2021,16:05,09-07-2021,Interest,LU0904784781,Buy 0.59 at 1 GBP,,,,GBP,109.53,"""
fields_en = (
'Date',
'Time',
'Value date',
'Product',
'ISIN',
'Description',
'FX',
'c_change',
'Change', # unknown
'c_balance',
'Balance', # unknown
'Order Id'
)
def format_datetime(x):
return pd.to_datetime(x, format='%d-%m-%Y %H:%M')
if __name__ == '__main__':
lines = data.splitlines(True)
header = lines[0]
del lines[0]
lines = [header] + list(reversed(lines))
df = pd.read_csv(
StringIO(''.join(lines)), encoding='utf-8', header=0, names=fields_en,
parse_dates={'datetime': ['Date', 'Time']},
date_parser=format_datetime,
)
Problem description
The above script produces following stack trace:
Traceback (most recent call last):
File "/home/fugkco/tmp/beancount-degiro/venv/lib/python3.9/site-packages/pandas/io/parsers/readers.py", line 488, in _read
return parser.read(nrows)
File "/home/fugkco/tmp/beancount-degiro/venv/lib/python3.9/site-packages/pandas/io/parsers/readers.py", line 1047, in read
index, columns, col_dict = self._engine.read(nrows)
File "/home/fugkco/tmp/beancount-degiro/venv/lib/python3.9/site-packages/pandas/io/parsers/c_parser_wrapper.py", line 260, in read
raise NotImplementedError("file structure not yet supported")
NotImplementedError: file structure not yet supported
What's really odd is that adding a single new line at the end of this data, seems to fix the issue, i.e.:
data = """Date,Time,Value date,Product,ISIN,Description,FX,Change,,Balance,,Order Id
08-07-2021,04:33,07-07-2021,AAPL,,Dividend,,USD,0.97,USD,0.82,
09-07-2021,16:05,09-07-2021,Interest,LU0904784781,Buy 0.59 at 1 GBP,,,,GBP,109.53,
"""
Does not produce the same exception. After investigating using a debugger, it seems that for reason I do not understand, not having the new line causes _reader.leading_cols
to contain more than 0 columns, causing it to go into this branch. This seems odd behaviour, and it sounds to me like having a single newline or not at the end of the file shouldn't make a difference in the output.
Expected Output
No errors
Output of pd.show_versions()
INSTALLED VERSIONS
commit : f00ed8f
python : 3.9.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.0-7620-generic
Version : #21162437974721.04~3abeff8-Ubuntu SMP Wed Jun 23 02:34:03 UTC
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.3.0
numpy : 1.21.0
pytz : 2021.1
dateutil : 2.8.1
pip : 20.3.4
setuptools : 44.1.1
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
None