Turn pandas dataframe into a file-like object in memory?

Python module io(docs) has necessary tools for file-like objects.

import io

# text buffer
s_buf = io.StringIO()

# saving a data frame to a buffer (same as with a regular file):
df.to_csv(s_buf)

Edit.
(I forgot) In order to read from the buffer afterwards, its position should be set to the beginning:

s_buf.seek(0)

I’m not familiar with psycopg2 but according to docs both copy_expert and copy_from can be used, for example:

cur.copy_from(s_buf, table)

(For Python 2, see StringIO.)

Leave a Comment