polars.LazyFrame.serialize#

LazyFrame.serialize(
file: IOBase | str | Path | None = None,
*,
format: SerializationFormat = 'binary',
) bytes | str | None[source]#

Serialize the logical plan of this LazyFrame to a file or string in JSON format.

Parameters:
file

File path to which the result should 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) (deprecated).

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 logical plan into a binary representation.

>>> lf = pl.LazyFrame({"a": [1, 2, 3]}).sum()
>>> bytes = lf.serialize()

The bytes can later be deserialized back into a LazyFrame.

>>> import io
>>> pl.LazyFrame.deserialize(io.BytesIO(bytes)).collect()
shape: (1, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 6   │
└─────┘