Skip to content

Commit

Permalink
feat(api): implement to_pandas() API for ecosystem compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Feb 2, 2023
1 parent db94f4a commit cad316c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions ibis/backends/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def _create_temp_table_with_schema(con, temp_table_name, schema, data=None):
con.drop_table(temp_table_name, force=True)
con.create_table(temp_table_name, schema=schema)
temporary = con.table(temp_table_name)
assert temporary.execute().empty
assert temporary.to_pandas().empty

if data is not None and isinstance(data, pd.DataFrame):
con.load_data(temp_table_name, data, if_exists="append")
result = temporary.execute()
result = temporary.to_pandas()
assert len(result) == len(data.index)
tm.assert_frame_equal(
result.sort_values(result.columns[0]).reset_index(drop=True),
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def test_trig_functions_literals(con, expr, expected):
def test_trig_functions_columns(backend, expr, alltypes, df, expected_fn):
dc_max = df.double_col.max()
expr = alltypes.mutate(dc=(_.double_col / dc_max).nullifzero()).select(tmp=expr)
result = expr.tmp.execute()
result = expr.tmp.to_pandas()
expected = expected_fn((df.double_col / dc_max).replace(0.0, np.nan)).rename("tmp")
backend.assert_series_equal(result, expected)

Expand Down
13 changes: 12 additions & 1 deletion ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import ibis.common.exceptions as com
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
from ibis import util
from ibis.common.grounds import Singleton
from ibis.expr.types.core import Expr, _binop, _FixedTextJupyterMixin

if TYPE_CHECKING:
import pandas as pd

import ibis.expr.types as ir
import ibis.expr.window as win

Expand Down Expand Up @@ -509,6 +510,16 @@ def as_table(self) -> ir.Table:
table = roots[0].to_expr()
return table.select(self)

def to_pandas(self, **kwargs) -> pd.Series:
"""Convert a column expression to a pandas Series or scalar object.
Parameters
----------
kwargs
Same as keyword arguments to [`execute`][ibis.expr.types.core.Expr.execute]
"""
return self.execute(**kwargs)


@public
class Scalar(Value):
Expand Down
13 changes: 12 additions & 1 deletion ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
from ibis.expr.types.core import Expr, _FixedTextJupyterMixin

if TYPE_CHECKING:
import pandas as pd

import ibis.expr.schema as sch
import ibis.expr.types as ir
from ibis.expr.types.generic import Column
from ibis.expr.types.groupby import GroupedTable


Expand Down Expand Up @@ -1361,6 +1362,16 @@ def sql(self, query: str) -> ir.Table:
)
return op.to_expr()

def to_pandas(self, **kwargs) -> pd.DataFrame:
"""Convert a table expression to a pandas DataFrame.
Parameters
----------
kwargs
Same as keyword arguments to [`execute`][ibis.expr.types.core.Expr.execute]
"""
return self.execute(**kwargs)


def _resolve_predicates(
table: Table, predicates
Expand Down

0 comments on commit cad316c

Please sign in to comment.