From 6259b7ec221dd644f90b3782b7f7f77bf71dbc0e Mon Sep 17 00:00:00 2001 From: Jim Crist-Harif Date: Tue, 7 May 2024 11:06:52 -0500 Subject: [PATCH] feat(pyarrow): support Arrow PyCapsule interface on `ibis.Table` objects --- ibis/backends/tests/test_export.py | 15 +++++++++++++++ ibis/expr/types/relations.py | 3 +++ 2 files changed, 18 insertions(+) diff --git a/ibis/backends/tests/test_export.py b/ibis/backends/tests/test_export.py index 8d8af79a20b7..1a2d13b9987b 100644 --- a/ibis/backends/tests/test_export.py +++ b/ibis/backends/tests/test_export.py @@ -4,6 +4,7 @@ import pyarrow as pa import pyarrow.csv as pcsv import pytest +from packaging.version import parse as vparse from pytest import param import ibis @@ -44,6 +45,20 @@ limit_no_limit = limit + no_limit +@pytest.mark.skipif( + vparse(pa.__version__) < vparse("14"), reason="pyarrow >= 14 required" +) +def test_table___arrow_c_stream__(awards_players): + res = pa.table(awards_players) + sol = awards_players.to_pyarrow() + assert res.equals(sol) + + # With explicit schema + schema = awards_players.schema().to_pyarrow() + res = pa.table(awards_players, schema=schema) + assert res.equals(sol) + + @pytest.mark.parametrize("limit", limit_no_limit) def test_table_to_pyarrow_batches(limit, awards_players): with awards_players.to_pyarrow_batches(limit=limit) as batch_reader: diff --git a/ibis/expr/types/relations.py b/ibis/expr/types/relations.py index 387a8a60a1d7..29838bfe323c 100644 --- a/ibis/expr/types/relations.py +++ b/ibis/expr/types/relations.py @@ -188,6 +188,9 @@ def __dataframe__(self, nan_as_null: bool = False, allow_copy: bool = True): return IbisDataFrame(self, nan_as_null=nan_as_null, allow_copy=allow_copy) + def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: + return self.to_pyarrow().__arrow_c_stream__(requested_schema) + def __pyarrow_result__( self, table: pa.Table, data_mapper: type[PyArrowData] | None = None ) -> pa.Table: