Skip to content

Commit

Permalink
feat(api): implement __array__
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 21, 2022
1 parent a980b34 commit 1402347
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ibis/backends/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import platform
import re

import numpy as np
import pandas as pd
import pandas.testing as tm
import pytest
Expand Down Expand Up @@ -691,3 +692,23 @@ def test_default_backend():
SUM\\((\\w+)\\.a\\) AS sum
FROM \\w+ AS \\1"""
assert re.match(rx, sql) is not None


def test_dunder_array_table(alltypes, df):
expr = alltypes.group_by("string_col").int_col.sum().sort_by("string_col")
result = np.array(expr)
expected = np.array(
df.groupby("string_col")
.int_col.sum()
.reset_index()
.sort_values(["string_col"])
)
np.testing.assert_array_equal(result, expected)


@pytest.mark.broken(["dask"], reason="Dask backend duplicates data")
def test_dunder_array_column(alltypes, df):
expr = alltypes.sort_by("id").head(10).int_col
result = np.array(expr)
expected = df.sort_values(["id"]).head(10).int_col
np.testing.assert_array_equal(result, expected)
3 changes: 3 additions & 0 deletions ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ def _repr_html_(self) -> str | None:

@public
class Column(Value):
def __array__(self):
return self.execute().__array__()

def _repr_html_(self) -> str | None:
if not ibis.options.interactive:
return None
Expand Down
3 changes: 3 additions & 0 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def f(

@public
class Table(Expr):
def __array__(self):
return self.execute().__array__()

def __contains__(self, name):
return name in self.schema()

Expand Down

0 comments on commit 1402347

Please sign in to comment.