polars.DataFrame.write_ndjson#

DataFrame.write_ndjson(
file: str | Path | IO[bytes] | IO[str] | None = None,
*,
compression: Literal['uncompressed', 'gzip', 'zstd'] = 'uncompressed',
compression_level: int | None = None,
check_extension: bool = True,
) str | None[source]#

Serialize to newline delimited JSON representation.

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.

compression

What compression format to use.

Warning

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

compression_level

The compression level to use, typically 0-9 or None to let the engine choose.

Warning

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

check_extension

Whether to check if the filename matches the compression settings. Will raise an error if compression is set to ‘uncompressed’ and the filename ends in one of (“.gz”, “.zst”, “.zstd”) or if compression != ‘uncompressed’ and the file uses an mismatched extension. Only applies if file is a path.

Warning

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Examples

>>> df = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6, 7, 8],
...     }
... )
>>> df.write_ndjson()
'{"foo":1,"bar":6}\n{"foo":2,"bar":7}\n{"foo":3,"bar":8}\n'