Skip to main content
 首页 » 编程设计

python之将 pandas df 转换为 parquet-file-bytes-object

2025年05月04日21exmyth

我有一个 pandas 数据框,想将其作为 parquet 文件写入 Azure 文件存储。

到目前为止,我无法将数据帧直接转换为字节,然后我可以将其上传到 Azure。 我目前的解决方法是将其作为 Parquet 文件保存到本地驱动器,然后将其作为字节对象读取,我可以将其上传到 Azure。

谁能告诉我如何将 pandas 数据帧直接转换为“parquet 文件”-bytes 对象而不将其写入磁盘? I/O 操作真的很慢,感觉很像非常丑陋的代码......

# Transform the data_frame into a parquet file on the local drive     
data_frame.to_parquet('temp_p.parquet', engine='auto', compression='snappy') 
 
# Read the parquet file as bytes. 
with open("temp_p.parquet", mode='rb') as f: 
     fileContent = f.read() 
 
     # Upload the bytes object to Azure 
     service.create_file_from_bytes(share_name, file_path, file_name, fileContent, index=0, count=len(fileContent)) 

我正在寻找实现类似这样的东西,其中 transform_functionality 返回一个字节对象:

my_bytes = data_frame.transform_functionality() 
service.create_file_from_bytes(share_name, file_path, file_name, my_bytes, index=0, count=len(my_bytes)) 

请您参考如下方法:

我找到了一个解决方案,我会把它贴在这里,以防有人需要做同样的任务。在使用 to_parquet 文件将其写入 buffer 后,我使用 .getvalue() 功能从缓冲区中获取字节对象,如下所示:

buffer = BytesIO() 
data_frame.to_parquet(buffer, engine='auto', compression='snappy') 
 
service.create_file_from_bytes(share_name, file_path, file_name, buffer.getvalue(), index=0, count=buffer.getbuffer().nbytes )