-
Notifications
You must be signed in to change notification settings - Fork 0
Add option for lazily evaluating data #491
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
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7112e3d
Add option for lazily evaluating data
mallport dea81f8
fixup
mallport 6c08b08
Add README example
mallport 4287a50
make mypy happy
mallport 989e046
bumper
mallport 3c14995
incorporate improvements
mallport 377f280
fix tests
mallport 9ee37b3
fixer
mallport 08319c3
Remove ugly env methods
mallport a470a04
Actually test lazyframe vs dataframe
mallport 4620bfe
fix names and add docstrings
mallport 8ae410f
Upgrade to 100 rows
mallport File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| from dapla_pseudo.utils import get_file_format_from_file_name | ||
| from dapla_pseudo.v1.models.api import PseudoFieldResponse | ||
| from dapla_pseudo.v1.supported_file_format import write_from_df | ||
| from dapla_pseudo.v1.supported_file_format import write_from_lazy_df | ||
|
|
||
|
|
||
| class Result: | ||
|
|
@@ -32,7 +33,7 @@ def __init__( | |
| schema: pd.Series | pl.Schema | None = None, | ||
| ) -> None: | ||
| """Initialise a PseudonymizationResult.""" | ||
| self._pseudo_data: pl.DataFrame = pseudo_response.data | ||
| self._pseudo_data: pl.DataFrame | pl.LazyFrame = pseudo_response.data | ||
| self._metadata: dict[str, dict[str, list[Any]]] = {} | ||
| self._datadoc: Datadoc | list[Variable] | ||
| self._schema = schema | ||
|
|
@@ -122,6 +123,11 @@ def to_polars(self, **kwargs: Any) -> pl.DataFrame: | |
| if "__index_level_0__" in df.columns: | ||
| df = df.drop("__index_level_0__") | ||
| return df | ||
| case pl.LazyFrame() as ldf: | ||
| df = ldf.collect() | ||
| if "__index_level_0__" in df.columns: | ||
| df = df.drop("__index_level_0__") | ||
| return df | ||
| case _ as invalid_pseudo_data: | ||
| raise ValueError(f"Invalid file type: {type(invalid_pseudo_data)}") | ||
|
|
||
|
|
@@ -146,6 +152,14 @@ def to_pandas(self, **kwargs: Any) -> pd.DataFrame: | |
| ): # Apply original schema if available | ||
| pandas_df = pandas_df.astype(self._schema) | ||
|
|
||
| return pandas_df | ||
| case pl.LazyFrame() as ldf: | ||
| pandas_df = ldf.collect().to_pandas() | ||
| if isinstance( | ||
| self._schema, pd.Series | ||
| ): # Apply original schema if available | ||
| pandas_df = pandas_df.astype(self._schema) | ||
|
|
||
| return pandas_df | ||
| case _ as invalid_pseudo_data: | ||
| raise ValueError(f"Invalid response type: {type(invalid_pseudo_data)}") | ||
|
|
@@ -169,26 +183,24 @@ def to_file(self, file_path: str, **kwargs: Any) -> None: | |
| datadoc_file_path: Path | GSPath | ||
| if file_path.startswith(GSPath.cloud_prefix): | ||
| client = GSClient() | ||
| gs_path = GSPath(cloud_path=file_path, client=client) | ||
| cloud_path = GSPath(cloud_path=file_path, client=client) | ||
|
|
||
| file_handle = gs_path.open(mode="wb") | ||
|
Contributor
Author
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. After we upgraded Polars, we no longer have to use file handles for writing to GCS, we can just supply the "gs://"-filepath and Polars automatically infers the authentication from the environment |
||
|
|
||
| datadoc_file_path = gs_path.parent.joinpath(Path(datadoc_file_name)) | ||
| datadoc_file_path = cloud_path.parent.joinpath(Path(datadoc_file_name)) | ||
| datadoc_file_handle = datadoc_file_path.open(mode="w") | ||
| else: | ||
| file_handle = Path(file_path).open(mode="wb") | ||
|
|
||
| datadoc_file_path = Path(file_path).parent.joinpath(Path(datadoc_file_name)) | ||
| datadoc_file_handle = datadoc_file_path.open(mode="w") | ||
|
|
||
| match self._pseudo_data: | ||
| case pl.DataFrame() as df: | ||
| write_from_df(df, file_format, file_handle, **kwargs) | ||
| write_from_df(df, file_format, file_path, **kwargs) | ||
| datadoc_file_handle.write(self.datadoc) | ||
| case pl.LazyFrame() as ldf: | ||
| write_from_lazy_df(ldf, file_format, file_path, **kwargs) | ||
| datadoc_file_handle.write(self.datadoc) | ||
| case _ as invalid_pseudo_data: | ||
| raise ValueError(f"Invalid response type: {type(invalid_pseudo_data)}") | ||
|
|
||
| file_handle.close() | ||
| datadoc_file_handle.close() | ||
|
|
||
| @property | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.