예제 #1
0
    def when(self, case_expr, result_expr):
        """
        Add a new case-result pair.

        Parameters
        ----------
        case : Expr
          Expression to equality-compare with base expression. Must be
          comparable with the base.
        result : Expr
          Value when the case predicate evaluates to true.

        Returns
        -------
        builder : CaseBuilder
        """
        case_expr = rlz.any(case_expr)
        result_expr = rlz.any(result_expr)

        if not isinstance(case_expr, ir.BooleanValue):
            raise TypeError(case_expr)

        cases = list(self.cases)
        cases.append(case_expr)

        results = list(self.results)
        results.append(result_expr)

        # Maintain immutability
        return type(self)(cases, results, self.default)
예제 #2
0
 def f(self, other):
     try:
         other = rlz.any(other)
         op = klass(self, other)
         return op.to_expr()
     except (com.IbisTypeError, NotImplementedError):
         return NotImplemented
예제 #3
0
    def between(
        self,
        lower: Value,
        upper: Value,
    ) -> ir.BooleanValue:
        """Check if this expression is between `lower` and `upper`, inclusive.

        Parameters
        ----------
        lower
            Lower bound
        upper
            Upper bound

        Returns
        -------
        BooleanValue
            Expression indicating membership in the provided range
        """
        import ibis.expr.operations as ops
        import ibis.expr.rules as rlz

        return ops.Between(self, rlz.any(lower), rlz.any(upper)).to_expr()
예제 #4
0
    def __rsub__(
        self,
        other: TimeValue | IntervalValue,
    ) -> IntervalValue | TimeValue | NotImplemented:
        """Subtract a time or an interval from a time expression."""
        import ibis.expr.operations as ops
        import ibis.expr.rules as rlz

        other = rlz.any(other)

        if isinstance(other, TimeValue):
            op = ops.TimeDiff
        else:
            op = ops.TimeSub  # let the operation validate

        return _binop(op, other, self)
예제 #5
0
    def else_(self, result_expr):
        """
        Specify

        Returns
        -------
        builder : CaseBuilder
        """
        kwargs = {
            slot: getattr(self, slot)
            for slot in self.__slots__
            if slot != 'default'
        }

        result_expr = rlz.any(result_expr)
        kwargs['default'] = result_expr
        # Maintain immutability
        return type(self)(**kwargs)
예제 #6
0
    def __rsub__(
        self,
        other: datetime.datetime
        | pd.Timestamp
        | TimestampValue
        | datetime.timedelta
        | pd.Timedelta
        | IntervalValue,
    ) -> IntervalValue | TimestampValue | NotImplemented:
        """Subtract a timestamp or an interval from a timestamp."""
        import ibis.expr.operations as ops
        import ibis.expr.rules as rlz

        right = rlz.any(other)

        if isinstance(right, TimestampValue):
            op = ops.TimestampDiff
        else:
            op = ops.TimestampSub  # let the operation validate

        return _binop(op, other, self)
예제 #7
0
    def identical_to(self, other: Value) -> ir.BooleanValue:
        """Return whether this expression is identical to other.

        Corresponds to `IS NOT DISTINCT FROM` in SQL.

        Parameters
        ----------
        other
            Expression to compare to

        Returns
        -------
        BooleanValue
            Whether this expression is not distinct from `other`
        """
        import ibis.expr.operations as ops
        import ibis.expr.rules as rlz

        try:
            return ops.IdenticalTo(self, rlz.any(other)).to_expr()
        except (com.IbisTypeError, NotImplementedError):
            return NotImplemented
예제 #8
0
    def __xor__(self, other: BooleanValue) -> BooleanValue:
        from ibis.expr import operations as ops
        from ibis.expr import rules as rlz

        return _binop(ops.Xor, self, rlz.any(other))
예제 #9
0
    def __lt__(self, other: Value) -> ir.BooleanValue:
        import ibis.expr.operations as ops
        import ibis.expr.rules as rlz

        return _binop(ops.Less, self, rlz.any(other))
예제 #10
0
    def __ge__(self, other: Value) -> ir.BooleanValue:
        import ibis.expr.operations as ops
        import ibis.expr.rules as rlz

        return _binop(ops.GreaterEqual, self, rlz.any(other))
예제 #11
0
    def mutate(
        self,
        exprs: Sequence[ir.Expr] | None = None,
        **mutations: ir.Value,
    ) -> Table:
        """Add columns to a table expression.

        Parameters
        ----------
        exprs
            List of named expressions to add as columns
        mutations
            Named expressions using keyword arguments

        Returns
        -------
        Table
            Table expression with additional columns

        Examples
        --------
        Using keywords arguments to name the new columns

        >>> import ibis
        >>> table = ibis.table(
        ...     [('foo', 'double'), ('bar', 'double')],
        ...     name='t'
        ... )
        >>> expr = table.mutate(qux=table.foo + table.bar, baz=5)
        >>> expr
        r0 := UnboundTable[t]
          foo float64
          bar float64
        Selection[r0]
          selections:
            r0
            baz: 5
            qux: r0.foo + r0.bar

        Use the [`name`][ibis.expr.types.generic.Value.name] method to name
        the new columns.

        >>> new_columns = [ibis.literal(5).name('baz',),
        ...                (table.foo + table.bar).name('qux')]
        >>> expr2 = table.mutate(new_columns)
        >>> expr.equals(expr2)
        True

        """
        from ibis.expr import analysis as an
        from ibis.expr import rules as rlz

        exprs = [] if exprs is None else util.promote_list(exprs)
        for name, expr in sorted(
            mutations.items(), key=operator.itemgetter(0)
        ):
            if util.is_function(expr):
                value = expr(self)
            elif isinstance(expr, Deferred):
                value = expr.resolve(self)
            else:
                value = rlz.any(expr)
            exprs.append(value.name(name))

        mutation_exprs = an.get_mutation_exprs(exprs, self)
        return self.projection(mutation_exprs)
예제 #12
0
파일: generic.py 프로젝트: cpcloud/ibis
    def __ne__(self, other: AnyValue) -> ir.BooleanValue:
        import ibis.expr.operations as ops
        import ibis.expr.rules as rlz

        return _binop(ops.NotEquals, self, rlz.any(other))