Value

Value(arg)

Base class for a data generating expression having a known type.

Methods

Name Description
asc Sort an expression ascending.
between Check if this expression is between lower and upper, inclusive.
case Create a SimpleCaseBuilder to chain multiple if-else statements.
cases Create a case expression in one shot.
cast Cast expression to indicated data type.
coalesce Return the first non-null value from args.
collect Aggregate this expression’s elements into an array.
desc Sort an expression descending.
fill_null Replace any null values with the indicated fill value.
fillna Deprecated - use fill_null instead.
group_concat Concatenate values using the indicated separator to produce a string.
hash Compute an integer hash value.
identical_to Return whether this expression is identical to other.
isin Check whether this expression’s values are in values.
isnull Return whether this expression is NULL.
name Rename an expression to name.
notin Check whether this expression’s values are not in values.
notnull Return whether this expression is not NULL.
nullif Set values to null if they equal the values null_if_expr.
over Construct a window expression.
substitute Replace values given in values with replacement.
to_pandas Convert a column expression to a pandas Series or scalar object.
try_cast Try cast expression to indicated data type.
type Return the DataType of self.
typeof Return the string name of the datatype of self.

asc

asc(nulls_first=False)

Sort an expression ascending.

between

between(lower, upper)

Check if this expression is between lower and upper, inclusive.

Parameters

Name Type Description Default
lower Value Lower bound, inclusive required
upper Value Upper bound, inclusive required

Returns

Name Type Description
BooleanValue Expression indicating membership in the provided range

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch().limit(5)
>>> t.bill_length_mm.between(35, 38)
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Between(bill_length_mm, 35, 38) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean                         │
├─────────────────────────────────┤
│ False                           │
│ False                           │
│ False                           │
│ NULL                            │
│ True                            │
└─────────────────────────────────┘

case

case()

Create a SimpleCaseBuilder to chain multiple if-else statements.

Add new search expressions with the .when() method. These must be comparable with this column expression. Conclude by calling .end().

Returns

Name Type Description
SimpleCaseBuilder A case builder

See Also

Value.substitute() ibis.cases() ibis.case()

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> x = ibis.examples.penguins.fetch().head(5)["sex"]
>>> x
┏━━━━━━━━┓
┃ sex    ┃
┡━━━━━━━━┩
│ string │
├────────┤
│ male   │
│ female │
│ female │
│ NULL   │
│ female │
└────────┘
>>> x.case().when("male", "M").when("female", "F").else_("U").end()
/tmp/ipykernel_31185/2271248134.py:1: FutureWarning: `Value.case` is deprecated as of v10.0.0; use value.cases() or ibis.cases()
  x.case().when("male", "M").when("female", "F").else_("U").end()
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ SimpleCase(sex, ('male', 'female'), ('M', 'F'), 'U') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string                                               │
├──────────────────────────────────────────────────────┤
│ M                                                    │
│ F                                                    │
│ F                                                    │
│ U                                                    │
│ F                                                    │
└──────────────────────────────────────────────────────┘

Cases not given result in the ELSE case

>>> x.case().when("male", "M").else_("OTHER").end()
/tmp/ipykernel_31185/2674247010.py:1: FutureWarning: `Value.case` is deprecated as of v10.0.0; use value.cases() or ibis.cases()
  x.case().when("male", "M").else_("OTHER").end()
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ SimpleCase(sex, ('male',), ('M',), 'OTHER') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string                                      │
├─────────────────────────────────────────────┤
│ M                                           │
│ OTHER                                       │
│ OTHER                                       │
│ OTHER                                       │
│ OTHER                                       │
└─────────────────────────────────────────────┘

If you don’t supply an ELSE, then NULL is used

>>> x.case().when("male", "M").end()
/tmp/ipykernel_31185/1790570072.py:1: FutureWarning: `Value.case` is deprecated as of v10.0.0; use value.cases() or ibis.cases()
  x.case().when("male", "M").end()
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ SimpleCase(sex, ('male',), ('M',), Cast(None, string)) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string                                                 │
├────────────────────────────────────────────────────────┤
│ M                                                      │
│ NULL                                                   │
│ NULL                                                   │
│ NULL                                                   │
│ NULL                                                   │
└────────────────────────────────────────────────────────┘

