Numeric and Boolean expressions

Integer, floating point, decimal, and boolean expressions.

NumericValue

NumericValue(self, arg)

Methods

Name Description
abs Return the absolute value of self.
acos Compute the arc cosine of self.
asin Compute the arc sine of self.
atan Compute the arc tangent of self.
atan2 Compute the two-argument version of arc tangent.
ceil Return the ceiling of self.
clip Trim values outside of lower and upper bounds.
cos Compute the cosine of self.
cot Compute the cotangent of self.
degrees Compute the degrees of self radians.
exp Compute \(e^\texttt{self}\).
floor Return the floor of an expression.
ln Compute \(\ln\left(\texttt{self}\right)\).
log Compute \(\log_{\texttt{base}}\left(\texttt{self}\right)\).
log10 Compute \(\log_{10}\left(\texttt{self}\right)\).
log2 Compute \(\log_{2}\left(\texttt{self}\right)\).
negate Negate a numeric expression.
point Return a point constructed from the coordinate values.
radians Compute radians from self degrees.
round Round values to an indicated number of decimal places.
sign Return the sign of the input.
sin Compute the sine of self.
sqrt Compute the square root of self.
tan Compute the tangent of self.

abs

abs()

Return the absolute value of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 2, -3, 4]})
>>> t.values.abs()
┏━━━━━━━━━━━━━┓
┃ Abs(values) ┃
┡━━━━━━━━━━━━━┩
│ int64       │
├─────────────┤
│           1 │
│           2 │
│           3 │
│           4 │
└─────────────┘

acos

acos()

Compute the arc cosine of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 0, 1]})
>>> t.values.acos()
┏━━━━━━━━━━━━━━┓
┃ Acos(values) ┃
┡━━━━━━━━━━━━━━┩
│ float64      │
├──────────────┤
│     3.141593 │
│     1.570796 │
│     0.000000 │
└──────────────┘

asin

asin()

Compute the arc sine of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 0, 1]})
>>> t.values.asin()
┏━━━━━━━━━━━━━━┓
┃ Asin(values) ┃
┡━━━━━━━━━━━━━━┩
│ float64      │
├──────────────┤
│    -1.570796 │
│     0.000000 │
│     1.570796 │
└──────────────┘

atan

atan()

Compute the arc tangent of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 0, 1]})
>>> t.values.atan()
┏━━━━━━━━━━━━━━┓
┃ Atan(values) ┃
┡━━━━━━━━━━━━━━┩
│ float64      │
├──────────────┤
│    -0.785398 │
│     0.000000 │
│     0.785398 │
└──────────────┘

atan2

atan2(other)

Compute the two-argument version of arc tangent.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 0, 1]})
>>> t.values.atan2(0)
┏━━━━━━━━━━━━━━━━━━┓
┃ Atan2(values, 0) ┃
┡━━━━━━━━━━━━━━━━━━┩
│ float64          │
├──────────────────┤
│        -1.570796 │
│         0.000000 │
│         1.570796 │
└──────────────────┘

ceil

ceil()

Return the ceiling of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 1.1, 2, 2.1, 3.3]})
>>> t.values.ceil()
┏━━━━━━━━━━━━━━┓
┃ Ceil(values) ┃
┡━━━━━━━━━━━━━━┩
│ int64        │
├──────────────┤
│            1 │
│            2 │
│            2 │
│            3 │
│            4 │
└──────────────┘

clip

clip(lower=None, upper=None)

Trim values outside of lower and upper bounds.

NULL values are preserved and are not replaced with bounds.

Parameters

Name Type Description Default
lower NumericValue | None Lower bound None
upper NumericValue | None Upper bound None

Returns

Name Type Description
NumericValue Clipped input

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {"values": [None, 2, 3, None, 5, None, None, 8]},
...     schema=dict(values="int"),
... )
>>> t.values.clip(lower=3, upper=6)
┏━━━━━━━━━━━━━━━━━━━━┓
┃ Clip(values, 3, 6) ┃
┡━━━━━━━━━━━━━━━━━━━━┩
│ int64              │
├────────────────────┤
│               NULL │
│                  3 │
│                  3 │
│               NULL │
│                  5 │
│               NULL │
│               NULL │
│                  6 │
└────────────────────┘

cos

cos()

Compute the cosine of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 0, 1]})
>>> t.values.cos()
┏━━━━━━━━━━━━━┓
┃ Cos(values) ┃
┡━━━━━━━━━━━━━┩
│ float64     │
├─────────────┤
│    0.540302 │
│    1.000000 │
│    0.540302 │
└─────────────┘

cot

cot()

Compute the cotangent of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, -2, 3]})
>>> t.values.cot()
┏━━━━━━━━━━━━━┓
┃ Cot(values) ┃
┡━━━━━━━━━━━━━┩
│ float64     │
├─────────────┤
│   -0.642093 │
│    0.457658 │
│   -7.015253 │
└─────────────┘

