polars.DataFrame.serialize
-
Serialize this DataFrame to a file or string in JSON format.
- Parameters:
-
- file
-
File path or writable file-like object to which the result will be written. If set to
None(default), the output is returned as a string instead. - format
-
The format in which to serialize. Options:
-
"binary": Serialize to binary format (bytes). This is the default. -
"json": Serialize to JSON format (string).
-
Notes
Serialization is not stable across Polars versions: a LazyFrame serialized in one Polars version may not be deserializable in another Polars version.
Examples
Serialize the DataFrame into a binary representation.
>>> df = pl.DataFrame( ... { ... "foo": [1, 2, 3], ... "bar": [6, 7, 8], ... } ... ) >>> bytes = df.serialize() >>> type(bytes) <class 'bytes'>The bytes can later be deserialized back into a DataFrame.
>>> import io >>> pl.DataFrame.deserialize(io.BytesIO(bytes)) shape: (3, 2) ┌─────┬─────┐ │ foo ┆ bar │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 1 ┆ 6 │ │ 2 ┆ 7 │ │ 3 ┆ 8 │ └─────┴─────┘
DataFrame.serialize(
file: IOBase | str | Path | None = None,
*,
format: SerializationFormat = 'binary',
) → bytes | str | None
© 2020 Ritchie Vink
© 2022 Polars contributors
Licensed under the MIT License.
https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.serialize.html