-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python)!: Implement binary serialization of LazyFrame/DataFrame/…
…Expr and set it as the default format (#17223)
- Loading branch information
Showing
20 changed files
with
698 additions
and
358 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Utility for serializing Polars objects.""" | ||
|
||
from __future__ import annotations | ||
|
||
from io import BytesIO, StringIO | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, Callable, Literal, overload | ||
|
||
from polars._utils.various import normalize_filepath | ||
|
||
if TYPE_CHECKING: | ||
from io import IOBase | ||
|
||
from polars.type_aliases import SerializationFormat | ||
|
||
|
||
@overload | ||
def serialize_polars_object( | ||
serializer: Callable[[IOBase | str], None], file: None, format: Literal["binary"] | ||
) -> bytes: ... | ||
@overload | ||
def serialize_polars_object( | ||
serializer: Callable[[IOBase | str], None], file: None, format: Literal["json"] | ||
) -> str: ... | ||
@overload | ||
def serialize_polars_object( | ||
serializer: Callable[[IOBase | str], None], | ||
file: IOBase | str | Path, | ||
format: SerializationFormat, | ||
) -> None: ... | ||
|
||
|
||
def serialize_polars_object( | ||
serializer: Callable[[IOBase | str], None], | ||
file: IOBase | str | Path | None, | ||
format: SerializationFormat, | ||
) -> bytes | str | None: | ||
"""Serialize a Polars object (DataFrame/LazyFrame/Expr).""" | ||
|
||
def serialize_to_bytes() -> bytes: | ||
with BytesIO() as buf: | ||
serializer(buf) | ||
serialized = buf.getvalue() | ||
return serialized | ||
|
||
if file is None: | ||
serialized = serialize_to_bytes() | ||
return serialized.decode() if format == "json" else serialized | ||
elif isinstance(file, StringIO): | ||
serialized_str = serialize_to_bytes().decode() | ||
file.write(serialized_str) | ||
return None | ||
elif isinstance(file, BytesIO): | ||
serialized = serialize_to_bytes() | ||
file.write(serialized) | ||
return None | ||
elif isinstance(file, (str, Path)): | ||
file = normalize_filepath(file) | ||
serializer(file) | ||
return None | ||
else: | ||
serializer(file) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.