cases

cases(case_result_pairs, default=None)

Create a case expression in one shot.

Parameters

Name Type Description Default
case_result_pairs Iterable[tuple[ir.BooleanValue, Value]] Conditional-result pairs required
default Value | None Value to return if none of the case conditions are true None

Returns

Name Type Description
Value Value expression

See Also

Value.substitute() ibis.cases() ibis.case()

Examples

>>> import xorq as xo
>>> xo.options.interactive = True
>>> t = xo.memtable({"values": [1, 2, 1, 2, 3, 2, 4]})
>>> t
┏━━━━━━━━┓
┃ values ┃
┡━━━━━━━━┩
│ int64  │
├────────┤
│      1 │
│      2 │
│      1 │
│      2 │
│      3 │
│      2 │
│      4 │
└────────┘
>>> number_letter_map = ((1, "a"), (2, "b"), (3, "c"))
>>> t.values.cases(number_letter_map, default="unk").name("replace")
┏━━━━━━━━━┓
┃ replace ┃
┡━━━━━━━━━┩
│ string  │
├─────────┤
│ a       │
│ b       │
│ a       │
│ b       │
│ c       │
│ b       │
│ unk     │
└─────────┘

cast

cast(target_type)

Cast expression to indicated data type.

Similar to pandas.Series.astype.

Parameters

Name Type Description Default
target_type Any Type to cast to. Anything accepted by ibis.dtype() required

Returns

Name Type Description
Value Casted expression

See Also

Value.try_cast() ibis.dtype()

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> x = ibis.examples.penguins.fetch()["bill_depth_mm"]
>>> x
┏━━━━━━━━━━━━━━━┓
┃ bill_depth_mm ┃
┡━━━━━━━━━━━━━━━┩
│ float64       │
├───────────────┤
│          18.7 │
│          17.4 │
│          18.0 │
│          NULL │
│          19.3 │
│          20.6 │
│          17.8 │
│          19.6 │
│          18.1 │
│          20.2 │
│              │
└───────────────┘

python’s built-in types can be used

>>> x.cast(int)
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Cast(bill_depth_mm, int64) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ int64                      │
├────────────────────────────┤
│                         19 │
│                         17 │
│                         18 │
│                       NULL │
│                         19 │
│                         21 │
│                         18 │
│                         20 │
│                         18 │
│                         20 │
│                           │
└────────────────────────────┘

or string names

>>> x.cast("int8")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Cast(bill_depth_mm, int8) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ int8                      │
├───────────────────────────┤
│                        19 │
│                        17 │
│                        18 │
│                      NULL │
│                        19 │
│                        21 │
│                        18 │
│                        20 │
│                        18 │
│                        20 │
│                          │
└───────────────────────────┘

If you make an illegal cast, you won’t know until the backend actually executes it. Consider .try_cast().

>>> ibis.literal("a string").cast("int64")  

coalesce

coalesce(*args)

Return the first non-null value from args.

Parameters

Name Type Description Default
args Value Arguments from which to choose the first non-null value ()

Returns

Name Type Description
Value Coalesced expression

See Also

ibis.coalesce() Value.fill_null()

Examples

>>> import ibis
>>> ibis.coalesce(None, 4, 5).name("x")

┌───┐
│ 4 │
└───┘

collect

collect(where=None, order_by=None, include_null=False)

Aggregate this expression’s elements into an array.

This function is called array_agg, list_agg, or list in other systems.

Parameters

Name Type Description Default
where ir.BooleanValue | None An optional filter expression. If provided, only rows where where is True will be included in the aggregate. None
order_by Any An ordering key (or keys) to use to order the rows before aggregating. If not provided, the order of the items in the result is undefined and backend specific. None
include_null bool Whether to include null values when performing this aggregation. Set to True to include nulls in the result. False

