Skip to content

Base Expression Types

These APIs are shared by both table and column expressions.

Expr

Bases: Immutable

Base expression class.

Source code in ibis/expr/types/core.py
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
@public
class Expr(Immutable):
    """Base expression class."""

    __slots__ = ("_arg",)

    def __init__(self, arg: ops.Node) -> None:
        object.__setattr__(self, "_arg", arg)

    def __repr__(self) -> str:
        from ibis.expr.types.pretty import simple_console

        if not options.interactive:
            return self._repr()

        with simple_console.capture() as capture:
            try:
                simple_console.print(self)
            except TranslationError as e:
                lines = [
                    "Translation to backend failed",
                    f"Error message: {e.args[0]}",
                    "Expression repr follows:",
                    self._repr(),
                ]
                return "\n".join(lines)
        return capture.get()

    def __reduce__(self):
        return (self.__class__, (self._arg,))

    def __hash__(self):
        return hash((self.__class__, self._arg))

    def _repr(self) -> str:
        from ibis.expr.format import fmt

        return fmt(self)

    def equals(self, other):
        """Return whether this expression is _structurally_ equivalent to `other`.

        If you want to produce an equality expression, use `==` syntax.

        Parameters
        ----------
        other
            Another expression

        Examples
        --------
        >>> import ibis
        >>> t1 = ibis.table(dict(a="int"), name="t")
        >>> t2 = ibis.table(dict(a="int"), name="t")
        >>> t1.equals(t2)
        True
        >>> v = ibis.table(dict(a="string"), name="v")
        >>> t1.equals(v)
        False
        """
        if not isinstance(other, Expr):
            raise TypeError(
                f"invalid equality comparison between Expr and {type(other)}"
            )
        return self._arg.equals(other._arg)

    def __bool__(self) -> bool:
        raise ValueError("The truth value of an Ibis expression is not defined")

    __nonzero__ = __bool__

    def has_name(self):
        """Check whether this expression has an explicit name."""
        return isinstance(self._arg, ops.Named)

    def get_name(self):
        """Return the name of this expression."""
        return self._arg.name

    def _repr_png_(self) -> bytes | None:
        if options.interactive or not options.graphviz_repr:
            return None
        try:
            import ibis.expr.visualize as viz
        except ImportError:
            return None
        else:
            # Something may go wrong, and we can't error in the notebook
            # so fallback to the default text representation.
            with contextlib.suppress(Exception):
                return viz.to_graph(self).pipe(format='png')

    def visualize(
        self,
        format: str = "svg",
        *,
        label_edges: bool = False,
        verbose: bool = False,
    ) -> None:
        """Visualize an expression as a GraphViz graph in the browser.

        Parameters
        ----------
        format
            Image output format. These are specified by the ``graphviz`` Python
            library.
        label_edges
            Show operation input names as edge labels
        verbose
            Print the graphviz DOT code to stderr if [`True`][True]

        Raises
        ------
        ImportError
            If ``graphviz`` is not installed.
        """
        import ibis.expr.visualize as viz

        path = viz.draw(
            viz.to_graph(self, label_edges=label_edges),
            format=format,
            verbose=verbose,
        )
        webbrowser.open(f'file://{os.path.abspath(path)}')

    def pipe(self, f, *args: Any, **kwargs: Any) -> Expr:
        """Compose `f` with `self`.

        Parameters
        ----------
        f
            If the expression needs to be passed as anything other than the
            first argument to the function, pass a tuple with the argument
            name. For example, (f, 'data') if the function f expects a 'data'
            keyword
        args
            Positional arguments to `f`
        kwargs
            Keyword arguments to `f`

        Examples
        --------
        >>> import ibis
        >>> t = ibis.table([('a', 'int64'), ('b', 'string')], name='t')
        >>> f = lambda a: (a + 1).name('a')
        >>> g = lambda a: (a * 2).name('a')
        >>> result1 = t.a.pipe(f).pipe(g)
        >>> result1
        r0 := UnboundTable: t
          a int64
          b string
        a: r0.a + 1 * 2

        >>> result2 = g(f(t.a))  # equivalent to the above
        >>> result1.equals(result2)
        True

        Returns
        -------
        Expr
            Result type of passed function
        """
        if isinstance(f, tuple):
            f, data_keyword = f
            kwargs = kwargs.copy()
            kwargs[data_keyword] = self
            return f(*args, **kwargs)
        else:
            return f(self, *args, **kwargs)

    def op(self) -> ops.Node:  # noqa: D102
        return self._arg

    def _find_backends(self) -> tuple[list[BaseBackend], bool]:
        """Return the possible backends for an expression.

        Returns
        -------
        list[BaseBackend]
            A list of the backends found.
        """
        backends = set()
        has_unbound = False
        node_types = (ops.DatabaseTable, ops.SQLQueryResult, ops.UnboundTable)
        for table in self.op().find(node_types):
            if isinstance(table, ops.UnboundTable):
                has_unbound = True
            else:
                backends.add(table.source)

        return list(backends), has_unbound

    def _find_backend(self, *, use_default: bool = False) -> BaseBackend:
        """Find the backend attached to an expression.

        Parameters
        ----------
        use_default
            If [`True`][True] and the default backend isn't set, initialize the
            default backend and use that. This should only be set to `True` for
            `.execute()`. For other contexts such as compilation, this option
            doesn't make sense so the default value is [`False`][False].

        Returns
        -------
        BaseBackend
            A backend that is attached to the expression
        """
        backends, has_unbound = self._find_backends()

        if not backends:
            if has_unbound:
                raise IbisError(
                    "Expression contains unbound tables and therefore cannot "
                    "be executed. Use ibis.<backend>.execute(expr) or "
                    "assign a backend instance to "
                    "`ibis.options.default_backend`."
                )
            default = _default_backend() if use_default else None
            if default is None:
                raise IbisError(
                    'Expression depends on no backends, and found no default'
                )
            return default

        if len(backends) > 1:
            raise IbisError('Multiple backends found for this expression')

        return backends[0]

    def execute(
        self,
        limit: int | str | None = 'default',
        timecontext: TimeContext | None = None,
        params: Mapping[ir.Value, Any] | None = None,
        **kwargs: Any,
    ):
        """Execute an expression against its backend if one exists.

        Parameters
        ----------
        limit
            An integer to effect a specific row limit. A value of `None` means
            "no limit". The default is in `ibis/config.py`.
        timecontext
            Defines a time range of `(begin, end)`. When defined, the execution
            will only compute result for data inside the time range. The time
            range is inclusive of both endpoints. This is conceptually same as
            a time filter.
            The time column must be named `'time'` and should preserve
            across the expression. For example, if that column is dropped then
            execute will result in an error.
        params
            Mapping of scalar parameter expressions to value
        kwargs
            Keyword arguments
        """
        return self._find_backend(use_default=True).execute(
            self, limit=limit, timecontext=timecontext, params=params, **kwargs
        )

    def compile(
        self,
        limit: int | None = None,
        timecontext: TimeContext | None = None,
        params: Mapping[ir.Value, Any] | None = None,
    ):
        """Compile to an execution target.

        Parameters
        ----------
        limit
            An integer to effect a specific row limit. A value of `None` means
            "no limit". The default is in `ibis/config.py`.
        timecontext
            Defines a time range of `(begin, end)`. When defined, the execution
            will only compute result for data inside the time range. The time
            range is inclusive of both endpoints. This is conceptually same as
            a time filter.
            The time column must be named `'time'` and should preserve
            across the expression. For example, if that column is dropped then
            execute will result in an error.
        params
            Mapping of scalar parameter expressions to value
        """
        return self._find_backend().compile(
            self, limit=limit, timecontext=timecontext, params=params
        )

    @experimental
    def to_pyarrow_batches(
        self,
        *,
        limit: int | str | None = None,
        params: Mapping[ir.Value, Any] | None = None,
        chunk_size: int = 1_000_000,
        **kwargs: Any,
    ) -> pa.ipc.RecordBatchReader:
        """Execute expression and return a RecordBatchReader.

        This method is eager and will execute the associated expression
        immediately.

        Parameters
        ----------
        limit
            An integer to effect a specific row limit. A value of `None` means
            "no limit". The default is in `ibis/config.py`.
        params
            Mapping of scalar parameter expressions to value.
        chunk_size
            Maximum number of rows in each returned record batch.
        kwargs
            Keyword arguments

        Returns
        -------
        results
            RecordBatchReader
        """
        return self._find_backend(use_default=True).to_pyarrow_batches(
            self,
            params=params,
            limit=limit,
            chunk_size=chunk_size,
            **kwargs,
        )

    @experimental
    def to_pyarrow(
        self,
        *,
        params: Mapping[ir.Scalar, Any] | None = None,
        limit: int | str | None = None,
        **kwargs: Any,
    ) -> pa.Table:
        """Execute expression and return results in as a pyarrow table.

        This method is eager and will execute the associated expression
        immediately.

        Parameters
        ----------
        params
            Mapping of scalar parameter expressions to value.
        limit
            An integer to effect a specific row limit. A value of `None` means
            "no limit". The default is in `ibis/config.py`.
        kwargs
            Keyword arguments

        Returns
        -------
        Table
            A pyarrow table holding the results of the executed expression.
        """
        return self._find_backend(use_default=True).to_pyarrow(
            self, params=params, limit=limit, **kwargs
        )

    @experimental
    def to_parquet(
        self,
        path: str | Path,
        *,
        params: Mapping[ir.Scalar, Any] | None = None,
        **kwargs: Any,
    ) -> None:
        """Write the results of executing the given expression to a parquet file

        This method is eager and will execute the associated expression
        immediately.

        Parameters
        ----------
        path
            The data source. A string or Path to the parquet file.
        params
            Mapping of scalar parameter expressions to value.
        **kwargs
            Additional keyword arguments passed to pyarrow.parquet.ParquetWriter

        https://arrow.apache.org/docs/python/generated/pyarrow.parquet.ParquetWriter.html

        Examples
        --------
        Write out an expression to a single parquet file.

        >>> import ibis
        >>> penguins = ibis.examples.penguins.fetch()
        >>> penguins.to_parquet("penguins.parquet")  # doctest: +SKIP

        Write out an expression to a hive-partitioned parquet file.

        >>> import ibis
        >>> penguins = ibis.examples.penguins.fetch()
        >>> # partition on single column
        >>> penguins.to_parquet("penguins_hive_dir", partition_by="year")  # doctest: +SKIP
        >>> # partition on multiple columns
        >>> penguins.to_parquet("penguins_hive_dir", partition_by=("year", "island"))  # doctest: +SKIP

        !!! note "Hive-partitioned output is currently only supported when using DuckDB"
        """
        self._find_backend(use_default=True).to_parquet(self, path, **kwargs)

    @experimental
    def to_csv(
        self,
        path: str | Path,
        *,
        params: Mapping[ir.Scalar, Any] | None = None,
        **kwargs: Any,
    ) -> None:
        """Write the results of executing the given expression to a CSV file

        This method is eager and will execute the associated expression
        immediately.

        Parameters
        ----------
        path
            The data source. A string or Path to the CSV file.
        params
            Mapping of scalar parameter expressions to value.
        **kwargs
            Additional keyword arguments passed to pyarrow.csv.CSVWriter

        https://arrow.apache.org/docs/python/generated/pyarrow.csv.CSVWriter.html
        """
        self._find_backend(use_default=True).to_csv(self, path, **kwargs)

    @experimental
    def to_delta(
        self,
        path: str | Path,
        *,
        params: Mapping[ir.Scalar, Any] | None = None,
        **kwargs: Any,
    ) -> None:
        """Write the results of executing the given expression to a Delta Lake table

        This method is eager and will execute the associated expression
        immediately.

        Parameters
        ----------
        path
            The data source. A string or Path to the Delta Lake table directory.
        params
            Mapping of scalar parameter expressions to value.
        **kwargs
            Additional keyword arguments passed to pyarrow.csv.CSVWriter

        https://arrow.apache.org/docs/python/generated/pyarrow.csv.CSVWriter.html
        """
        self._find_backend(use_default=True).to_delta(self, path, **kwargs)

    @experimental
    def to_torch(
        self,
        *,
        params: Mapping[ir.Scalar, Any] | None = None,
        limit: int | str | None = None,
        **kwargs: Any,
    ) -> dict[str, torch.Tensor]:
        """Execute an expression and return results as a dictionary of torch tensors.

        Parameters
        ----------
        params
            Parameters to substitute into the expression.
        limit
            An integer to effect a specific row limit. A value of `None` means no limit.
        kwargs
            Keyword arguments passed into the backend's `to_torch` implementation.

        Returns
        -------
        dict[str, torch.Tensor]
            A dictionary of torch tensors, keyed by column name.
        """
        return self._find_backend(use_default=True).to_torch(
            self, params=params, limit=limit, **kwargs
        )

    def unbind(self) -> ir.Table:
        """Return an expression built on `UnboundTable` instead of backend-specific objects."""
        from ibis.expr.analysis import substitute_unbound

        return substitute_unbound(self.op()).to_expr()

    def as_table(self) -> ir.Table:
        """Convert an expression to a table."""
        raise NotImplementedError(type(self))

Functions

as_table()

Convert an expression to a table.

Source code in ibis/expr/types/core.py
528
529
530
def as_table(self) -> ir.Table:
    """Convert an expression to a table."""
    raise NotImplementedError(type(self))

compile(limit=None, timecontext=None, params=None)

Compile to an execution target.

Parameters:

Name Type Description Default
limit int | None

An integer to effect a specific row limit. A value of None means "no limit". The default is in ibis/config.py.

None
timecontext TimeContext | None

Defines a time range of (begin, end). When defined, the execution will only compute result for data inside the time range. The time range is inclusive of both endpoints. This is conceptually same as a time filter. The time column must be named 'time' and should preserve across the expression. For example, if that column is dropped then execute will result in an error.

None
params Mapping[ir.Value, Any] | None

Mapping of scalar parameter expressions to value

None
Source code in ibis/expr/types/core.py
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
def compile(
    self,
    limit: int | None = None,
    timecontext: TimeContext | None = None,
    params: Mapping[ir.Value, Any] | None = None,
):
    """Compile to an execution target.

    Parameters
    ----------
    limit
        An integer to effect a specific row limit. A value of `None` means
        "no limit". The default is in `ibis/config.py`.
    timecontext
        Defines a time range of `(begin, end)`. When defined, the execution
        will only compute result for data inside the time range. The time
        range is inclusive of both endpoints. This is conceptually same as
        a time filter.
        The time column must be named `'time'` and should preserve
        across the expression. For example, if that column is dropped then
        execute will result in an error.
    params
        Mapping of scalar parameter expressions to value
    """
    return self._find_backend().compile(
        self, limit=limit, timecontext=timecontext, params=params
    )

equals(other)

Return whether this expression is structurally equivalent to other.

If you want to produce an equality expression, use == syntax.

Parameters:

Name Type Description Default
other

Another expression

required

Examples:

>>> import ibis
>>> t1 = ibis.table(dict(a="int"), name="t")
>>> t2 = ibis.table(dict(a="int"), name="t")
>>> t1.equals(t2)
True
>>> v = ibis.table(dict(a="string"), name="v")
>>> t1.equals(v)
False
Source code in ibis/expr/types/core.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def equals(self, other):
    """Return whether this expression is _structurally_ equivalent to `other`.

    If you want to produce an equality expression, use `==` syntax.

    Parameters
    ----------
    other
        Another expression

    Examples
    --------
    >>> import ibis
    >>> t1 = ibis.table(dict(a="int"), name="t")
    >>> t2 = ibis.table(dict(a="int"), name="t")
    >>> t1.equals(t2)
    True
    >>> v = ibis.table(dict(a="string"), name="v")
    >>> t1.equals(v)
    False
    """
    if not isinstance(other, Expr):
        raise TypeError(
            f"invalid equality comparison between Expr and {type(other)}"
        )
    return self._arg.equals(other._arg)

execute(limit='default', timecontext=None, params=None, **kwargs)

Execute an expression against its backend if one exists.