degrees

degrees()

Compute the degrees of self radians.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> from math import pi
>>> t = ibis.memtable({"values": [0, pi / 2, pi, 3 * pi / 2, 2 * pi]})
>>> t.values.degrees()
┏━━━━━━━━━━━━━━━━━┓
┃ Degrees(values) ┃
┡━━━━━━━━━━━━━━━━━┩
│ float64         │
├─────────────────┤
│             0.0 │
│            90.0 │
│           180.0 │
│           270.0 │
│           360.0 │
└─────────────────┘

exp

exp()

Compute \(e^\texttt{self}\).

Returns

Name Type Description
NumericValue \(e^\texttt{self}\)

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": range(4)})
>>> t.values.exp()
┏━━━━━━━━━━━━━┓
┃ Exp(values) ┃
┡━━━━━━━━━━━━━┩
│ float64     │
├─────────────┤
│    1.000000 │
│    2.718282 │
│    7.389056 │
│   20.085537 │
└─────────────┘

floor

floor()

Return the floor of an expression.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 1.1, 2, 2.1, 3.3]})
>>> t.values.floor()
┏━━━━━━━━━━━━━━━┓
┃ Floor(values) ┃
┡━━━━━━━━━━━━━━━┩
│ int64         │
├───────────────┤
│             1 │
│             1 │
│             2 │
│             2 │
│             3 │
└───────────────┘

ln

ln()

Compute \(\ln\left(\texttt{self}\right)\).

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 2.718281828, 3]})
>>> t.values.ln()
┏━━━━━━━━━━━━┓
┃ Ln(values) ┃
┡━━━━━━━━━━━━┩
│ float64    │
├────────────┤
│   0.000000 │
│   1.000000 │
│   1.098612 │
└────────────┘

log

log(base=None)

Compute \(\log_{\texttt{base}}\left(\texttt{self}\right)\).

Parameters

Name Type Description Default
base NumericValue | None The base of the logarithm. If None, base e is used. None

Returns

Name Type Description
NumericValue Logarithm of arg with base base

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> from math import e
>>> t = ibis.memtable({"values": [e, e**2, e**3]})
>>> t.values.log()
┏━━━━━━━━━━━━━┓
┃ Log(values) ┃
┡━━━━━━━━━━━━━┩
│ float64     │
├─────────────┤
│         1.0 │
│         2.0 │
│         3.0 │
└─────────────┘
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [10, 100, 1000]})
>>> t.values.log(base=10)
┏━━━━━━━━━━━━━━━━━┓
┃ Log(values, 10) ┃
┡━━━━━━━━━━━━━━━━━┩
│ float64         │
├─────────────────┤
│             1.0 │
│             2.0 │
│             3.0 │
└─────────────────┘

log10

log10()

Compute \(\log_{10}\left(\texttt{self}\right)\).

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 10, 100]})
>>> t.values.log10()
┏━━━━━━━━━━━━━━━┓
┃ Log10(values) ┃
┡━━━━━━━━━━━━━━━┩
│ float64       │
├───────────────┤
│           0.0 │
│           1.0 │
│           2.0 │
└───────────────┘

log2

log2()

Compute \(\log_{2}\left(\texttt{self}\right)\).

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 2, 4, 8]})
>>> t.values.log2()
┏━━━━━━━━━━━━━━┓
┃ Log2(values) ┃
┡━━━━━━━━━━━━━━┩
│ float64      │
├──────────────┤
│          0.0 │
│          1.0 │
│          2.0 │
│          3.0 │
└──────────────┘

negate

negate()

Negate a numeric expression.

Returns

Name Type Description
NumericValue A numeric value expression

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 0, 1]})
>>> t.values.negate()
┏━━━━━━━━━━━━━━━━┓
┃ Negate(values) ┃
┡━━━━━━━━━━━━━━━━┩
│ int64          │
├────────────────┤
│              1 │
│              0 │
│             -1 │
└────────────────┘

point

point(right)

Return a point constructed from the coordinate values.

Constant coordinates result in construction of a POINT literal or column.

Parameters

Name Type Description Default
right int | float | NumericValue Y coordinate required

Returns

Name Type Description
PointValue Points

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.zones.fetch()
>>> t.x_cent.point(t.y_cent)
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ GeoPoint(x_cent, y_cent)         ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ point:geometry                   │
├──────────────────────────────────┤
│ <POINT (935996.821 191376.75)>   │
│ <POINT (1031085.719 164018.754)> │
│ <POINT (1026452.617 254265.479)> │
│ <POINT (990633.981 202959.782)>  │
│ <POINT (931871.37 140681.351)>   │
│ <POINT (964319.735 157998.936)>  │
│ <POINT (1006496.679 216719.218)> │
│ <POINT (1005551.571 222936.088)> │
│ <POINT (1043002.677 212969.849)> │
│ <POINT (1042223.605 186706.496)> │
│                                 │
└──────────────────────────────────┘

radians

radians()

Compute radians from self degrees.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [0, 90, 180, 270, 360]})
>>> t.values.radians()
┏━━━━━━━━━━━━━━━━━┓
┃ Radians(values) ┃
┡━━━━━━━━━━━━━━━━━┩
│ float64         │
├─────────────────┤
│        0.000000 │
│        1.570796 │
│        3.141593 │
│        4.712389 │
│        6.283185 │
└─────────────────┘

round

round(digits=0)

Round values to an indicated number of decimal places.

Parameters

Name Type Description Default
digits int | IntegerValue The number of digits to round to. Here’s how the digits parameter affects the expression output type: - digits is False-y; self.type() is decimaldecimal - digits is nonzero; self.type() is decimaldecimal - digits is False-y; self.type() is Floating → int64 - digits is nonzero; self.type() is Floating → float64 0

Returns

Name Type Description
NumericValue The rounded expression

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1.22, 1.64, 2.15, 2.54]})
>>> t
┏━━━━━━━━━┓
┃ values  ┃
┡━━━━━━━━━┩
│ float64 │
├─────────┤
│    1.22 │
│    1.64 │
│    2.15 │
│    2.54 │
└─────────┘
>>> t.values.round()
┏━━━━━━━━━━━━━━━━━━┓
┃ Round(values, 0) ┃
┡━━━━━━━━━━━━━━━━━━┩
│ int64            │
├──────────────────┤
│                1 │
│                2 │
│                2 │
│                3 │
└──────────────────┘
>>> t.values.round(digits=1)
┏━━━━━━━━━━━━━━━━━━┓
┃ Round(values, 1) ┃
┡━━━━━━━━━━━━━━━━━━┩
│ float64          │
├──────────────────┤
│              1.2 │
│              1.6 │
│              2.2 │
│              2.5 │
└──────────────────┘

sign

sign()

Return the sign of the input.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 2, -3, 4]})
>>> t.values.sign()
┏━━━━━━━━━━━━━━┓
┃ Sign(values) ┃
┡━━━━━━━━━━━━━━┩
│ int64        │
├──────────────┤
│           -1 │
│            1 │
│           -1 │
│            1 │
└──────────────┘

sin

sin()

Compute the sine of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 0, 1]})
>>> t.values.sin()
┏━━━━━━━━━━━━━┓
┃ Sin(values) ┃
┡━━━━━━━━━━━━━┩
│ float64     │
├─────────────┤
│   -0.841471 │
│    0.000000 │
│    0.841471 │
└─────────────┘

sqrt

sqrt()

Compute the square root of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [1, 4, 9, 16]})
>>> t.values.sqrt()
┏━━━━━━━━━━━━━━┓
┃ Sqrt(values) ┃
┡━━━━━━━━━━━━━━┩
│ float64      │
├──────────────┤
│          1.0 │
│          2.0 │
│          3.0 │
│          4.0 │
└──────────────┘

tan

tan()

Compute the tangent of self.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"values": [-1, 0, 1]})
>>> t.values.tan()
┏━━━━━━━━━━━━━┓
┃ Tan(values) ┃
┡━━━━━━━━━━━━━┩
│ float64     │
├─────────────┤
│   -1.557408 │
│    0.000000 │
│    1.557408 │
└─────────────┘

NumericColumn

NumericColumn(self, arg)

Methods

Name Description
approx_quantile Compute one or more approximate quantiles of a column.
bucket Compute a discrete binning of a numeric array.
corr Return the correlation of two numeric columns.
cov Return the covariance of two numeric columns.
cummean Return the cumulative mean of the input.
cumsum Return the cumulative sum of the input.
histogram Compute a histogram with fixed width bins.
mean Return the mean of a numeric column.
std Return the standard deviation of a numeric column.
sum Return the sum of a numeric column.
var Return the variance of a numeric column.

approx_quantile

approx_quantile(quantile, where=None)

Compute one or more approximate quantiles of a column.

The result may or may not be exact

Whether the result is an approximation depends on the backend.

Parameters

Name Type Description Default
quantile float | ir.NumericValue | Sequence[ir.NumericValue | float] 0 <= quantile <= 1, or an array of such values indicating the quantile or quantiles to compute required
where ir.BooleanValue | None Boolean filter for input values None

Returns

Name Type Description
Scalar Quantile of the input

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch()

Compute the approximate 0.50 quantile of bill_depth_mm.

>>> t.bill_depth_mm.approx_quantile(0.50)

┌────────┐
│ 17.318 │
└────────┘

Compute multiple approximate quantiles in one call - in this case the result is an array.

