2

I have a io.BytesIO object, iostream, which is a be2 file read from disk, and I am going to append column headers to the table/iostream,

f = io.BytesIO()
f.write(b'A,B,C,D\n')
f.write(iostream.getvalue())

pd.read_table(f, sep=',', index_col=False, error_bad_lines=False, encoding='utf-8', dtype=type_map)

but it gave me an error,

pandas.errors.EmptyDataError: No columns to parse from file

I am wondering how to solve this issue.

Also tried

f = io.StringIO()
f.write('A,B,C,D\n')    
f.write(iostream.getvalue().decode())

pd.read_table(f, sep=',', index_col=False, error_bad_lines=False, encoding='utf-8', dtype=type_map)

got error

pandas.errors.ParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.
daiyue
  • 7,196
  • 25
  • 82
  • 149

1 Answers1

5

I managed to reproduce your error. The problem that you have on your first try is that you are at the end of the stream 'f' at the moment of calling to 'pd.read_table', as you have just written all the contents. 'pd.read_table' calls internally to read(), which read from your current position. So it returns an empty string. This is the cause of error:

 pandas.errors.EmptyDataError: No columns to parse from file

The solution is simple. You just have to move again to the begining of the stream with 'seek'. This code worked for me:

f = io.BytesIO()
f.write(b'A,B,C,D\n')
f.write(iostream.getvalue())
f.seek(0)

pd.read_table(f, sep=',', index_col=False, error_bad_lines=False, encoding='utf-8')
migjimen
  • 551
  • 1
  • 4
  • 6