polars.Expr.meta.serialize#

Expr.meta.serialize(file: IOBase | str | Path | None = None) str | None[source]#

Serialize this expression 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.

See also

Expr.deserialize

Examples

Serialize the expression into a JSON string.

>>> expr = pl.col("foo").sum().over("bar")
>>> json = expr.meta.serialize()
>>> json
'{"Window":{"function":{"Agg":{"Sum":{"Column":"foo"}}},"partition_by":[{"Column":"bar"}],"options":{"Over":"GroupsToRows"}}}'

The expression can later be deserialized back into an Expr object.

>>> from io import StringIO
>>> pl.Expr.deserialize(StringIO(json))  
<Expr ['col("foo").sum().over([col("ba…'] at ...>