>>> t.bill_depth_mm.approx_quantile([0.25, 0.75])

┌────────────────────────┐
│ [15.565625, 18.671875] │
└────────────────────────┘

bucket

bucket(
    buckets,
    closed='left',
    close_extreme=True,
    include_under=False,
    include_over=False,
)

Compute a discrete binning of a numeric array.

Parameters

Name Type Description Default
buckets Sequence[int] List of buckets required
closed Literal['left', 'right'] Which side of each interval is closed. For example: python buckets = [0, 100, 200] closed = "left" # 100 falls in 2nd bucket closed = "right" # 100 falls in 1st bucket 'left'
close_extreme bool Whether the extreme values fall in the last bucket True
include_over bool Include values greater than the last bucket in the last bucket False
include_under bool Include values less than the first bucket in the first bucket False

Returns

Name Type Description
IntegerColumn A categorical column expression

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "values": [-1, 3, 5, 6, 8, 10, 11],
...     }
... )
>>> buckets = [0, 5, 10]
>>> t.mutate(
...     bucket_closed_left=t.values.bucket(buckets),
...     bucket_closed_right=t.values.bucket(buckets, closed="right"),
...     bucket_over_under=t.values.bucket(buckets, include_over=True, include_under=True),
... )
┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
┃ values  bucket_closed_left  bucket_closed_right  bucket_over_under ┃
┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
│ int64int8int8int8              │
├────────┼────────────────────┼─────────────────────┼───────────────────┤
│     -1NULLNULL0 │
│      3001 │
│      5102 │
│      6112 │
│      8112 │
│     10112 │
│     11NULLNULL3 │
└────────┴────────────────────┴─────────────────────┴───────────────────┘

corr

corr(right, where=None, how='sample')

Return the correlation of two numeric columns.

Parameters

Name Type Description Default
right NumericColumn Numeric column required
where ir.BooleanValue | None Filter None
how Literal['sample', 'pop'] Population or sample correlation 'sample'

Returns

Name Type Description
NumericScalar The correlation of left and right

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "left": [1, 3, 3, 4, 5, 7],
...         "right": [7, 5, 4, 3, 3, 1],
...     }
... )
>>> t.left.corr(t.right, how="pop")

┌────────┐
│ -0.968 │
└────────┘
>>> t.mutate(corr_col=t.left.corr(t.right, how="pop"))
┏━━━━━━━┳━━━━━━━┳━━━━━━━━━━┓
┃ left   right  corr_col ┃
┡━━━━━━━╇━━━━━━━╇━━━━━━━━━━┩
│ int64int64float64  │
├───────┼───────┼──────────┤
│     17-0.968 │
│     35-0.968 │
│     34-0.968 │
│     43-0.968 │
│     53-0.968 │
│     71-0.968 │
└───────┴───────┴──────────┘

cov

cov(right, where=None, how='sample')

Return the covariance of two numeric columns.

Parameters

Name Type Description Default
right NumericColumn Numeric column required
where ir.BooleanValue | None Filter None
how Literal['sample', 'pop'] Population or sample covariance 'sample'

Returns

Name Type Description
NumericScalar The covariance of self and right

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "left": [1, 3, 3, 4, 5, 7],
...         "right": [7, 5, 4, 3, 3, 1],
...     }
... )
>>> t.left.cov(t.right)

┌───────────┐
│ -4.033333 │
└───────────┘
>>> t.left.cov(t.right, how="pop")

┌───────────┐
│ -3.361111 │
└───────────┘
>>> t.mutate(cov_col=t.left.cov(t.right, how="pop"))
┏━━━━━━━┳━━━━━━━┳━━━━━━━━━━━┓
┃ left   right  cov_col   ┃
┡━━━━━━━╇━━━━━━━╇━━━━━━━━━━━┩
│ int64int64float64   │
├───────┼───────┼───────────┤
│     17-3.361111 │
│     35-3.361111 │
│     34-3.361111 │
│     43-3.361111 │
│     53-3.361111 │
│     71-3.361111 │
└───────┴───────┴───────────┘

cummean

cummean(where=None, group_by=None, order_by=None)

Return the cumulative mean of the input.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "id": [1, 2, 3, 4, 5, 6],
...         "grouper": ["a", "a", "a", "b", "b", "c"],
...         "values": [3, 2, 1, 2, 3, 2],
...     }
... )
>>> t.mutate(cummean=t.values.cummean()).order_by("id")
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┓
┃ id     grouper  values  cummean  ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━┩
│ int64stringint64float64  │
├───────┼─────────┼────────┼──────────┤
│     1a      33.000000 │
│     2a      22.500000 │
│     3a      12.000000 │
│     4b      22.000000 │
│     5b      32.200000 │
│     6c      22.166667 │
└───────┴─────────┴────────┴──────────┘
>>> t.mutate(cummean=t.values.cummean(where=t.grouper != "c", group_by="grouper")).order_by(
...     "id"
... )
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━┓
┃ id     grouper  values  cummean ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━┩
│ int64stringint64float64 │
├───────┼─────────┼────────┼─────────┤
│     1a      33.0 │
│     2a      22.5 │
│     3a      12.0 │
│     4b      22.0 │
│     5b      32.5 │
│     6c      2NULL │
└───────┴─────────┴────────┴─────────┘

