为了账号安全,请及时绑定邮箱和手机立即绑定

在 Python 中读取 parquet 字节对象

在 Python 中读取 parquet 字节对象

墨色风雨 2022-06-02 16:17:07
我有一个 python 对象,我知道这是一个加载到对象的镶木地板文件。(我没有可能从文件中实际读取它)。该对象var_1包含b'PAR1\x15\x....1\x00PAR1当我检查类型时:type(var_1)我得到的结果是bytes。有没有办法阅读这个?说成熊猫数据框?我试过: 1)from fastparquet import ParquetFilepf = ParquetFile(var_1)并得到:TypeError: a bytes-like object is required, not 'str'2import pyarrow.parquet as pqdataset = pq.ParquetDataset(var_1)并得到:TypeError: not a path-like object请注意,如何将 Parquet 文件读入 Pandas DataFrame 的解决方案?. 即pd.read_parquet(var_1, engine='fastparquet')导致TypeError: a bytes-like object is required, not 'str'Python熊猫镶木地板
查看完整描述

2 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

这是用 Pandas 1.2.3 测试的


要将parquet对象读取bytes为 Pandas 数据框:


import io


import pandas as pd


pq_bytes = b'PAR1\x15\x02\x19\x1c5\x00\x18\x06schema\x15\x00\x00\x16\x00\x19\x1c\x19\x0c\x16\x00\x16\x00&\x00\x16\x00\x14\x00\x00\x19,\x18\x06pandas\x18\x8c\x01{"index_columns": [], "column_indexes": [], "columns": [], "creator": {"library": "pyarrow", "version": "1.0.1"}, "pandas_version": "1.1.3"}\x00\x18\x0cARROW:schema\x18\xd8\x02//////gAAAAQAAAAAAAKAA4ABgAFAAgACgAAAAABBAAQAAAAAAAKAAwAAAAEAAgACgAAAMQAAAAEAAAAAQAAAAwAAAAIAAwABAAIAAgAAACcAAAABAAAAIwAAAB7ImluZGV4X2NvbHVtbnMiOiBbXSwgImNvbHVtbl9pbmRleGVzIjogW10sICJjb2x1bW5zIjogW10sICJjcmVhdG9yIjogeyJsaWJyYXJ5IjogInB5YXJyb3ciLCAidmVyc2lvbiI6ICIxLjAuMSJ9LCAicGFuZGFzX3ZlcnNpb24iOiAiMS4xLjMifQAAAAAGAAAAcGFuZGFzAAAAAAAAAAAAAA==\x00\x18"parquet-cpp version 1.5.1-SNAPSHOT\x19\x0c\x00M\x02\x00\x00PAR1'

pq_file = io.BytesIO(pq_bytes)

df = pd.read_parquet(pq_file)

要将Pandas 数据框写入bytes对象:


import pandas as pd


df = pd.DataFrame()

df.to_parquet()

b'PAR1\x15\x04\x15\x00\x15\x02L\x15\x00\x15\x04\x12\x00\x00\x00&&\x1c\x15\x02\x195\x04\x00\x06\x19\x18\x11__index_level_0__\x15\x02\x16\x00\x16\x1c\x16\x1e&\x00&\x08)\x1c\x15\x04\x15\x04\x15\x02\x00\x00\x00\x15\x02\x19,5\x00\x18\x06schema\x15\x02\x00\x15\x02%\x02\x18\x11__index_level_0__l\xbc\x00\x00\x00\x16\x00\x19\x1c\x19\x1c&&\x1c\x15\x02\x195\x04\x00\x06\x19\x18\x11__index_level_0__\x15\x02\x16\x00\x16\x1c\x16\x1e&\x00&\x08)\x1c\x15\x04\x15\x04\x15\x02\x00\x00\x00\x16\x1e\x16\x00&&\x16\x1e\x14\x00\x00\x19,\x18\x06pandas\x18\xf6\x02{"index_columns": ["__index_level_0__"], "column_indexes": [{"name": null, "field_name": null, "pandas_type": "empty", "numpy_type": "object", "metadata": null}], "columns": [{"name": null, "field_name": "__index_level_0__", "pandas_type": "empty", "numpy_type": "object", "metadata": null}], "creator": {"library": "pyarrow", "version": "3.0.0"}, "pandas_version": "1.2.3"}\x00\x18\x0cARROW:schema\x18\xec\x05/////ygCAAAQAAAAAAAKAA4ABgAFAAgACgAAAAABBAAQAAAAAAAKAAwAAAAEAAgACgAAAKwBAAAEAAAAAQAAAAwAAAAIAAwABAAIAAgAAACEAQAABAAAAHYBAAB7ImluZGV4X2NvbHVtbnMiOiBbIl9faW5kZXhfbGV2ZWxfMF9fIl0sICJjb2x1bW5faW5kZXhlcyI6IFt7Im5hbWUiOiBudWxsLCAiZmllbGRfbmFtZSI6IG51bGwsICJwYW5kYXNfdHlwZSI6ICJlbXB0eSIsICJudW1weV90eXBlIjogIm9iamVjdCIsICJtZXRhZGF0YSI6IG51bGx9XSwgImNvbHVtbnMiOiBbeyJuYW1lIjogbnVsbCwgImZpZWxkX25hbWUiOiAiX19pbmRleF9sZXZlbF8wX18iLCAicGFuZGFzX3R5cGUiOiAiZW1wdHkiLCAibnVtcHlfdHlwZSI6ICJvYmplY3QiLCAibWV0YWRhdGEiOiBudWxsfV0sICJjcmVhdG9yIjogeyJsaWJyYXJ5IjogInB5YXJyb3ciLCAidmVyc2lvbiI6ICIzLjAuMCJ9LCAicGFuZGFzX3ZlcnNpb24iOiAiMS4yLjMifQAABgAAAHBhbmRhcwAAAQAAABQAAAAQABQACAAGAAcADAAAABAAEAAAAAAAAQEQAAAAKAAAAAQAAAAAAAAAEQAAAF9faW5kZXhfbGV2ZWxfMF9fAAAABAAEAAQAAAA=\x00\x18"parquet-cpp version 1.5.1-SNAPSHOT\x19\x1c\x1c\x00\x00\x00\x1f\x05\x00\x00PAR1'



查看完整回答
反对 回复 2022-06-02
?
函数式编程

TA贡献1807条经验 获得超9个赞

您可以通过将bytes对象包装在pyarrow.BufferReader.


import pyarrow as pa

import pyarrow.parquet as pq


var_1 = …    

reader = pa.BufferReader(var_1)

table = pq.read_table(reader)

df = table.to_pandas()  # This results in a pandas.DataFrame


查看完整回答
反对 回复 2022-06-02
  • 2 回答
  • 0 关注
  • 625 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信