Returns

Name Type Description
ArrayScalar Collected array

Examples

Basic collect usage

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"key": list("aaabb"), "value": [1, 2, 3, 4, 5]})
>>> t
┏━━━━━━━━┳━━━━━━━┓
┃ key     value ┃
┡━━━━━━━━╇━━━━━━━┩
│ stringint64 │
├────────┼───────┤
│ a     1 │
│ a     2 │
│ a     3 │
│ b     4 │
│ b     5 │
└────────┴───────┘
>>> t.value.collect()

┌────────────────┐
│ [1, 2, ... +3] │
└────────────────┘
>>> type(t.value.collect())
ibis.expr.types.arrays.ArrayScalar

Collect elements per group

>>> t.group_by("key").agg(v=lambda t: t.value.collect()).order_by("key")
┏━━━━━━━━┳━━━━━━━━━━━━━━━━┓
┃ key     v              ┃
┡━━━━━━━━╇━━━━━━━━━━━━━━━━┩
│ stringarray<int64>   │
├────────┼────────────────┤
│ a     [1, 2, ... +1] │
│ b     [4, 5]         │
└────────┴────────────────┘

Collect elements per group using a filter

>>> t.group_by("key").agg(v=lambda t: t.value.collect(where=t.value > 1)).order_by("key")
┏━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ key     v            ┃
┡━━━━━━━━╇━━━━━━━━━━━━━━┩
│ stringarray<int64> │
├────────┼──────────────┤
│ a     [2, 3]       │
│ b     [4, 5]       │
└────────┴──────────────┘

desc

desc(nulls_first=False)

Sort an expression descending.

fill_null

fill_null(fill_value)

Replace any null values with the indicated fill value.

Parameters

Name Type Description Default
fill_value Scalar Value with which to replace NULL values in self required

See Also

Value.coalesce() ibis.coalesce()

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch().limit(5)
>>> t.sex
┏━━━━━━━━┓
┃ sex    ┃
┡━━━━━━━━┩
│ string │
├────────┤
│ male   │
│ female │
│ female │
│ NULL   │
│ female │
└────────┘
>>> t.sex.fill_null("unrecorded").name("sex")
┏━━━━━━━━━━━━┓
┃ sex        ┃
┡━━━━━━━━━━━━┩
│ string     │
├────────────┤
│ male       │
│ female     │
│ female     │
│ unrecorded │
│ female     │
└────────────┘

Returns

Name Type Description
Value self filled with fill_value where it is NULL

fillna

fillna(fill_value)

Deprecated - use fill_null instead.

group_concat

group_concat(sep=',', where=None, order_by=None)

Concatenate values using the indicated separator to produce a string.

Parameters

Name Type Description Default
sep str The separator to use to join strings. ','
where ir.BooleanValue | None An optional filter expression. If provided, only rows where where is True will be included in the aggregate. None
order_by Any An ordering key (or keys) to use to order the rows before aggregating. If not provided, the order of the items in the result is undefined and backend specific. None

Returns

Name Type Description
StringScalar Concatenated string expression

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch().limit(5)
>>> t[["bill_length_mm", "bill_depth_mm"]]
┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ bill_length_mm  bill_depth_mm ┃
┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ float64float64       │
├────────────────┼───────────────┤
│           39.118.7 │
│           39.517.4 │
│           40.318.0 │
│           NULLNULL │
│           36.719.3 │
└────────────────┴───────────────┘
>>> t.bill_length_mm.group_concat()

┌─────────────────────┐
│ 39.1,39.5,40.3,36.7 │
└─────────────────────┘
>>> t.bill_length_mm.group_concat(sep=": ")

┌────────────────────────┐
│ 39.1: 39.5: 40.3: 36.7 │
└────────────────────────┘
>>> t.bill_length_mm.group_concat(sep=": ", where=t.bill_depth_mm > 18)

┌────────────┐
│ 39.1: 36.7 │
└────────────┘