Parameters:

Name Type Description Default
limit int | str | None

An integer to effect a specific row limit. A value of None means "no limit". The default is in ibis/config.py.

'default'
timecontext TimeContext | None

Defines a time range of (begin, end). When defined, the execution will only compute result for data inside the time range. The time range is inclusive of both endpoints. This is conceptually same as a time filter. The time column must be named 'time' and should preserve across the expression. For example, if that column is dropped then execute will result in an error.

None
params Mapping[ir.Value, Any] | None

Mapping of scalar parameter expressions to value

None
kwargs Any

Keyword arguments

{}
Source code in ibis/expr/types/core.py
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
def execute(
    self,
    limit: int | str | None = 'default',
    timecontext: TimeContext | None = None,
    params: Mapping[ir.Value, Any] | None = None,
    **kwargs: Any,
):
    """Execute an expression against its backend if one exists.

    Parameters
    ----------
    limit
        An integer to effect a specific row limit. A value of `None` means
        "no limit". The default is in `ibis/config.py`.
    timecontext
        Defines a time range of `(begin, end)`. When defined, the execution
        will only compute result for data inside the time range. The time
        range is inclusive of both endpoints. This is conceptually same as
        a time filter.
        The time column must be named `'time'` and should preserve
        across the expression. For example, if that column is dropped then
        execute will result in an error.
    params
        Mapping of scalar parameter expressions to value
    kwargs
        Keyword arguments
    """
    return self._find_backend(use_default=True).execute(
        self, limit=limit, timecontext=timecontext, params=params, **kwargs
    )

