コード例 #1
0
def wrap_case_result(raw: np.ndarray, expr: ir.ValueExpr):
    """Wrap a CASE statement result in a Series and handle returning scalars.

    Parameters
    ----------
    raw : ndarray[T]
        The raw results of executing the ``CASE`` expression
    expr : ValueExpr
        The expression from the which `raw` was computed

    Returns
    -------
    Union[scalar, Series]
    """
    raw_1d = np.atleast_1d(raw)
    if np.any(isnull(raw_1d)):
        result = dd.from_array(raw_1d)
    else:
        result = dd.from_array(
            raw_1d.astype(constants.IBIS_TYPE_TO_PANDAS_TYPE[expr.type()])
        )
    # TODO - we force computation here
    if isinstance(expr, ir.ScalarExpr) and result.size.compute() == 1:
        return result.head().item()
    return result
コード例 #2
0
ファイル: window.py プロジェクト: jelitox/ibis
def propagate_down_window(expr: ir.ValueExpr, window: Window):
    import ibis.expr.operations as ops

    op = expr.op()

    clean_args = []
    unchanged = True
    for arg in op.args:
        if isinstance(arg, ir.Expr) and not isinstance(op, ops.WindowOp):
            new_arg = propagate_down_window(arg, window)
            if isinstance(new_arg.op(), ops.AnalyticOp):
                new_arg = ops.WindowOp(new_arg, window).to_expr()
            if arg is not new_arg:
                unchanged = False
            arg = new_arg

        clean_args.append(arg)

    if unchanged:
        return expr
    else:
        return type(op)(*clean_args).to_expr()
コード例 #3
0
    def mutate(
        self,
        exprs: ir.ValueExpr | Sequence[ir.ValueExpr] | None = None,
        **kwds: ir.ValueExpr,
    ):
        """Return a table projection with window functions applied.

        Any arguments can be functions.

        Parameters
        ----------
        exprs
            List of expressions
        kwds
            Expressions

        Examples
        --------
        >>> import ibis
        >>> t = ibis.table([
        ...     ('foo', 'string'),
        ...     ('bar', 'string'),
        ...     ('baz', 'double'),
        ... ], name='t')
        >>> t
        UnboundTable[table]
          name: t
          schema:
            foo : string
            bar : string
            baz : float64
        >>> expr = (t.group_by('foo')
        ...          .order_by(ibis.desc('bar'))
        ...          .mutate(qux=lambda x: x.baz.lag(),
        ...                  qux2=t.baz.lead()))
        >>> print(expr)  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
        ref_0
        UnboundTable[table]
          name: t
          schema:
            foo : string
            bar : string
            baz : float64
        Selection[table]
          table:
            Table: ref_0
          selections:
            Table: ref_0
            qux = WindowOp[float64*]
              qux = Lag[float64*]
                baz = Column[float64*] 'baz' from table
                  ref_0
                offset:
                  None
                default:
                  None
              <ibis.expr.window.Window object at 0x...>
            qux2 = WindowOp[float64*]
              qux2 = Lead[float64*]
                baz = Column[float64*] 'baz' from table
                  ref_0
                offset:
                  None
                default:
                  None
              <ibis.expr.window.Window object at 0x...>

        Returns
        -------
        TableExpr
            A table expression with window functions applied
        """
        if exprs is None:
            exprs = []
        else:
            exprs = util.promote_list(exprs)

        kwd_names = list(kwds.keys())
        kwd_values = list(kwds.values())
        kwd_values = self.table._resolve(kwd_values)

        for k, v in sorted(zip(kwd_names, kwd_values)):
            exprs.append(v.name(k))

        return self.projection([self.table] + exprs)
コード例 #4
0
 def operate(self, arg: ir.ValueExpr,
             matches: ir.ValueExpr) -> ir.ValueExpr:
     return arg.nullif(matches)
コード例 #5
0
    def mutate(
        self,
        exprs: ir.ValueExpr | Sequence[ir.ValueExpr] | None = None,
        **kwds: ir.ValueExpr,
    ):
        """Return a table projection with window functions applied.

        Any arguments can be functions.

        Parameters
        ----------
        exprs
            List of expressions
        kwds
            Expressions

        Examples
        --------
        >>> import ibis
        >>> t = ibis.table([
        ...     ('foo', 'string'),
        ...     ('bar', 'string'),
        ...     ('baz', 'double'),
        ... ], name='t')
        >>> t
        UnboundTable[t]
          foo string
          bar string
          baz float64
        >>> expr = (t.group_by('foo')
        ...          .order_by(ibis.desc('bar'))
        ...          .mutate(qux=lambda x: x.baz.lag(),
        ...                  qux2=t.baz.lead()))
        >>> print(expr)
        r0 := UnboundTable[t]
          foo string
          bar string
          baz float64
        Selection[r0]
          selections:
            r0
            qux:  WindowOp(Lag(r0.baz), window=Window(group_by=[r0.foo], order_by=[desc|r0.bar], how='rows'))
            qux2: WindowOp(Lead(r0.baz), window=Window(group_by=[r0.foo], order_by=[desc|r0.bar], how='rows'))

        Returns
        -------
        TableExpr
            A table expression with window functions applied
        """  # noqa: E501
        if exprs is None:
            exprs = []
        else:
            exprs = util.promote_list(exprs)

        kwd_keys = list(kwds.keys())
        kwd_values = self.table._resolve(list(kwds.values()))

        for k, v in zip(kwd_keys, kwd_values):
            exprs.append(v.name(k))

        return self.projection([self.table] + exprs)