hash

hash()

Compute an integer hash value.

The hashing function used is backend-dependent.

Returns

Name Type Description
IntegerValue The hash value of self

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> ibis.literal("hello").hash()

┌─────────────────────┐
│ 8628213525773890682 │
└─────────────────────┘

identical_to

identical_to(other)

Return whether this expression is identical to other.

Corresponds to IS NOT DISTINCT FROM in SQL.

Parameters

Name Type Description Default
other Value Expression to compare to required

Returns

Name Type Description
BooleanValue Whether this expression is not distinct from other

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> one = ibis.literal(1)
>>> two = ibis.literal(2)
>>> two.identical_to(one + one)

┌──────┐
│ True │
└──────┘

isin

isin(values)

Check whether this expression’s values are in values.

NULL values are propagated in the output. See examples for details.

Parameters

Name Type Description Default
values Value | Sequence[Value] Values or expression to check for membership required

Returns

Name Type Description
BooleanValue Expression indicating membership

See Also

Value.notin()

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"a": [1, 2, 3], "b": [2, 3, 4]})
>>> t
┏━━━━━━━┳━━━━━━━┓
┃ a      b     ┃
┡━━━━━━━╇━━━━━━━┩
│ int64int64 │
├───────┼───────┤
│     12 │
│     23 │
│     34 │
└───────┴───────┘

Check against a literal sequence of values

>>> t.a.isin([1, 2])
┏━━━━━━━━━━━━━━━━━━━━━┓
┃ InValues(a, (1, 2)) ┃
┡━━━━━━━━━━━━━━━━━━━━━┩
│ boolean             │
├─────────────────────┤
│ True                │
│ True                │
│ False               │
└─────────────────────┘

Check against a derived expression

>>> t.a.isin(t.b + 1)
┏━━━━━━━━━━━━━━━┓
┃ InSubquery(a) ┃
┡━━━━━━━━━━━━━━━┩
│ boolean       │
├───────────────┤
│ False         │
│ False         │
│ True          │
└───────────────┘

Check against a column from a different table

>>> t2 = ibis.memtable({"x": [99, 2, 99]})
>>> t.a.isin(t2.x)
┏━━━━━━━━━━━━━━━┓
┃ InSubquery(a) ┃
┡━━━━━━━━━━━━━━━┩
│ boolean       │
├───────────────┤
│ False         │
│ True          │
│ False         │
└───────────────┘

NULL behavior

>>> t = ibis.memtable({"x": [1, 2]})
>>> t.x.isin([1, None])
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ InValues(x, (1, None)) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean                │
├────────────────────────┤
│ True                   │
│ NULL                   │
└────────────────────────┘
>>> t = ibis.memtable({"x": [1, None, 2]})
>>> t.x.isin([1])
┏━━━━━━━━━━━━━━━━━━━┓
┃ InValues(x, (1,)) ┃
┡━━━━━━━━━━━━━━━━━━━┩
│ boolean           │
├───────────────────┤
│ True              │
│ NULL              │
│ False             │
└───────────────────┘
>>> t.x.isin([3])
┏━━━━━━━━━━━━━━━━━━━┓
┃ InValues(x, (3,)) ┃
┡━━━━━━━━━━━━━━━━━━━┩
│ boolean           │
├───────────────────┤
│ False             │
│ NULL              │
│ False             │
└───────────────────┘

isnull

isnull()

Return whether this expression is NULL.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch().limit(5)
>>> t.bill_depth_mm
┏━━━━━━━━━━━━━━━┓
┃ bill_depth_mm ┃
┡━━━━━━━━━━━━━━━┩
│ float64       │
├───────────────┤
│          18.7 │
│          17.4 │
│          18.0 │
│          NULL │
│          19.3 │
└───────────────┘
>>> t.bill_depth_mm.isnull()
┏━━━━━━━━━━━━━━━━━━━━━━━┓
┃ IsNull(bill_depth_mm) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean               │
├───────────────────────┤
│ False                 │
│ False                 │
│ False                 │
│ True                  │
│ False                 │
└───────────────────────┘

