gpt4 book ai didi

python - Fastparquet 在使用 dataframe.to_parquet() 时给出 "TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO"

转载 作者:行者123 更新时间:2023-12-04 12:00:31 26 4
gpt4 key购买 nike

我正在尝试为 AWS Lambda 创建代码以将 csv 转换为 parquet。我可以使用 Pyarrow 做到这一点,但它的大小太大(约 200 MB 未压缩),因此我无法在 Lambda 的部署包中使用它。我正在尝试使用 BytesIO 库直接将 Parquet 文件写入 s3 存储桶。

下面是我的 lambda 函数代码:

import json
import boto3
import pandas as pd
from io import BytesIO


def lambda_handler():

s3 = boto3.client('s3')
response = s3.list_objects_v2(
Bucket = 'mybucket',
Prefix = 'subfolder/'
)
files = get_object_keys(response)
for file in files:
obj = s3.get_object(Bucket='mybucket', Key=file)
df = pd.read_csv(obj['Body'], sep='|')


buf = BytesIO()
df.to_parquet(buf, engine='fastparquet', index=False, compression='snappy')
buf.seek(0)
key = f"output/{file.split('/')[1].split('.')[0]}.parquet"
s3.put_object(Bucket='mybucket', Body=buf.getvalue(), Key=key)

def get_object_keys(response):

files = []
for content in response['Contents']:
if content['Key'].endswith('.csv'):
files.append(content['Key'])
return files

lambda_handler()

当我在 dataframe.to_parquet() 中使用“fastparquet”作为引擎时,出现以下错误:
Traceback (most recent call last):
File ".\lambda_function.py", line 77, in <module>
lambda_handler()
File ".\lambda_function.py", line 64, in lambda_handler
df.to_parquet(buf, engine='fastparquet', index=False, compression='snappy')
File "C:\Users\tansingh\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\util\_decorators.py", line 214, in wrapper
return func(*args, **kwargs)
File "C:\Users\tansingh\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\frame.py", line 2116, in to_parquet
**kwargs,
File "C:\Users\tansingh\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parquet.py", line 264, in to_parquet
**kwargs,
File "C:\Users\tansingh\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parquet.py", line 185, in write
**kwargs,
File "C:\Users\tansingh\AppData\Local\Programs\Python\Python37\lib\site-packages\fastparquet\writer.py", line 880, in write
compression, open_with, has_nulls, append)
File "C:\Users\tansingh\AppData\Local\Programs\Python\Python37\lib\site-packages\fastparquet\writer.py", line 734, in write_simple
with open_with(fn, mode) as f:
File "C:\Users\tansingh\AppData\Local\Programs\Python\Python37\lib\site-packages\fastparquet\util.py", line 42, in default_open
return open(f, mode)
TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO

有谁知道如何解决这一问题?

最佳答案

此错误已通过使用 pyarrow 作为写入引擎解决。
示例代码。

buffer = ioBytesIO()
df.to_parquet(buffer, engine="pyarrow", index = False)
s3_resource = boto3.resource('s3')
s3_resouce.Object(`bucketname`, `path_withfilename`).put(body = buffer.getvalue())
为了在 python 3.6 中读取 Parquet 文件,我使用了 fastparquet,但是为了编写 pyarrow 引擎似乎可以工作。

关于python - Fastparquet 在使用 dataframe.to_parquet() 时给出 "TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61365145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com