2 回答
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'
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
添加回答
举报