name

name(name)

Rename an expression to name.

Parameters

Name Type Description Default
name The new name of the expression required

Returns

Name Type Description
Value self with name name

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"a": [1, 2]}, name="t")
>>> t.a
┏━━━━━━━┓
┃ a     ┃
┡━━━━━━━┩
│ int64 │
├───────┤
│     1 │
│     2 │
└───────┘
>>> t.a.name("b")
┏━━━━━━━┓
┃ b     ┃
┡━━━━━━━┩
│ int64 │
├───────┤
│     1 │
│     2 │
└───────┘

notin

notin(values)

Check whether this expression’s values are not in values.

Opposite of Value.isin().

Parameters

Name Type Description Default
values Value | Sequence[Value] Values or expression to check for lack of membership required

Returns

Name Type Description
BooleanValue Whether self’s values are not contained in values

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch().limit(5)
>>> t.bill_depth_mm
┏━━━━━━━━━━━━━━━┓
┃ bill_depth_mm ┃
┡━━━━━━━━━━━━━━━┩
│ float64       │
├───────────────┤
│          18.7 │
│          17.4 │
│          18.0 │
│          NULL │
│          19.3 │
└───────────────┘
>>> t.bill_depth_mm.notin([18.7, 18.1])
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Not(InValues(bill_depth_mm, (18.7, 18.1))) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean                                    │
├────────────────────────────────────────────┤
│ False                                      │
│ True                                       │
│ True                                       │
│ NULL                                       │
│ True                                       │
└────────────────────────────────────────────┘

notnull

notnull()

Return whether this expression is not NULL.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch().limit(5)
>>> t.bill_depth_mm
┏━━━━━━━━━━━━━━━┓
┃ bill_depth_mm ┃
┡━━━━━━━━━━━━━━━┩
│ float64       │
├───────────────┤
│          18.7 │
│          17.4 │
│          18.0 │
│          NULL │
│          19.3 │
└───────────────┘
>>> t.bill_depth_mm.notnull()
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ NotNull(bill_depth_mm) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean                │
├────────────────────────┤
│ True                   │
│ True                   │
│ True                   │
│ False                  │
│ True                   │
└────────────────────────┘

nullif

nullif(null_if_expr)

Set values to null if they equal the values null_if_expr.

Commonly used to avoid divide-by-zero problems by replacing zero with NULL in the divisor.

Equivalent to (self == null_if_expr).ifelse(ibis.null(), self).

Parameters

Name Type Description Default
null_if_expr Value Expression indicating what values should be NULL required

Returns

Name Type Description
Value Value expression

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> vals = ibis.examples.penguins.fetch().head(5).sex
>>> vals
┏━━━━━━━━┓
┃ sex    ┃
┡━━━━━━━━┩
│ string │
├────────┤
│ male   │
│ female │
│ female │
│ NULL   │
│ female │
└────────┘
>>> vals.nullif("male")
┏━━━━━━━━━━━━━━━━━━━━━┓
┃ NullIf(sex, 'male') ┃
┡━━━━━━━━━━━━━━━━━━━━━┩
│ string              │
├─────────────────────┤
│ NULL                │
│ female              │
│ female              │
│ NULL                │
│ female              │
└─────────────────────┘

over

over(window=None, *, rows=None, range=None, group_by=None, order_by=None)

Construct a window expression.

Parameters

Name Type Description Default
window Window specification None
rows Whether to use the ROWS window clause None
range Whether to use the RANGE window clause None
group_by Grouping key None
order_by Ordering key None

Returns

Name Type Description
Value A window function expression

substitute

substitute(value, replacement=None, else_=None)

Replace values given in values with replacement.

This is similar to the pandas replace method.

Parameters

Name Type Description Default
value Value | dict Expression or dict. required
replacement Value | None If an expression is passed to value, this must be passed. None
else_ Value | None If an original value does not match value, then else_ is used. The default of None means leave the original value unchanged. None

