From b3d961998b6cf6a6fcff5e1405e1b37b144dd5f6 Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Thu, 8 Jun 2023 11:09:21 -0400 Subject: [PATCH] feat(api): add `__dataframe__` implementation --- ibis/backends/tests/test_export.py | 16 ++++++++++++++++ ibis/expr/types/relations.py | 3 +++ 2 files changed, 19 insertions(+) diff --git a/ibis/backends/tests/test_export.py b/ibis/backends/tests/test_export.py index 630d1bd061da..6f5a94db81e7 100644 --- a/ibis/backends/tests/test_export.py +++ b/ibis/backends/tests/test_export.py @@ -396,3 +396,19 @@ def test_arrow_timestamp_with_time_zone(alltypes): (batch,) = t.to_pyarrow_batches() assert batch.schema.types == expected + + +@pytest.mark.notimpl(["dask", "druid"]) +@pytest.mark.notimpl( + ["mysql"], + raises=pa.ArrowInvalid, + reason="attempted conversion from decimal to double", +) +@pytest.mark.notimpl( + ["impala"], raises=AttributeError, reason="missing `fetchmany` on the cursor" +) +def test_dataframe_protocol(alltypes): + pytest.importorskip("pyarrow", minversion="12") + output = alltypes.__dataframe__() + assert list(output.column_names()) == alltypes.columns + assert alltypes.count().execute() == output.num_rows() diff --git a/ibis/expr/types/relations.py b/ibis/expr/types/relations.py index 4176d7cec355..64439f598f66 100644 --- a/ibis/expr/types/relations.py +++ b/ibis/expr/types/relations.py @@ -109,6 +109,9 @@ class Table(Expr, _FixedTextJupyterMixin): def __array__(self, dtype=None): return self.execute().__array__(dtype) + def __dataframe__(self): + return self.to_pyarrow().__dataframe__() + def as_table(self) -> Table: """Promote the expression to a table.