cumsum

cumsum(where=None, group_by=None, order_by=None)

Return the cumulative sum of the input.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "id": [1, 2, 3, 4, 5, 6],
...         "grouper": ["a", "a", "a", "b", "b", "c"],
...         "values": [3, 2, 1, 2, 3, 2],
...     }
... )
>>> t.mutate(cumsum=t.values.cumsum())
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┓
┃ id     grouper  values  cumsum ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━┩
│ int64stringint64int64  │
├───────┼─────────┼────────┼────────┤
│     1a      33 │
│     2a      25 │
│     3a      16 │
│     4b      28 │
│     5b      311 │
│     6c      213 │
└───────┴─────────┴────────┴────────┘
>>> t.mutate(cumsum=t.values.cumsum(where=t.grouper != "c"))
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┓
┃ id     grouper  values  cumsum ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━┩
│ int64stringint64int64  │
├───────┼─────────┼────────┼────────┤
│     1a      33 │
│     2a      25 │
│     3a      16 │
│     4b      28 │
│     5b      311 │
│     6c      211 │
└───────┴─────────┴────────┴────────┘

histogram

histogram(nbins=None, binwidth=None, base=None, eps=1e-13)

Compute a histogram with fixed width bins.

Parameters

Name Type Description Default
nbins int | None If supplied, will be used to compute the binwidth None
binwidth float | None If not supplied, computed from the data (actual max and min values) None
base float | None The value of the first histogram bin. Defaults to the minimum value of column. None
eps float Allowed floating point epsilon for histogram base 1e-13

Returns

Name Type Description
Column Bucketed column

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "values": [-1, 3, 5, 6, 8, 10, 11, 23, 25],
...     }
... )

Compute a histogram with 5 bins.

>>> t.mutate(histogram=t.values.histogram(nbins=5))
┏━━━━━━━━┳━━━━━━━━━━━┓
┃ values  histogram ┃
┡━━━━━━━━╇━━━━━━━━━━━┩
│ int64int64     │
├────────┼───────────┤
│     -10 │
│      30 │
│      51 │
│      61 │
│      81 │
│     102 │
│     112 │
│     234 │
│     254 │
└────────┴───────────┘

Compute a histogram with a fixed bin width of 10.

>>> t.mutate(histogram=t.values.histogram(binwidth=10))
┏━━━━━━━━┳━━━━━━━━━━━┓
┃ values  histogram ┃
┡━━━━━━━━╇━━━━━━━━━━━┩
│ int64int64     │
├────────┼───────────┤
│     -10 │
│      30 │
│      50 │
│      60 │
│      80 │
│     101 │
│     111 │
│     232 │
│     252 │
└────────┴───────────┘

mean

mean(where=None)

Return the mean of a numeric column.

Parameters

Name Type Description Default
where ir.BooleanValue | None Filter None

Returns

Name Type Description
NumericScalar The mean of the input expression

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "id": [1, 2, 3, 4, 5, 6],
...         "grouper": ["a", "a", "a", "b", "b", "c"],
...         "values": [3, 2, 1, 2, 3, 2],
...     }
... )
>>> t.mutate(mean_col=t.values.mean())
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┓
┃ id     grouper  values  mean_col ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━┩
│ int64stringint64float64  │
├───────┼─────────┼────────┼──────────┤
│     1a      32.166667 │
│     2a      22.166667 │
│     3a      12.166667 │
│     4b      22.166667 │
│     5b      32.166667 │
│     6c      22.166667 │
└───────┴─────────┴────────┴──────────┘
>>> t.mutate(mean_col=t.values.mean(where=t.grouper != "c"))
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┓
┃ id     grouper  values  mean_col ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━┩
│ int64stringint64float64  │
├───────┼─────────┼────────┼──────────┤
│     1a      32.2 │
│     2a      22.2 │
│     3a      12.2 │
│     4b      22.2 │
│     5b      32.2 │
│     6c      22.2 │
└───────┴─────────┴────────┴──────────┘

std

std(where=None, how='sample')

Return the standard deviation of a numeric column.

Parameters

Name Type Description Default
where ir.BooleanValue | None Filter None
how Literal['sample', 'pop'] Sample or population standard deviation 'sample'

Returns

Name Type Description
NumericScalar Standard deviation of arg

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "values": [1, 3, 3, 4, 5, 7],
...     }
... )
>>> t.values.std()