Returns

Name Type Description
Value Replaced values

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch()
>>> t.island.value_counts().order_by("island")
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ island     island_count ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━┩
│ stringint64        │
├───────────┼──────────────┤
│ Biscoe   168 │
│ Dream    124 │
│ Torgersen52 │
└───────────┴──────────────┘
>>> t.island.substitute({"Torgersen": "torg", "Biscoe": "bisc"}).name(
...     "island"
... ).value_counts().order_by("island")
┏━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ island  island_count ┃
┡━━━━━━━━╇━━━━━━━━━━━━━━┩
│ stringint64        │
├────────┼──────────────┤
│ Dream 124 │
│ bisc  168 │
│ torg  52 │
└────────┴──────────────┘

to_pandas

to_pandas(**kwargs)

Convert a column expression to a pandas Series or scalar object.

Parameters

Name Type Description Default
kwargs Same as keyword arguments to execute {}

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch().limit(5)
>>> t.to_pandas()
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 male 2007
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 female 2007
2 Adelie Torgersen 40.3 18.0 195.0 3250.0 female 2007
3 Adelie Torgersen NaN NaN NaN NaN None 2007
4 Adelie Torgersen 36.7 19.3 193.0 3450.0 female 2007

try_cast

try_cast(target_type)

Try cast expression to indicated data type.

If the cast fails for a row, the value is returned as null or NaN depending on target_type and backend behavior.

Parameters

Name Type Description Default
target_type Any Type to try cast to. Anything accepted by ibis.dtype() required

Returns

Name Type Description
Value Casted expression

See Also

Value.cast() ibis.dtype()

Examples

>>> import ibis
>>> from ibis import _
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"numbers": [1, 2, 3, 4], "strings": ["1.0", "2", "hello", "world"]})
>>> t
┏━━━━━━━━━┳━━━━━━━━━┓
┃ numbers  strings ┃
┡━━━━━━━━━╇━━━━━━━━━┩
│ int64string  │
├─────────┼─────────┤
│       11.0     │
│       22       │
│       3hello   │
│       4world   │
└─────────┴─────────┘
>>> t = t.mutate(numbers_to_strings=_.numbers.try_cast("string"))
>>> t = t.mutate(strings_to_numbers=_.strings.try_cast("int"))
>>> t
┏━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┓
┃ numbers  strings  numbers_to_strings  strings_to_numbers ┃
┡━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━┩
│ int64stringstringint64              │
├─────────┼─────────┼────────────────────┼────────────────────┤
│       11.0    1                 1 │
│       22      2                 2 │
│       3hello  3                 NULL │
│       4world  4                 NULL │
└─────────┴─────────┴────────────────────┴────────────────────┘

type

type()

Return the DataType of self.

typeof

typeof()

Return the string name of the datatype of self.

The values of the returned strings are necessarily backend dependent. e.g. duckdb may say “DOUBLE”, while sqlite may say “real”.

Returns

Name Type Description
StringValue A string indicating the type of the value

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> vals = ibis.examples.penguins.fetch().head(5).bill_length_mm
>>> vals
┏━━━━━━━━━━━━━━━━┓
┃ bill_length_mm ┃
┡━━━━━━━━━━━━━━━━┩
│ float64        │
├────────────────┤
│           39.1 │
│           39.5 │
│           40.3 │
│           NULL │
│           36.7 │
└────────────────┘
>>> vals.typeof()
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ TypeOf(bill_length_mm) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string                 │
├────────────────────────┤
│ DOUBLE                 │
│ DOUBLE                 │
│ DOUBLE                 │
│ DOUBLE                 │
│ DOUBLE                 │
└────────────────────────┘

Different backends have different names for their native types

>>> ibis.duckdb.connect().execute(ibis.literal(5.4).typeof())
'DECIMAL(2,1)'
>>> ibis.sqlite.connect().execute(ibis.literal(5.4).typeof())
'real'