get_name()

Return the name of this expression.

Source code in ibis/expr/types/core.py
112
113
114
def get_name(self):
    """Return the name of this expression."""
    return self._arg.name

has_name()

Check whether this expression has an explicit name.

Source code in ibis/expr/types/core.py
108
109
110
def has_name(self):
    """Check whether this expression has an explicit name."""
    return isinstance(self._arg, ops.Named)

pipe(f, *args, **kwargs)

Compose f with self.

Parameters:

Name Type Description Default
f

If the expression needs to be passed as anything other than the first argument to the function, pass a tuple with the argument name. For example, (f, 'data') if the function f expects a 'data' keyword

required
args Any

Positional arguments to f

()
kwargs Any

Keyword arguments to f

{}

Examples:

>>> import ibis
>>> t = ibis.table([('a', 'int64'), ('b', 'string')], name='t')
>>> f = lambda a: (a + 1).name('a')
>>> g = lambda a: (a * 2).name('a')
>>> result1 = t.a.pipe(f).pipe(g)
>>> result1
r0 := UnboundTable: t
  a int64
  b string
a: r0.a + 1 * 2
>>> result2 = g(f(t.a))  # equivalent to the above
>>> result1.equals(result2)
True

Returns:

Type Description
Expr

Result type of passed function

Source code in ibis/expr/types/core.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def pipe(self, f, *args: Any, **kwargs: Any) -> Expr:
    """Compose `f` with `self`.

    Parameters
    ----------
    f
        If the expression needs to be passed as anything other than the
        first argument to the function, pass a tuple with the argument
        name. For example, (f, 'data') if the function f expects a 'data'
        keyword
    args
        Positional arguments to `f`
    kwargs
        Keyword arguments to `f`

    Examples
    --------
    >>> import ibis
    >>> t = ibis.table([('a', 'int64'), ('b', 'string')], name='t')
    >>> f = lambda a: (a + 1).name('a')
    >>> g = lambda a: (a * 2).name('a')
    >>> result1 = t.a.pipe(f).pipe(g)
    >>> result1
    r0 := UnboundTable: t
      a int64
      b string
    a: r0.a + 1 * 2

    >>> result2 = g(f(t.a))  # equivalent to the above
    >>> result1.equals(result2)
    True

    Returns
    -------
    Expr
        Result type of passed function
    """
    if isinstance(f, tuple):
        f, data_keyword = f
        kwargs = kwargs.copy()
        kwargs[data_keyword] = self
        return f(*args, **kwargs)
    else:
        return f(self, *args, **kwargs)

