0

Goal: Get the Bytes of df.to_parquet() for upload.

I attempted:

import pandas as pd
import io

df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
                   'B': [6, 7, 8, 9, 10]})

bytes = io.BytesIO()
buffer = self.data.to_parquet(bytes)
bytes.seek(0)
buffer = bytes.read()

print("--BYTES--")
print(bytes)
print("--BUFFER--")
print(buffer)

Bytes and Buffer:

--BYTES--
<_io.BytesIO object at 0x7f7a77f5f9a0>
--BUFFER--
b'PAR1\x15\x04\x15 \x15$L\x15\x04\x15\x04\x12\x00\x00\x10<\xc3\xc35\x00\x00\x00\x00\x00\xc7\xe45\x00\x00\x00\x00\x00\x15\x00\x15\x12\x15\x16,\x15\x04\x15\x04\x15\x06\x15\x06\x1c\x18\x08\xc7\xe45\x00\x00\x00\x00\x00\x18\x08\xc3\xc35\x00\x00\x00\x00\x00\x16\x00(\x08\xc7\xe45\x00\x00\x00\x00\x00\x18\x08\xc3\xc35\x00\x00\x00\x00\x00\x00\x00\x00\t \x02\x00\x00\x00\x04\x01\x01\x03\x02&\xd8\x01\x1c\x15\x04\x195\x04\x00\x06\x19\x18\x02id\x15\x02\x16\x04\x16\xc8\x01\x16\xd0\x01&H&\x08\x1c\x18\x08\xc7\xe45\x00\x00\x00\x00\x00\x18\x08\xc3\xc35\x00\x00\x00\x00\x00\x16\x00(\x08\xc7\xe45\x00\x00\x00\x00\x00\x18\x08\xc3\xc35\x00\x00\x00\x00\x00\x00\x19,\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00\x15\x04\x15~\x15\x80\x01L\x15\x04\x15\x04\x12\x00\x00?\xa8\x1e\x00\x00\x00Parent has physical disability\x19\x00\x00\x00Cervi\x01\x17<smear action NOS\x15\x00\x15\x12\x15\x16,\x15\x04\x15\x04\x15\x06\x15\x06\x1c6\x00(\x1eParent has physical disability\x18\x19Cervical smear action NOS\x00\x00\x00\t \x02\x00\x00\x00\x04\x01\x01\x03\x02&\xe4\x05\x1c\x15\x0c\x195\x04\x00\x06\x19\x18\rvocabulary_id\x15\x02\x16\x04\x16\xce\x02\x16\xd4\x02&\xae\x04&\x90\x03\x1c6\x00(\x1eParent has physical disability\x18\x19Cervical smear action NOS\x00\x19,\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00\x15\x04\x15\x1e\x15"L\x15\x02\x15\x04\x12\x00\x00\x0f8\x0b\x00\x00\x00Observation\x15\x00\x15\x12\x15\x16,\x15\x04\x15\x04\x15\x06\x15\x06\x1c6\x00(\x0bObservation\x18\x0bObservation\x00\x00\x00\t \x02\x00\x00\x00\x04\x01\x01\x04\x00&\x8e\t\x1c\x15\x0c\x195\x04\x00\x06\x19\x18\x0cconcept_code\x15\x02\x16\x04\x16\xaa\x01\x16\xb2\x01&\x9a\x08&\xdc\x07\x1c6\x00(\x0bObservation\x18\x0bObservation\x00\x19,\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00\x15\x04\x15\x00\x15\x02L\x15\x00\x15\x04\x12\x00\x00\x00\x15\x00\x15\x0e\x15\x12,\x15\x04\x15\x04\x15\x06\x15\x06\x1c\x00\x00\x00\x07\x18\x02\x00\x00\x00\x04\x00\x00&\x98\x0b\x1c\x15\x02\x195\x04\x00\x06\x19\x18\x0cconcept_text\x15\x02\x16\x04\x16P\x16V&\xe0\n&\xc2\n),\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00\x15\x04\x15 \x15$L\x15\x04\x15\x04\x12\x00\x00\x10<\x0cs\x06\x00\x00\x00\x00\x002\x93\x0f\x00\x00\x00\x00\x00\x15\x00\x15\x12\x15\x16,\x15\x04\x15\x04\x15\x06\x15\x06\x1c\x18\x082\x93\x0f\x00\x00\x00\x00\x00\x18\x08\x0cs\x06\x00\x00\x00\x00\x00\x16\x00(\x082\x93\x0f\x00\x00\x00\x00\x00\x18\x08\x0cs\x06\x00\x00\x00\x00\x00\x00\x00\x00\t \x02\x00\x00\x00\x04\x01\x01\x03\x02&\xdc\r\x1c\x15\x04\x195\x04\x00\x06\x19\x18\x11__index_level_0__\x15\x02\x16\x04\x16\xc8\x01\x16\xd0\x01&\xcc\x0c&\x8c\x0c\x1c\x18\x082\x93\x0f\x00\x00\x00\x00\x00\x18\x08\x0cs\x06\x00\x00\x00\x00\x00\x16\x00(\x082\x93\x0f\x00\x00\x00\x00\x00\x18\x08\x0cs\x06\x00\x00\x00\x00\x00\x00\x19,\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00\x15\x02\x19l5\x00\x18\x06schema\x15\n\x00\x15\x04%\x02\x18\x02id\x00\x15\x0c%\x02\x18\rvocabulary_id%\x00L\x1c\x00\x00\x00\x15\x0c%\x02\x18\x0cconcept_code%\x00L\x1c\x00\x00\x00\x15\x02%\x02\x18\x0cconcept_textl\xbc\x00\x00\x00\x15\x04%\x02\x18\x11__index_level_0__\x00\x16\x04\x19\x1c\x19\\&\xd8\x01\x1c\x15\x04\x195\x04\x00\x06\x19\x18\x02id\x15\x02\x16\x04\x16\xc8\x01\x16\xd0\x01&H&\x08\x1c\x18\x08\xc7\xe45\x00\x00\x00\x00\x00\x18\x08\xc3\xc35\x00\x00\x00\x00\x00\x16\x00(\x08\xc7\xe45\x00\x00\x00\x00\x00\x18\x08\xc3\xc35\x00\x00\x00\x00\x00\x00\x19,\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00&\xe4\x05\x1c\x15\x0c\x195\x04\x00\x06\x19\x18\rvocabulary_id\x15\x02\x16\x04\x16\xce\x02\x16\xd4\x02&\xae\x04&\x90\x03\x1c6\x00(\x1eParent has physical disability\x18\x19Cervical smear action NOS\x00\x19,\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00&\x8e\t\x1c\x15\x0c\x195\x04\x00\x06\x19\x18\x0cconcept_code\x15\x02\x16\x04\x16\xaa\x01\x16\xb2\x01&\x9a\x08&\xdc\x07\x1c6\x00(\x0bObservation\x18\x0bObservation\x00\x19,\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00&\x98\x0b\x1c\x15\x02\x195\x04\x00\x06\x19\x18\x0cconcept_text\x15\x02\x16\x04\x16P\x16V&\xe0\n&\xc2\n),\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00&\xdc\r\x1c\x15\x04\x195\x04\x00\x06\x19\x18\x11__index_level_0__\x15\x02\x16\x04\x16\xc8\x01\x16\xd0\x01&\xcc\x0c&\x8c\x0c\x1c\x18\x082\x93\x0f\x00\x00\x00\x00\x00\x18\x08\x0cs\x06\x00\x00\x00\x00\x00\x16\x00(\x082\x93\x0f\x00\x00\x00\x00\x00\x18\x08\x0cs\x06\x00\x00\x00\x00\x00\x00\x19,\x15\x04\x15\x04\x15\x02\x00\x15\x00\x15\x04\x15\x02\x00\x00\x00\x16\xd8\x07\x16\x04&\x08\x16\xfc\x07\x14\x00\x00\x19,\x18\x06pandas\x18\xe2\x06{"index_columns": ["__index_level_0__"], "column_indexes": [{"name": null, "field_name": null, "pandas_type": "unicode", "numpy_type": "object", "metadata": {"encoding": "UTF-8"}}], "columns": [{"name": "id", "field_name": "id", "pandas_type": "int64", "numpy_type": "object", "metadata": null}, {"name": "vocabulary_id", "field_name": "vocabulary_id", "pandas_type": "unicode", "numpy_type": "object", "metadata": null}, {"name": "concept_code", "field_name": "concept_code", "pandas_type": "unicode", "numpy_type": "object", "metadata": null}, {"name": "concept_text", "field_name": "concept_text", "pandas_type": "empty", "numpy_type": "object", "metadata": null}, {"name": null, "field_name": "__index_level_0__", "pandas_type": "int64", "numpy_type": "int64", "metadata": null}], "creator": {"library": "pyarrow", "version": "6.0.1"}, "pandas_version": "1.1.4"}\x00\x18\x0cARROW:schema\x18\xa0\r//////AEAAAQAAAAAAAKAA4ABgAFAAgACgAAAAABBAAQAAAAAAAKAAwAAAAEAAgACgAA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RhdGEiOiBudWxsfV0sICJjcmVhdG9yIjogeyJsaWJyYXJ5IjogInB5YXJyb3ciLCAidmVyc2lvbiI6ICI2LjAuMSJ9LCAicGFuZGFzX3ZlcnNpb24iOiAiMS4xLjQifQAABQAAAPQAAACsAAAAeAAAAEQAAAAEAAAAMP///wAAAQIQAAAAJAAAAAQAAAAAAAAAEQAAAF9faW5kZXhfbGV2ZWxfMF9fAAAAMP///wAAAAFAAAAAbP///wAAAQEQAAAAIAAAAAQAAAAAAAAADAAAAGNvbmNlcHRfdGV4dAAAAACg////nP///wAAAQUQAAAAIAAAAAQAAAAAAAAADAAAAGNvbmNlcHRfY29kZQAAAADQ////zP///wAAAQUQAAAAJAAAAAQAAAAAAAAADQAAAHZvY2FidWxhcnlfaWQAAAAEAAQABAAAABAAFAAIAAYABwAMAAAAEAAQAAAAAAABAhAAAAAcAAAABAAAAAAAAAACAAAAaWQAAAgADAAIAAcACAAAAAAAAAFAAAAA\x00\x18\x1fparquet-cpp-arrow version 6.0.1\x19\\\x1c\x00\x00\x1c\x00\x00\x1c\x00\x00\x1c\x00\x00\x1c\x00\x00\x00\xbd\x0c\x00\x00PAR1'

