-
Notifications
You must be signed in to change notification settings - Fork 93
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
feat: Support Arrow PyCapsule Interface for export #786
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
256860e
feat: Support Arrow PyCapsule
MarcoGorelli 9f19a73
fallback to pyarrow
MarcoGorelli c03ac8c
set minimum pyarrow version
MarcoGorelli ec970c6
Merge remote-tracking branch 'upstream/main' into py-capsule
MarcoGorelli b937b5f
fixup
MarcoGorelli 086d45a
correct min version
MarcoGorelli f14f7ea
add to reference
MarcoGorelli c738f7f
fixup
MarcoGorelli 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 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
handler: python | ||
options: | ||
members: | ||
- __arrow_c_stream__ | ||
- __getitem__ | ||
- clone | ||
- collect_schema | ||
|
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 |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
handler: python | ||
options: | ||
members: | ||
- __arrow_c_stream__ | ||
- __getitem__ | ||
- abs | ||
- alias | ||
- all | ||
|
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
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,42 @@ | ||
import polars as pl | ||
import pyarrow as pa | ||
import pyarrow.compute as pc | ||
import pytest | ||
|
||
import narwhals.stable.v1 as nw | ||
from narwhals.utils import parse_version | ||
|
||
|
||
@pytest.mark.skipif( | ||
parse_version(pl.__version__) < (1, 3), reason="too old for pycapsule in Polars" | ||
) | ||
def test_arrow_c_stream_test() -> None: | ||
df = nw.from_native(pl.Series([1, 2, 3]).to_frame("a"), eager_only=True) | ||
result = pa.table(df) | ||
expected = pa.table({"a": [1, 2, 3]}) | ||
assert pc.all(pc.equal(result["a"], expected["a"])).as_py() | ||
|
||
|
||
@pytest.mark.skipif( | ||
parse_version(pl.__version__) < (1, 3), reason="too old for pycapsule in Polars" | ||
) | ||
def test_arrow_c_stream_test_invalid(monkeypatch: pytest.MonkeyPatch) -> None: | ||
# "poison" the dunder method to make sure it actually got called above | ||
monkeypatch.setattr( | ||
"narwhals.dataframe.DataFrame.__arrow_c_stream__", lambda *_: 1 / 0 | ||
) | ||
df = nw.from_native(pl.Series([1, 2, 3]).to_frame("a"), eager_only=True) | ||
with pytest.raises(ZeroDivisionError, match="division by zero"): | ||
pa.table(df) | ||
|
||
|
||
@pytest.mark.skipif( | ||
parse_version(pl.__version__) < (1, 3), reason="too old for pycapsule in Polars" | ||
) | ||
def test_arrow_c_stream_test_fallback(monkeypatch: pytest.MonkeyPatch) -> None: | ||
# Check that fallback to PyArrow works | ||
monkeypatch.delattr("polars.DataFrame.__arrow_c_stream__") | ||
df = nw.from_native(pl.Series([1, 2, 3]).to_frame("a"), eager_only=True) | ||
result = pa.table(df) | ||
expected = pa.table({"a": [1, 2, 3]}) | ||
assert pc.all(pc.equal(result["a"], expected["a"])).as_py() |
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,41 @@ | ||
import polars as pl | ||
import pyarrow as pa | ||
import pyarrow.compute as pc | ||
import pytest | ||
|
||
import narwhals.stable.v1 as nw | ||
from narwhals.utils import parse_version | ||
|
||
|
||
@pytest.mark.skipif( | ||
parse_version(pl.__version__) < (1, 3), reason="too old for pycapsule in Polars" | ||
) | ||
def test_arrow_c_stream_test() -> None: | ||
s = nw.from_native(pl.Series([1, 2, 3]), series_only=True) | ||
result = pa.chunked_array(s) | ||
expected = pa.chunked_array([[1, 2, 3]]) | ||
assert pc.all(pc.equal(result, expected)).as_py() | ||
|
||
|
||
@pytest.mark.skipif( | ||
parse_version(pl.__version__) < (1, 3), reason="too old for pycapsule in Polars" | ||
) | ||
def test_arrow_c_stream_test_invalid(monkeypatch: pytest.MonkeyPatch) -> None: | ||
# "poison" the dunder method to make sure it actually got called above | ||
monkeypatch.setattr("narwhals.series.Series.__arrow_c_stream__", lambda *_: 1 / 0) | ||
s = nw.from_native(pl.Series([1, 2, 3]), series_only=True) | ||
with pytest.raises(ZeroDivisionError, match="division by zero"): | ||
pa.chunked_array(s) | ||
|
||
|
||
@pytest.mark.skipif( | ||
parse_version(pl.__version__) < (1, 3), reason="too old for pycapsule in Polars" | ||
) | ||
def test_arrow_c_stream_test_fallback(monkeypatch: pytest.MonkeyPatch) -> None: | ||
# Check that fallback to PyArrow works | ||
monkeypatch.delattr("polars.Series.__arrow_c_stream__") | ||
s = nw.from_native(pl.Series([1, 2, 3]).to_frame("a"), eager_only=True)["a"] | ||
s.__arrow_c_stream__() | ||
result = pa.chunked_array(s) | ||
expected = pa.chunked_array([[1, 2, 3]]) | ||
assert pc.all(pc.equal(result, expected)).as_py() |
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.
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.
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.
I think this might require pyarrow 15
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.
in pandas the requirement is PyArrow 14+ (I also just ran the tests with pyarrow 13 and 14 - the former fails, the latter passes)
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.
ah sorry, that's for DataFrame. looks like it's even PyArrow 16+ for chunkedarray?
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.
It was added to pa.chunked_array in a later release, yes. I think it was here: apache/arrow#40818