Ejemplo n.º 1
0
    def projection(self, exprs):
        """
        Compute new table expression with the indicated column expressions from
        this table.

        Parameters
        ----------
        exprs : column expression, or string, or list of column expressions and
          strings. If strings passed, must be columns in the table already

        Returns
        -------
        projection : TableExpr
        """
        import ibis.expr.analysis as L

        if isinstance(exprs, (Expr, ) + six.string_types):
            exprs = [exprs]

        exprs = [self._ensure_expr(e) for e in exprs]
        op = L.Projector(self, exprs).get_result()
        return TableExpr(op)
Ejemplo n.º 2
0
    def select(
        self,
        exprs: ir.ValueExpr | str | Sequence[ir.ValueExpr | str],
    ) -> TableExpr:
        """Compute a new table expression using `exprs`.

        Passing an aggregate function to this method will broadcast the
        aggregate's value over the number of rows in the table and
        automatically constructs a window function expression. See the examples
        section for more details.

        Parameters
        ----------
        exprs
            Column expression, string, or list of column expressions and
            strings.

        Returns
        -------
        TableExpr
            Table expression

        Examples
        --------
        Simple projection

        >>> import ibis
        >>> fields = [('a', 'int64'), ('b', 'double')]
        >>> t = ibis.table(fields, name='t')
        >>> proj = t.projection([t.a, (t.b + 1).name('b_plus_1')])
        >>> proj
        r0 := UnboundTable[t]
          a int64
          b float64
        Selection[r0]
          selections:
            a:        r0.a
            b_plus_1: r0.b + 1
        >>> proj2 = t[t.a, (t.b + 1).name('b_plus_1')]
        >>> proj.equals(proj2)
        True

        Aggregate projection

        >>> agg_proj = t[t.a.sum().name('sum_a'), t.b.mean().name('mean_b')]
        >>> agg_proj
        r0 := UnboundTable[t]
          a int64
          b float64
        Selection[r0]
          selections:
            sum_a:  WindowOp(Sum(r0.a), window=Window(how='rows'))
            mean_b: WindowOp(Mean(r0.b), window=Window(how='rows'))

        Note the `Window` objects here.

        Their existence means that the result of the aggregation will be
        broadcast across the number of rows in the input column.
        The purpose of this expression rewrite is to make it easy to write
        column/scalar-aggregate operations like

        >>> t[(t.a - t.a.mean()).name('demeaned_a')]
        r0 := UnboundTable[t]
          a int64
          b float64
        Selection[r0]
          selections:
            demeaned_a: r0.a - WindowOp(Mean(r0.a), window=Window(how='rows'))
        """
        import ibis.expr.analysis as an

        if isinstance(exprs, (Expr, str)):
            exprs = [exprs]

        projector = an.Projector(self, exprs)
        op = projector.get_result()
        return op.to_expr()