Skip to content

Commit

Permalink
chore(backends): convert scalars to non-numpy python objects (#10233)
Browse files Browse the repository at this point in the history
This PR converts scalar numpy objects into their non-numpy Python
equivalents for consistent scalar return types for all backends.

BREAKING CHANGE: `execute` now returns non-numpy objects for scalar values.
  • Loading branch information
cpcloud authored Sep 27, 2024
1 parent c898cbc commit df08d5e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
9 changes: 2 additions & 7 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,15 +1065,10 @@ def test_int_column(alltypes):
assert result.dtype == np.int8


@pytest.mark.notimpl(["druid", "oracle"])
@pytest.mark.never(
["bigquery", "sqlite", "snowflake"], reason="backend only implements int64"
)
def test_int_scalar(alltypes):
expr = alltypes.int_col.min()
result = expr.execute()
assert expr.type() == dt.int32
assert result.dtype == np.int32
assert expr.type().is_integer()
assert isinstance(expr.execute(), int)


@pytest.mark.notimpl(["datafusion", "polars", "druid"])
Expand Down
6 changes: 3 additions & 3 deletions ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ def param(type: dt.DataType) -> ir.Scalar:
... )
>>> expr = t.filter(t.date_col >= start).value.sum()
>>> expr.execute(params={start: date(2013, 1, 1)})
np.float64(6.0)
6.0
>>> expr.execute(params={start: date(2013, 1, 2)})
np.float64(5.0)
5.0
>>> expr.execute(params={start: date(2013, 1, 3)})
np.float64(3.0)
3.0
"""
return ops.ScalarParameter(type).to_expr()

Expand Down
3 changes: 1 addition & 2 deletions ibis/expr/operations/udf.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ def builtin(
>>> expr = hamming("duck", "luck")
>>> con = ibis.connect("duckdb://")
>>> con.execute(expr)
np.int64(1)
1
"""
return _wrap(
cls._make_wrapper,
Expand Down
13 changes: 12 additions & 1 deletion ibis/formats/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,18 @@ def convert_column(cls, obj, dtype):
@classmethod
def convert_scalar(cls, obj, dtype):
df = PandasData.convert_table(obj, sch.Schema({str(obj.columns[0]): dtype}))
return df.iat[0, 0]
value = df.iat[0, 0]

if dtype.is_array():
try:
return value.tolist()
except AttributeError:
return value

try:
return value.item()
except AttributeError:
return value

@classmethod
def convert_GeoSpatial(cls, s, dtype, pandas_type):
Expand Down

0 comments on commit df08d5e

Please sign in to comment.