┌──────────┐
│ 2.041241 │
└──────────┘
>>> t.mutate(std_col=t.values.std())
┏━━━━━━━━┳━━━━━━━━━━┓
┃ values  std_col  ┃
┡━━━━━━━━╇━━━━━━━━━━┩
│ int64float64  │
├────────┼──────────┤
│      12.041241 │
│      32.041241 │
│      32.041241 │
│      42.041241 │
│      52.041241 │
│      72.041241 │
└────────┴──────────┘

sum

sum(where=None)

Return the sum of a numeric column.

Parameters

Name Type Description Default
where ir.BooleanValue | None Filter None

Returns

Name Type Description
NumericScalar The sum of the input expression

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "id": [1, 2, 3, 4, 5, 6],
...         "grouper": ["a", "a", "a", "b", "b", "c"],
...         "values": [3, 2, 1, 2, 3, 2],
...     }
... )
>>> t.mutate(sum_col=t.values.sum())
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━┓
┃ id     grouper  values  sum_col ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━┩
│ int64stringint64int64   │
├───────┼─────────┼────────┼─────────┤
│     1a      313 │
│     2a      213 │
│     3a      113 │
│     4b      213 │
│     5b      313 │
│     6c      213 │
└───────┴─────────┴────────┴─────────┘
>>> t.mutate(sum_col=t.values.sum(where=t.grouper != "c"))
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━┓
┃ id     grouper  values  sum_col ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━┩
│ int64stringint64int64   │
├───────┼─────────┼────────┼─────────┤
│     1a      311 │
│     2a      211 │
│     3a      111 │
│     4b      211 │
│     5b      311 │
│     6c      211 │
└───────┴─────────┴────────┴─────────┘

var

var(where=None, how='sample')

Return the variance of a numeric column.

Parameters

Name Type Description Default
where ir.BooleanValue | None Filter None
how Literal['sample', 'pop'] Sample or population variance 'sample'

Returns

Name Type Description
NumericScalar Standard deviation of arg

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "values": [1, 3, 3, 4, 5, 7],
...     }
... )
>>> t.values.var()

┌──────────┐
│ 4.166667 │
└──────────┘
>>> t.mutate(var_col=t.values.var())
┏━━━━━━━━┳━━━━━━━━━━┓
┃ values  var_col  ┃
┡━━━━━━━━╇━━━━━━━━━━┩
│ int64float64  │
├────────┼──────────┤
│      14.166667 │
│      34.166667 │
│      34.166667 │
│      44.166667 │
│      54.166667 │
│      74.166667 │
└────────┴──────────┘

IntegerValue

IntegerValue(self, arg)

Methods

Name Description
as_interval Convert an integer to an interval.
as_timestamp Convert an integral UNIX timestamp to a timestamp expression.
convert_base Convert an integer from one base to another.

as_interval

as_interval(unit='s')

Convert an integer to an interval.

Parameters

Name Type Description Default
unit Literal['Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'] Unit for the resulting interval 's'

Returns

Name Type Description
IntervalValue An interval in units of unit

Examples

>>> from datetime import datetime
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "timestamp_col": [
...             datetime(2024, 1, 1, 0, 0, 0),
...             datetime(2024, 1, 1, 0, 0, 0),
...             datetime(2024, 1, 1, 0, 0, 0),
...         ],
...         "int_col": [1, 2, 3],
...     }
... )
>>> t.int_col.as_interval("h")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ IntervalFromInteger(int_col, HOUR)                         ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ interval('h')                                              │
├────────────────────────────────────────────────────────────┤
│ MonthDayNano(months=0, days=0, nanoseconds=3600000000000)  │
│ MonthDayNano(months=0, days=0, nanoseconds=7200000000000)  │
│ MonthDayNano(months=0, days=0, nanoseconds=10800000000000) │
└────────────────────────────────────────────────────────────┘
>>> t.mutate(timestamp_added_col=t.timestamp_col + t.int_col.as_interval("h"))
┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓
┃ timestamp_col        int_col  timestamp_added_col ┃
┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━┩
│ timestampint64timestamp           │
├─────────────────────┼─────────┼─────────────────────┤
│ 2024-01-01 00:00:0012024-01-01 01:00:00 │
│ 2024-01-01 00:00:0022024-01-01 02:00:00 │
│ 2024-01-01 00:00:0032024-01-01 03:00:00 │
└─────────────────────┴─────────┴─────────────────────┘

as_timestamp

as_timestamp(unit)

Convert an integral UNIX timestamp to a timestamp expression.

Parameters

Name Type Description Default
unit Literal['s', 'ms', 'us'] The resolution of arg required

Returns