Traceback:

TypeError: object of type '_io.BytesIO' has no len()

A closed Git Issue references a requests for this feature but it has not been updated.

How do I get Bytes of a parquet file? Preferably, without storing to local disk.

DanielBell99
  • 896
  • 5
  • 25
  • 57
  • Does this answer your question? [Encode .parquet into io.Bytes](https://stackoverflow.com/questions/71412256/encode-parquet-into-io-bytes) – 0x26res Apr 21 '22 at 14:40
  • Lmao, that's an old post of mine... It seems my above attempt is based on that solution, but the solution is a misuse of the function. As detailed in a very old Git Issue – DanielBell99 Apr 21 '22 at 14:41
  • `buffer` and `bytes` = `None` ;( – DanielBell99 Apr 21 '22 at 14:50

1 Answers1

2

Looking at the examples in the documentation for to_parquet:

import io
f = io.BytesIO()
df.to_parquet(f)
f.seek(0)
content = f.read()

content contains the bytes of the parquet file.

0x26res
  • 11,925
  • 11
  • 54
  • 108
  • I keep getting `Bytes` and `Buffer` muddled up. It seems I need both in order to upload to MinIO. Whichever your solution gets; I need the other. I've successfully done this for `.csv`, `.txt` and `.json` file types. – DanielBell99 Apr 22 '22 at 14:42