-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support Arrow PyCapsule Interface for export (#786)
- Loading branch information
1 parent
6075ec7
commit 350fe7d
Showing
7 changed files
with
142 additions
and
3 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
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