to_csv(path, *, params=None, **kwargs)

Write the results of executing the given expression to a CSV file

This method is eager and will execute the associated expression immediately.

Parameters:

Name Type Description Default
path str | Path

The data source. A string or Path to the CSV file.

required
params Mapping[ir.Scalar, Any] | None

Mapping of scalar parameter expressions to value.

None
**kwargs Any

Additional keyword arguments passed to pyarrow.csv.CSVWriter

{}
Source code in ibis/expr/types/core.py
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
@experimental
def to_csv(
    self,
    path: str | Path,
    *,
    params: Mapping[ir.Scalar, Any] | None = None,
    **kwargs: Any,
) -> None:
    """Write the results of executing the given expression to a CSV file

    This method is eager and will execute the associated expression
    immediately.

    Parameters
    ----------
    path
        The data source. A string or Path to the CSV file.
    params
        Mapping of scalar parameter expressions to value.
    **kwargs
        Additional keyword arguments passed to pyarrow.csv.CSVWriter

    https://arrow.apache.org/docs/python/generated/pyarrow.csv.CSVWriter.html
    """
    self._find_backend(use_default=True).to_csv(self, path, **kwargs)

to_delta(path, *, params=None, **kwargs)

Write the results of executing the given expression to a Delta Lake table

This method is eager and will execute the associated expression immediately.

Parameters:

Name Type Description Default
path str | Path

The data source. A string or Path to the Delta Lake table directory.

required
params Mapping[ir.Scalar, Any] | None

Mapping of scalar parameter expressions to value.

None
**kwargs Any

Additional keyword arguments passed to pyarrow.csv.CSVWriter

{}
Source code in ibis/expr/types/core.py
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
@experimental
def to_delta(
    self,
    path: str | Path,
    *,
    params: Mapping[ir.Scalar, Any] | None = None,
    **kwargs: Any,
) -> None:
    """Write the results of executing the given expression to a Delta Lake table

    This method is eager and will execute the associated expression
    immediately.

    Parameters
    ----------
    path
        The data source. A string or Path to the Delta Lake table directory.
    params
        Mapping of scalar parameter expressions to value.
    **kwargs
        Additional keyword arguments passed to pyarrow.csv.CSVWriter

    https://arrow.apache.org/docs/python/generated/pyarrow.csv.CSVWriter.html
    """
    self._find_backend(use_default=True).to_delta(self, path, **kwargs)

to_parquet(path, *, params=None, **kwargs)

Write the results of executing the given expression to a parquet file

This method is eager and will execute the associated expression immediately.

Parameters:

Name Type Description Default
path str | Path

The data source. A string or Path to the parquet file.

required
params Mapping[ir.Scalar, Any] | None

Mapping of scalar parameter expressions to value.

None
**kwargs Any

Additional keyword arguments passed to pyarrow.parquet.ParquetWriter

{}

Examples:

Write out an expression to a single parquet file.

