python-3.x - 将 Pandas 数据帧保存到 S3 的最快方法是什么?

标签 python-3.x pandas amazon-s3

我试图找出将 LARGE pandas DataFrame 写入 S3 文件系统的最快方法。我目前正在尝试两种方式:

1)通过gzip压缩(BytesIO)和boto3

gz_buffer = BytesIO()

with gzip.GzipFile(mode='w', fileobj=gz_buffer) as gz_file:
    df.to_csv(TextIOWrapper(gz_file, 'utf8'), index=False)

s3_resource = boto3.resource('s3')
s3_object = s3_resource.Object(bucket, s3_path + name_zip)
s3_object.put(Body=gz_buffer.getvalue())

对于 7M 行的数据帧,写入 S3 大约需要 420 秒。

2)通过不压缩写入csv文件(StringIO缓冲区)

csv_buffer = StringIO()
data.to_csv(csv_buffer)
s3_resource = boto3.resource('s3')
s3_resource.Object(bucket, s3_path + name_csv).put(Body=csv_buffer.getvalue())

大约需要 371 秒...

问题是:
有没有其他更快的方法可以将 Pandas 数据帧写入 S3?

最佳答案

使用分段上传可以更快地传输到 S3。压缩使文件更小,所以这也有帮助。

import boto3
s3 = boto3.client('s3')

csv_buffer = BytesIO()
df.to_csv(csv_buffer, compression='gzip')

# multipart upload
# use boto3.s3.transfer.TransferConfig if you need to tune part size or other settings
s3.upload_fileobj(csv_buffer, bucket, key)
s3.upload_fileobj 的文档在这里:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.upload_fileobj

关于python-3.x - 将 Pandas 数据帧保存到 S3 的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55404263/

相关文章:

python - 导入 .txt 文件,其中包含逗号且数字之间没有空格

python-3.x - Python - 计算两个 datetime.time 对象之间的差异

amazon-web-services - AWS如何在视频上传后自动触发mediaconvert

python - 为什么这会返回 'Too Many Indexers' ?

amazon-web-services - 在 Web 应用程序中公开 AWS S3 签名 URL?

amazon-s3 - CORS、Amazon S3 和 Rails - 在 IE 10 和 Safari 上失败

python-3.x - 如何从python中的列表中删除特殊字符

python - 如何在 OSX 上通过 Python Tkinter 安装和使用 TkDnD?

python - 如何根据pandas中的时间值过滤数据?

python - 处理可变列数数据框 - Python