-
Notifications
You must be signed in to change notification settings - Fork 198
Fix issue 791: add LazyFrame sink classes for parquet, csv, ipc, ndjson #1653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ | |
|
|
||
| from hamilton import registry | ||
| from hamilton.io import utils | ||
| from hamilton.io.data_adapters import DataLoader | ||
| from hamilton.io.data_adapters import DataLoader, DataSaver | ||
|
|
||
| DATAFRAME_TYPE = pl.LazyFrame | ||
| COLUMN_TYPE = pl.Expr | ||
|
|
@@ -288,13 +288,96 @@ def load_data(self, type_: type) -> tuple[DATAFRAME_TYPE, dict[str, Any]]: | |
| def name(cls) -> str: | ||
| return "feather" | ||
|
|
||
| @dataclasses.dataclass | ||
| class PolarsLazyFrameSinkParquet(DataSaver): | ||
| """Class to handle sinking a Polars LazyFrame to a Parquet file. | ||
| Should map to https://docs.pola.rs/api/python/stable/reference/lazyframe/api/polars.LazyFrame.sink_parquet.html | ||
| """ | ||
| path: str | Path | ||
|
|
||
| @classmethod | ||
| def applicable_types(cls) -> Collection[type]: | ||
| return [DATAFRAME_TYPE] | ||
|
|
||
| def save_data(self, data: pl.LazyFrame) -> dict[str, Any]: | ||
| data.sink_parquet(self.path) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add the ability to add the lazyframe.sink_parquet kwargs? https://docs.pola.rs/api/python/stable/reference/api/polars.LazyFrame.sink_parquet.html |
||
| return utils.get_file_metadata(self.path) | ||
|
|
||
| @classmethod | ||
| def name(cls) -> str: | ||
| return "parquet" | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class PolarsLazyFrameSinkCSV(DataSaver): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same rename: |
||
| """Class to handle sinking a Polars LazyFrame to a CSV file. | ||
| Should map to https://docs.pola.rs/api/python/stable/reference/lazyframe/api/polars.LazyFrame.sink_csv.html | ||
| """ | ||
| path: str | Path | ||
|
|
||
| @classmethod | ||
| def applicable_types(cls) -> Collection[type]: | ||
| return [DATAFRAME_TYPE] | ||
|
|
||
| def save_data(self, data: pl.LazyFrame) -> dict[str, Any]: | ||
| data.sink_csv(self.path) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return utils.get_file_metadata(self.path) | ||
|
|
||
| @classmethod | ||
| def name(cls) -> str: | ||
| return "csv" | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class PolarsLazyFrameSinkIPC(DataSaver): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same rename style: |
||
| """Class to handle sinking a Polars LazyFrame to an IPC/Feather file. | ||
| Should map to https://docs.pola.rs/api/python/stable/reference/lazyframe/api/polars.LazyFrame.sink_ipc.html | ||
| """ | ||
| path: str | Path | ||
|
|
||
| @classmethod | ||
| def applicable_types(cls) -> Collection[type]: | ||
| return [DATAFRAME_TYPE] | ||
|
|
||
| def save_data(self, data: pl.LazyFrame) -> dict[str, Any]: | ||
| data.sink_ipc(self.path) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here adding kwargs: https://docs.pola.rs/api/python/dev/reference/api/polars.LazyFrame.sink_ipc.html |
||
| return utils.get_file_metadata(self.path) | ||
|
|
||
| @classmethod | ||
| def name(cls) -> str: | ||
| return "ipc" | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class PolarsLazyFrameSinkNDJSON(DataSaver): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same rename: |
||
| """Class to handle sinking a Polars LazyFrame to an NDJSON file. | ||
| Should map to https://docs.pola.rs/api/python/stable/reference/lazyframe/api/polars.LazyFrame.sink_ndjson.html | ||
| """ | ||
| path: str | Path | ||
|
|
||
| @classmethod | ||
| def applicable_types(cls) -> Collection[type]: | ||
| return [DATAFRAME_TYPE] | ||
|
|
||
| def save_data(self, data: pl.LazyFrame) -> dict[str, Any]: | ||
| data.sink_ndjson(self.path) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return utils.get_file_metadata(self.path) | ||
|
|
||
| @classmethod | ||
| def name(cls) -> str: | ||
| return "ndjson" | ||
|
|
||
|
|
||
| def register_data_loaders(): | ||
| """Function to register the data loaders for this extension.""" | ||
| for loader in [ | ||
| PolarsScanCSVReader, | ||
| PolarsScanParquetReader, | ||
| PolarsScanFeatherReader, | ||
| PolarsLazyFrameSinkParquet, | ||
| PolarsLazyFrameSinkCSV, | ||
| PolarsLazyFrameSinkIPC, | ||
| PolarsLazyFrameSinkNDJSON, | ||
| ]: | ||
| registry.register_adapter(loader) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we name this
PolarsSinkParquetWriter?