polars.DataFrame.serialize#

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

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()
>>> bytes  
b'\xa1gcolumns\x82\xa4dnamecfoohdatatypeeInt64lbit_settings\x00fvalues\x83...'

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   │
└─────┴─────┘