Skip to content

Commit df08d5e

Browse files
authored
chore(backends): convert scalars to non-numpy python objects (#10233)
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.
1 parent c898cbc commit df08d5e

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

ibis/backends/tests/test_generic.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,15 +1065,10 @@ def test_int_column(alltypes):
10651065
assert result.dtype == np.int8
10661066

10671067

1068-
@pytest.mark.notimpl(["druid", "oracle"])
1069-
@pytest.mark.never(
1070-
["bigquery", "sqlite", "snowflake"], reason="backend only implements int64"
1071-
)
10721068
def test_int_scalar(alltypes):
10731069
expr = alltypes.int_col.min()
1074-
result = expr.execute()
1075-
assert expr.type() == dt.int32
1076-
assert result.dtype == np.int32
1070+
assert expr.type().is_integer()
1071+
assert isinstance(expr.execute(), int)
10771072

10781073

10791074
@pytest.mark.notimpl(["datafusion", "polars", "druid"])

ibis/expr/api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ def param(type: dt.DataType) -> ir.Scalar:
199199
... )
200200
>>> expr = t.filter(t.date_col >= start).value.sum()
201201
>>> expr.execute(params={start: date(2013, 1, 1)})
202-
np.float64(6.0)
202+
6.0
203203
>>> expr.execute(params={start: date(2013, 1, 2)})
204-
np.float64(5.0)
204+
5.0
205205
>>> expr.execute(params={start: date(2013, 1, 3)})
206-
np.float64(3.0)
206+
3.0
207207
"""
208208
return ops.ScalarParameter(type).to_expr()
209209

ibis/expr/operations/udf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ def builtin(
239239
>>> expr = hamming("duck", "luck")
240240
>>> con = ibis.connect("duckdb://")
241241
>>> con.execute(expr)
242-
np.int64(1)
243-
242+
1
244243
"""
245244
return _wrap(
246245
cls._make_wrapper,

ibis/formats/pandas.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,18 @@ def convert_column(cls, obj, dtype):
149149
@classmethod
150150
def convert_scalar(cls, obj, dtype):
151151
df = PandasData.convert_table(obj, sch.Schema({str(obj.columns[0]): dtype}))
152-
return df.iat[0, 0]
152+
value = df.iat[0, 0]
153+
154+
if dtype.is_array():
155+
try:
156+
return value.tolist()
157+
except AttributeError:
158+
return value
159+
160+
try:
161+
return value.item()
162+
except AttributeError:
163+
return value
153164

154165
@classmethod
155166
def convert_GeoSpatial(cls, s, dtype, pandas_type):

0 commit comments

Comments
 (0)