Skip to content

BUG: Unable to multi column "complex" dates (file structure not yet supported) #42497

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
fugkco opened this issue Jul 12, 2021 · 2 comments
Closed
2 of 3 tasks
Labels
IO CSV read_csv, to_csv Usage Question

Comments

@fugkco
Copy link

fugkco commented Jul 12, 2021

  • 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

@fugkco fugkco added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 12, 2021
@mroeschke mroeschke added IO CSV read_csv, to_csv and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 21, 2021
@phofl
Copy link
Member

phofl commented Dec 23, 2021

Can you provide a minimal example? This is really confusing. Write the data down as passed to read_csv and provide only a minimun number of columns

@phofl
Copy link
Member

phofl commented Dec 23, 2021

YOu have to join with \n not with ''

Closing as not a pandas issue

@phofl phofl closed this as completed Dec 23, 2021
@phofl phofl added Usage Question and removed Bug labels Dec 23, 2021
@phofl phofl added this to the No action milestone Dec 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO CSV read_csv, to_csv Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants