I’m trying to write a pandas dataframe as a pickle file into an s3 bucket in AWS. I know that I can write dataframe new_df
as a csv to an s3 bucket as follows:
JavaScript
x
9
1
bucket='mybucket'
2
key='path'
3
4
csv_buffer = StringIO()
5
s3_resource = boto3.resource('s3')
6
7
new_df.to_csv(csv_buffer, index=False)
8
s3_resource.Object(bucket,path).put(Body=csv_buffer.getvalue())
9
I’ve tried using the same code as above with to_pickle()
but with no success.
Advertisement
Answer
I’ve found the solution, need to call BytesIO into the buffer for pickle files instead of StringIO (which are for CSV files).
JavaScript
1
9
1
import io
2
import boto3
3
4
pickle_buffer = io.BytesIO()
5
s3_resource = boto3.resource('s3')
6
7
new_df.to_pickle(pickle_buffer)
8
s3_resource.Object(bucket, key).put(Body=pickle_buffer.getvalue())
9