-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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(python): Implement Arrow PyCapsule Interface for Series/DataFrame export #17676
Changes from all commits
435b972
11e45fc
a084a45
d9c8a80
eb9b960
59c2048
8226931
ab015a1
ecf5334
b309a57
5f23484
cc04d92
c2e2144
d40f696
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
from polars.exceptions import ComputeError, UnstableWarning | ||
from polars.interchange.protocol import CompatLevel | ||
from polars.testing import assert_frame_equal, assert_series_equal | ||
from tests.unit.utils.pycapsule_utils import PyCapsuleStreamHolder | ||
|
||
|
||
def test_arrow_list_roundtrip() -> None: | ||
|
@@ -749,3 +750,20 @@ def test_compat_level(monkeypatch: pytest.MonkeyPatch) -> None: | |
assert len(df.write_ipc_stream(None).getbuffer()) == 544 | ||
assert len(df.write_ipc_stream(None, compat_level=oldest).getbuffer()) == 672 | ||
assert len(df.write_ipc_stream(None, compat_level=newest).getbuffer()) == 544 | ||
|
||
|
||
def test_df_pycapsule_interface() -> None: | ||
df = pl.DataFrame( | ||
{ | ||
"a": [1, 2, 3], | ||
"b": ["a", "b", "c"], | ||
"c": ["fooooooooooooooooooooo", "bar", "looooooooooooooooong string"], | ||
} | ||
) | ||
out = pa.table(PyCapsuleStreamHolder(df)) | ||
assert df.shape == out.shape | ||
assert df.schema.names() == out.schema.names | ||
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. You could drop 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. I updated the test to not hold a bare capsule, but rather call the underlying object's |
||
|
||
df2 = pl.from_arrow(out) | ||
assert isinstance(df2, pl.DataFrame) | ||
assert df.equals(df2) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
ShapeError, | ||
) | ||
from polars.testing import assert_frame_equal, assert_series_equal | ||
from tests.unit.utils.pycapsule_utils import PyCapsuleStreamHolder | ||
|
||
if TYPE_CHECKING: | ||
from zoneinfo import ZoneInfo | ||
|
@@ -628,6 +629,13 @@ def test_arrow() -> None: | |
) | ||
|
||
|
||
def test_pycapsule_interface() -> None: | ||
a = pl.Series("a", [1, 2, 3, None]) | ||
out = pa.chunked_array(PyCapsuleStreamHolder(a)) | ||
out_arr = out.combine_chunks() | ||
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 idea here (drop |
||
assert out_arr == pa.array([1, 2, 3, None]) | ||
|
||
|
||
def test_get() -> None: | ||
a = pl.Series("a", [1, 2, 3]) | ||
pos_idxs = pl.Series("idxs", [2, 0, 1, 0], dtype=pl.Int8) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Any | ||
|
||
|
||
class PyCapsuleStreamHolder: | ||
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. This is put in a helper file because it's used by tests both in this PR and in https://github.com/pola-rs/polars/pull/17693/files. Let me know if there's a better place to put this test helper. |
||
""" | ||
Hold the Arrow C Stream pycapsule. | ||
|
||
A class that exposes _only_ the Arrow C Stream interface via Arrow PyCapsules. | ||
This ensures that pyarrow is seeing _only_ the `__arrow_c_stream__` dunder, and | ||
that nothing else (e.g. the dataframe or array interface) is actually being | ||
used. | ||
|
||
This is used by tests across multiple files. | ||
""" | ||
|
||
arrow_obj: Any | ||
|
||
def __init__(self, arrow_obj: object) -> None: | ||
self.arrow_obj = arrow_obj | ||
|
||
def __arrow_c_stream__(self, requested_schema: object = None) -> object: | ||
return self.arrow_obj.__arrow_c_stream__(requested_schema) |
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.
Arrow-rs also implements this: https://github.com/apache/arrow-rs/blob/6d4e2f2ceaf423031b0bc72f54c547dd77a0ddbb/arrow-array/src/ffi_stream.rs#L100