>>> import ibis
>>> penguins = ibis.examples.penguins.fetch()
>>> penguins.to_parquet("penguins.parquet")

Write out an expression to a hive-partitioned parquet file.

>>> import ibis
>>> penguins = ibis.examples.penguins.fetch()
>>> # partition on single column
>>> penguins.to_parquet("penguins_hive_dir", partition_by="year")
>>> # partition on multiple columns
>>> penguins.to_parquet("penguins_hive_dir", partition_by=("year", "island"))

Hive-partitioned output is currently only supported when using DuckDB

Source code in ibis/expr/types/core.py
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
@experimental
def to_parquet(
    self,
    path: str | Path,
    *,
    params: Mapping[ir.Scalar, Any] | None = None,
    **kwargs: Any,
) -> None:
    """Write the results of executing the given expression to a parquet file

    This method is eager and will execute the associated expression
    immediately.

    Parameters
    ----------
    path
        The data source. A string or Path to the parquet file.
    params
        Mapping of scalar parameter expressions to value.
    **kwargs
        Additional keyword arguments passed to pyarrow.parquet.ParquetWriter

    https://arrow.apache.org/docs/python/generated/pyarrow.parquet.ParquetWriter.html

    Examples
    --------
    Write out an expression to a single parquet file.

    >>> import ibis
    >>> penguins = ibis.examples.penguins.fetch()
    >>> penguins.to_parquet("penguins.parquet")  # doctest: +SKIP

    Write out an expression to a hive-partitioned parquet file.

    >>> import ibis
    >>> penguins = ibis.examples.penguins.fetch()
    >>> # partition on single column
    >>> penguins.to_parquet("penguins_hive_dir", partition_by="year")  # doctest: +SKIP
    >>> # partition on multiple columns
    >>> penguins.to_parquet("penguins_hive_dir", partition_by=("year", "island"))  # doctest: +SKIP

    !!! note "Hive-partitioned output is currently only supported when using DuckDB"
    """
    self._find_backend(use_default=True).to_parquet(self, path, **kwargs)

to_pyarrow(*, params=None, limit=None, **kwargs)

Execute expression and return results in as a pyarrow table.

This method is eager and will execute the associated expression immediately.

Parameters:

Name Type Description Default
params Mapping[ir.Scalar, Any] | None

Mapping of scalar parameter expressions to value.

None
limit int | str | None

An integer to effect a specific row limit. A value of None means "no limit". The default is in ibis/config.py.

None
kwargs Any

Keyword arguments

{}

Returns:

Type Description
Table

A pyarrow table holding the results of the executed expression.

Source code in ibis/expr/types/core.py
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
@experimental
def to_pyarrow(
    self,
    *,
    params: Mapping[ir.Scalar, Any] | None = None,
    limit: int | str | None = None,
    **kwargs: Any,
) -> pa.Table:
    """Execute expression and return results in as a pyarrow table.

    This method is eager and will execute the associated expression
    immediately.

    Parameters
    ----------
    params
        Mapping of scalar parameter expressions to value.
    limit
        An integer to effect a specific row limit. A value of `None` means
        "no limit". The default is in `ibis/config.py`.
    kwargs
        Keyword arguments

    Returns
    -------
    Table
        A pyarrow table holding the results of the executed expression.
    """
    return self._find_backend(use_default=True).to_pyarrow(
        self, params=params, limit=limit, **kwargs
    )

to_pyarrow_batches(*, limit=None, params=None, chunk_size=1000000, **kwargs)

Execute expression and return a RecordBatchReader.

This method is eager and will execute the associated expression immediately.

Parameters:

Name Type Description Default
limit int | str | None

An integer to effect a specific row limit. A value of None means "no limit". The default is in ibis/config.py.

None
params Mapping[ir.Value, Any] | None

Mapping of scalar parameter expressions to value.

None
chunk_size int

Maximum number of rows in each returned record batch.

1000000
kwargs Any

Keyword arguments

{}

