Community Conundrum 25:Feature Visualization is now live! Read More

Save pandas dataframe to .xlsx in managed S3 folder

Level 2
Save pandas dataframe to .xlsx in managed S3 folder

Dear all, 

In a similar vein to the question Save pandas dataframe to .csv in managed S3 folder I would like to know how to write an excel file to the same type of managed S3 folder. I have successfully been able to write CSVs, as well as, images (as explained here ) to said folder, but when I attempt to save my pandas dataframe to an excel file using the same syntax as for the CSV:

with handler.get_writer("my_df.xlsx") as w:
    df.to_excel(w)

I get the following error:

AttributeError: ManagedFolderWriter instance has no attribute 'tell'

Could anybody help me with this issue?

Regards, 

Mario

1 Reply
Level 2
Author

Nevermind, the solution was right under my nose. In fact, the contents of the dataframe must first be written to a BytesIO object which then is uploaded to the managed folder:

buf = io.BytesIO()
df.to_excel(buf)

handler.upload_stream('my_df.xlsx', buf.getvalue())
buf.close()

So, much like saving an image to the same type of folder.