Name Type Description
TimestampValue self converted to a timestamp

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
...     {
...         "int_col": [0, 1730501716, 2147483647],
...     }
... )
>>> t.int_col.as_timestamp("s")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ TimestampFromUNIX(int_col, SECOND) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ timestamp                          │
├────────────────────────────────────┤
│ 1970-01-01 00:00:00                │
│ 2024-11-01 22:55:16                │
│ 2038-01-19 03:14:07                │
└────────────────────────────────────┘

convert_base

convert_base(from_base, to_base)

Convert an integer from one base to another.

Parameters

Name Type Description Default
from_base IntegerValue Numeric base of expression required
to_base IntegerValue New base required

Returns

Name Type Description
IntegerValue Converted expression

IntegerColumn

IntegerColumn(self, arg)

Methods

Name Description
bit_and Aggregate the column using the bitwise and operator.
bit_or Aggregate the column using the bitwise or operator.
bit_xor Aggregate the column using the bitwise exclusive or operator.

bit_and

bit_and(where=None)

Aggregate the column using the bitwise and operator.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"x": [-1, 0, 1]})
>>> t.x.bit_and()

┌───┐
│ 0 │
└───┘
>>> t.x.bit_and(where=t.x != 0)

┌───┐
│ 1 │
└───┘

bit_or

bit_or(where=None)

Aggregate the column using the bitwise or operator.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"x": [-1, 0, 1]})
>>> t.x.bit_or()

┌────┐
│ -1 │
└────┘
>>> t.x.bit_or(where=t.x >= 0)

┌───┐
│ 1 │
└───┘

bit_xor

bit_xor(where=None)

Aggregate the column using the bitwise exclusive or operator.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"x": [-1, 0, 1]})
>>> t.x.bit_xor()

┌────┐
│ -2 │
└────┘
>>> t.x.bit_xor(where=t.x >= 0)

┌───┐
│ 1 │
└───┘

FloatingValue

FloatingValue(self, arg)

Methods

Name Description
isinf Return whether the value is infinity.
isnan Return whether the value is NaN.

isinf

isinf()

Return whether the value is infinity.

isnan

isnan()

Return whether the value is NaN.

DecimalValue

DecimalValue(self, arg)

BooleanValue

BooleanValue(self, arg)

Methods

Name Description
ifelse Construct a ternary conditional expression.
negate DEPRECATED.

ifelse

ifelse(true_expr, false_expr)

Construct a ternary conditional expression.

Parameters

Name Type Description Default
true_expr ir.Value Expression to return if self evaluates to True required
false_expr ir.Value Expression to return if self evaluates to False or NULL required

Returns

Name Type Description
Value The value of true_expr if arg is True else false_expr

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"is_person": [True, False, True, None]})
>>> t.is_person.ifelse("yes", "no")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ IfElse(is_person, 'yes', 'no') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string                         │
├────────────────────────────────┤
│ yes                            │
│ no                             │
│ yes                            │
│ no                             │
└────────────────────────────────┘

negate

negate()

DEPRECATED.

BooleanColumn

BooleanColumn(self, arg)

Methods

Name Description
all Return whether all elements are True.
any Return whether at least one element is True.
cumall Accumulate the all aggregate.
cumany Accumulate the any aggregate.
notall Return whether not all elements are True.
notany Return whether no elements are True.

all

all(where=None)

Return whether all elements are True.

Parameters

Name Type Description Default
where BooleanValue | None Optional filter for the aggregation None

Returns

Name Type Description
BooleanValue Whether all elements are True

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"arr": [1, 2, 3, 4]})
>>> (t.arr >= 1).all()

┌──────┐
│ True │
└──────┘
>>> (t.arr > 2).all()

┌───────┐
│ False │
└───────┘
>>> (t.arr == 2).all(where=t.arr == 2)

┌──────┐
│ True │
└──────┘
>>> (t.arr == 2).all(where=t.arr >= 2)

┌───────┐
│ False │
└───────┘

any

any(where=None)

Return whether at least one element is True.

If the expression does not reference any foreign tables, the result will be a scalar reduction, otherwise it will be a deferred expression constructing an exists subquery when passed to a table method.

Parameters

Name Type Description Default
where BooleanValue | None Optional filter for the aggregation None

Returns

Name Type Description
BooleanValue Whether at least one element is True.

Notes

Consider the following ibis expressions

import ibis

t = ibis.table(dict(a="string"))
s = ibis.table(dict(a="string"))

cond = (t.a == s.a).any()

Without knowing the table to use as the outer query there are two ways to turn this expression into a SQL EXISTS predicate, depending on which of t or s is filtered on.

Filtering from t:

SELECT *
FROM t
WHERE EXISTS (SELECT 1 FROM s WHERE t.a = s.a)

Filtering from s:

SELECT *
FROM s
WHERE EXISTS (SELECT 1 FROM t WHERE t.a = s.a)

Notably the correlated subquery cannot stand on its own.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"arr": [1, 2, 3, None]})
>>> (t.arr > 2).any()