Returns:

Type Description
results

RecordBatchReader

Source code in ibis/expr/types/core.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
@experimental
def to_pyarrow_batches(
    self,
    *,
    limit: int | str | None = None,
    params: Mapping[ir.Value, Any] | None = None,
    chunk_size: int = 1_000_000,
    **kwargs: Any,
) -> pa.ipc.RecordBatchReader:
    """Execute expression and return a RecordBatchReader.

    This method is eager and will execute the associated expression
    immediately.

    Parameters
    ----------
    limit
        An integer to effect a specific row limit. A value of `None` means
        "no limit". The default is in `ibis/config.py`.
    params
        Mapping of scalar parameter expressions to value.
    chunk_size
        Maximum number of rows in each returned record batch.
    kwargs
        Keyword arguments

    Returns
    -------
    results
        RecordBatchReader
    """
    return self._find_backend(use_default=True).to_pyarrow_batches(
        self,
        params=params,
        limit=limit,
        chunk_size=chunk_size,
        **kwargs,
    )

to_torch(*, params=None, limit=None, **kwargs)

Execute an expression and return results as a dictionary of torch tensors.

Parameters:

Name Type Description Default
params Mapping[ir.Scalar, Any] | None

Parameters to substitute into the expression.

None
limit int | str | None

An integer to effect a specific row limit. A value of None means no limit.

None
kwargs Any

Keyword arguments passed into the backend's to_torch implementation.

{}

Returns:

Type Description
dict[str, torch.Tensor]

A dictionary of torch tensors, keyed by column name.

Source code in ibis/expr/types/core.py
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
@experimental
def to_torch(
    self,
    *,
    params: Mapping[ir.Scalar, Any] | None = None,
    limit: int | str | None = None,
    **kwargs: Any,
) -> dict[str, torch.Tensor]:
    """Execute an expression and return results as a dictionary of torch tensors.

    Parameters
    ----------
    params
        Parameters to substitute into the expression.
    limit
        An integer to effect a specific row limit. A value of `None` means no limit.
    kwargs
        Keyword arguments passed into the backend's `to_torch` implementation.

    Returns
    -------
    dict[str, torch.Tensor]
        A dictionary of torch tensors, keyed by column name.
    """
    return self._find_backend(use_default=True).to_torch(
        self, params=params, limit=limit, **kwargs
    )

unbind()

Return an expression built on UnboundTable instead of backend-specific objects.

Source code in ibis/expr/types/core.py
522
523
524
525
526
def unbind(self) -> ir.Table:
    """Return an expression built on `UnboundTable` instead of backend-specific objects."""
    from ibis.expr.analysis import substitute_unbound

    return substitute_unbound(self.op()).to_expr()

visualize(format='svg', *, label_edges=False, verbose=False)

Visualize an expression as a GraphViz graph in the browser.

Parameters:

Name Type Description Default
format str

Image output format. These are specified by the graphviz Python library.

'svg'
label_edges bool

Show operation input names as edge labels

False
verbose bool

Print the graphviz DOT code to stderr if True

False

Raises:

Type Description
ImportError

If graphviz is not installed.

Source code in ibis/expr/types/core.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def visualize(
    self,
    format: str = "svg",
    *,
    label_edges: bool = False,
    verbose: bool = False,
) -> None:
    """Visualize an expression as a GraphViz graph in the browser.

    Parameters
    ----------
    format
        Image output format. These are specified by the ``graphviz`` Python
        library.
    label_edges
        Show operation input names as edge labels
    verbose
        Print the graphviz DOT code to stderr if [`True`][True]

    Raises
    ------
    ImportError
        If ``graphviz`` is not installed.
    """
    import ibis.expr.visualize as viz

    path = viz.draw(
        viz.to_graph(self, label_edges=label_edges),
        format=format,
        verbose=verbose,
    )
    webbrowser.open(f'file://{os.path.abspath(path)}')

Last update: June 22, 2023