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)
def f(self, other): try: other = rlz.any(other) op = klass(self, other) return op.to_expr() except (com.IbisTypeError, NotImplementedError): return NotImplemented
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()
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)
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)
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)
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
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))
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))
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))
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)
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))