-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add polars compatibility #6531
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
Merged
Add polars compatibility #6531
Changes from 21 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
83d1fbb
Add Polars support for data formatting and conversion
fec2bb3
Update Polars availability check in config.py
622e54e
Merge branch 'add-polars-compatibility' of github.com:psmyth94/datase…
82b9b7c
Merge branch 'add-polars-compatibility' of github.com:psmyth94/datase…
aae3f5a
Merge branch 'add-polars-compatibility' of github.com:psmyth94/datase…
7374e99
added to_polars
408b9d6
changed the logic of importing polars if not already called
39a5c56
Remove to and from_polars from table.py in order to maintain pa.table…
3aa7081
Merge branch 'main' into add-polars-compatibility
psmyth94 2f10384
fix unused import
a623f51
Merge branch 'add-polars-compatibility' of github.com:psmyth94/datase…
12fef57
fixed code formatting with ruff
a57fcbe
fix formatting issues with ruff
912c437
fix formatting issues using ruff
ce7c3c5
add tests for polars formatting
1b28d85
removed using InMemoryTable classmethod to convert polars to Table
eb4d7ce
added test for polars conversion
417f9ad
added missing ruff fixes
19e5d80
add polars in test dependencies
7c835a4
Fixed not executing default write method due to nested polars check.
d0582f9
Merge branch 'main' into add-polars-compatibility
psmyth94 fa51fd2
Update src/datasets/arrow_dataset.py
psmyth94 d09839c
Update src/datasets/arrow_dataset.py
psmyth94 1c6d2a5
Fix Polars DataFrame conversion bug
40614ee
Merge branch 'add-polars-compatibility' of github.com:psmyth94/datase…
7d6224b
Fix DataFrame conversion in arrow_dataset.py
a301eb3
Fix variable name in arrow_dataset.py
d062b57
Fix write_table to write_row in Dataset class
1dbdc80
fix formatting with ruff
1b9e450
Update polars dependency to include timezone support
23329d5
Remove polars in EXTRAS_REQUIRE
f4361cc
Replace deprecated method
53f471a
perform cleanup after use
52fd448
Merge branch 'main' into add-polars-compatibility
psmyth94 b898b57
remove unused import
4bacf3b
Add garbage collection to test_to_polars method
358b2cb
Remove unused import and unnecessary code in test_to_polars method
c00efad
Add additional args for to_polars method
ddaab5b
Fixed unclosed links to dataset file
a87998c
ruff cleanup
d1acc92
even ruffier cleanup
7ee3fdf
changed hash to reflect new SHA for ref/convert/parquet
HuggingFaceDocBuilder 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Copyright 2020 The HuggingFace Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import sys | ||
from collections.abc import Mapping | ||
from functools import partial | ||
from typing import TYPE_CHECKING, Optional | ||
|
||
import pyarrow as pa | ||
|
||
from .. import config | ||
from ..features import Features | ||
from ..features.features import decode_nested_example | ||
from ..utils.py_utils import no_op_if_value_is_null | ||
from .formatting import BaseArrowExtractor, TensorFormatter | ||
|
||
|
||
if TYPE_CHECKING: | ||
import polars as pl | ||
|
||
|
||
class PolarsArrowExtractor(BaseArrowExtractor["pl.DataFrame", "pl.Series", "pl.DataFrame"]): | ||
def extract_row(self, pa_table: pa.Table) -> "pl.DataFrame": | ||
if config.POLARS_AVAILABLE: | ||
if "polars" not in sys.modules: | ||
import polars | ||
else: | ||
polars = sys.modules["polars"] | ||
|
||
return polars.from_arrow(pa_table.slice(length=1)) | ||
else: | ||
raise ValueError("Polars needs to be installed to be able to return Polars dataframes.") | ||
|
||
def extract_column(self, pa_table: pa.Table) -> "pl.Series": | ||
if config.POLARS_AVAILABLE: | ||
if "polars" not in sys.modules: | ||
import polars | ||
else: | ||
polars = sys.modules["polars"] | ||
|
||
return polars.from_arrow(pa_table.select([0]))[pa_table.column_names[0]] | ||
else: | ||
raise ValueError("Polars needs to be installed to be able to return Polars dataframes.") | ||
|
||
def extract_batch(self, pa_table: pa.Table) -> "pl.DataFrame": | ||
if config.POLARS_AVAILABLE: | ||
if "polars" not in sys.modules: | ||
import polars | ||
else: | ||
polars = sys.modules["polars"] | ||
|
||
return polars.from_arrow(pa_table) | ||
else: | ||
raise ValueError("Polars needs to be installed to be able to return Polars dataframes.") | ||
|
||
|
||
class PolarsFeaturesDecoder: | ||
def __init__(self, features: Optional[Features]): | ||
self.features = features | ||
import polars as pl # noqa: F401 - import pl at initialization | ||
|
||
def decode_row(self, row: "pl.DataFrame") -> "pl.DataFrame": | ||
decode = ( | ||
{ | ||
column_name: no_op_if_value_is_null(partial(decode_nested_example, feature)) | ||
for column_name, feature in self.features.items() | ||
if self.features._column_requires_decoding[column_name] | ||
} | ||
if self.features | ||
else {} | ||
) | ||
if decode: | ||
row[list(decode.keys())] = row.map_rows(decode) | ||
return row | ||
|
||
def decode_column(self, column: "pl.Series", column_name: str) -> "pl.Series": | ||
decode = ( | ||
no_op_if_value_is_null(partial(decode_nested_example, self.features[column_name])) | ||
if self.features and column_name in self.features and self.features._column_requires_decoding[column_name] | ||
else None | ||
) | ||
if decode: | ||
column = column.map_elements(decode) | ||
return column | ||
|
||
def decode_batch(self, batch: "pl.DataFrame") -> "pl.DataFrame": | ||
return self.decode_row(batch) | ||
|
||
|
||
class PolarsFormatter(TensorFormatter[Mapping, "pl.DataFrame", Mapping]): | ||
def __init__(self, features=None, **np_array_kwargs): | ||
super().__init__(features=features) | ||
self.np_array_kwargs = np_array_kwargs | ||
self.polars_arrow_extractor = PolarsArrowExtractor | ||
self.polars_features_decoder = PolarsFeaturesDecoder(features) | ||
import polars as pl # noqa: F401 - import pl at initialization | ||
|
||
def format_row(self, pa_table: pa.Table) -> "pl.DataFrame": | ||
row = self.polars_arrow_extractor().extract_row(pa_table) | ||
row = self.polars_features_decoder.decode_row(row) | ||
return row | ||
|
||
def format_column(self, pa_table: pa.Table) -> "pl.Series": | ||
column = self.polars_arrow_extractor().extract_column(pa_table) | ||
column = self.polars_features_decoder.decode_column(column, pa_table.column_names[0]) | ||
return column | ||
|
||
def format_batch(self, pa_table: pa.Table) -> "pl.DataFrame": | ||
row = self.polars_arrow_extractor().extract_batch(pa_table) | ||
row = self.polars_features_decoder.decode_batch(row) | ||
return row |
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.