┌──────┐
│ True │
└──────┘
>>> (t.arr > 4).any()

┌───────┐
│ False │
└───────┘
>>> (t.arr == None).any(where=t.arr != None)

┌───────┐
│ False │
└───────┘

cumall

cumall(where=None, group_by=None, order_by=None)

Accumulate the all aggregate.

Returns

Name Type Description
BooleanColumn A boolean column with the cumulative all aggregate.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"arr": [1, 2, 3, 4]})
>>> ((t.arr > 1) & (t.arr >= 1)).cumall()
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ All(And(Greater(arr, 1), GreaterEqual(arr, 1))) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean                                         │
├─────────────────────────────────────────────────┤
│ False                                           │
│ False                                           │
│ False                                           │
│ False                                           │
└─────────────────────────────────────────────────┘
>>> ((t.arr > 0) & (t.arr >= 1)).cumall()
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ All(And(Greater(arr, 0), GreaterEqual(arr, 1))) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean                                         │
├─────────────────────────────────────────────────┤
│ True                                            │
│ True                                            │
│ True                                            │
│ True                                            │
└─────────────────────────────────────────────────┘

cumany

cumany(where=None, group_by=None, order_by=None)

Accumulate the any aggregate.

Returns

Name Type Description
BooleanColumn A boolean column with the cumulative any aggregate.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"arr": [1, 2, 3, 4]})
>>> ((t.arr > 1) | (t.arr >= 1)).cumany()
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Any(Or(Greater(arr, 1), GreaterEqual(arr, 1))) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean                                        │
├────────────────────────────────────────────────┤
│ True                                           │
│ True                                           │
│ True                                           │
│ True                                           │
└────────────────────────────────────────────────┘
>>> ((t.arr > 1) & (t.arr >= 1)).cumany()
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Any(And(Greater(arr, 1), GreaterEqual(arr, 1))) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean                                         │
├─────────────────────────────────────────────────┤
│ False                                           │
│ True                                            │
│ True                                            │
│ True                                            │
└─────────────────────────────────────────────────┘

notall

notall(where=None)

Return whether not all elements are True.

Parameters

Name Type Description Default
where BooleanValue | None Optional filter for the aggregation None

Returns

Name Type Description
BooleanValue Whether not all elements are True

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"arr": [1, 2, 3, 4]})
>>> (t.arr >= 1).notall()

┌───────┐
│ False │
└───────┘
>>> (t.arr > 2).notall()

┌──────┐
│ True │
└──────┘
>>> (t.arr == 2).notall(where=t.arr == 2)

┌───────┐
│ False │
└───────┘
>>> (t.arr == 2).notall(where=t.arr >= 2)

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

notany

notany(where=None)

Return whether no elements are True.

Parameters

Name Type Description Default
where BooleanValue | None Optional filter for the aggregation None

Returns

Name Type Description
BooleanValue Whether no elements are True.

Examples

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"arr": [1, 2, 3, 4]})
>>> (t.arr > 1).notany()

┌───────┐
│ False │
└───────┘
>>> (t.arr > 4).notany()

┌──────┐
│ True │
└──────┘
>>> m = ibis.memtable({"arr": [True, True, True, False]})
>>> (t.arr == None).notany(where=t.arr != None)

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

and_

ibis.and_(*predicates)

Combine multiple predicates using &.

Parameters

Name Type Description Default
predicates ir.BooleanValue Boolean value expressions ()

Returns

Name Type Description
BooleanValue A new predicate that evaluates to True if all composing predicates are True. If no predicates were provided, returns True.

or_

ibis.or_(*predicates)

Combine multiple predicates using |.

Parameters

Name Type Description Default
predicates ir.BooleanValue Boolean value expressions ()

Returns

Name Type Description
BooleanValue A new predicate that evaluates to True if any composing predicates are True. If no predicates were provided, returns False.

random

ibis.random()

Return a random floating point number in the range [0.0, 1.0).

Similar to random.random in the Python standard library.

Repeated use of random

ibis.random() will generate a column of distinct random numbers even if the same instance of ibis.random() is re-used.

When Ibis compiles an expression to SQL, each place where random is used will render as a separate call to the given backend’s random number generator.

>>> from ibis.interactive import *
>>> t = ibis.memtable({"a": range(5)})
>>> r_a = ibis.random()
>>> t.mutate(random_1=r_a, random_2=r_a)  # doctest: +SKIP
┏━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┓
┃ a     ┃ random_1 ┃ random_2 ┃
┡━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━┩
│ int64 │ float64  │ float64  │
├───────┼──────────┼──────────┤
00.1911300.098715
10.2552620.828454
20.0118040.392275
30.3099410.347300
40.4827830.095562
└───────┴──────────┴──────────┘

Returns

Name Type Description
FloatingScalar Random float value expression

e

e

pi

pi

Back to top