def cast(self, target_type: dt.DataType) -> Value: """Cast expression to indicated data type. Parameters ---------- target_type Type to cast to Returns ------- Value Casted expression """ import ibis.expr.operations as ops op = ops.Cast(self, to=target_type) if op.to.equals(self.type()): # noop case if passed type is the same return self if isinstance(op.to, (dt.Geography, dt.Geometry)): from_geotype = self.type().geotype or 'geometry' to_geotype = op.to.geotype if from_geotype == to_geotype: return self result = op.to_expr() if not self.has_name(): return result return result.name(f'cast({self.get_name()}, {op.to})')
def cast(arg, target_type): # validate op = _ops.Cast(arg, target_type) if op.args[1] == arg.type(): # noop case if passed type is the same return arg else: return op.to_expr()
def test_ops_smoke(): expr = ir.literal(3) ops.UnaryOp(expr) ops.Cast(expr, to='int64') ops.TypeOf(arg=2) ops.Negate(4) ops.Negate(4.0) ops.NullIfZero(0) ops.NullIfZero(1) ops.IsNull(ir.null()) ops.NotNull(ir.null()) ops.ZeroIfNull(ir.null()) ops.IfNull(1, ops.NullIfZero(0).to_expr()) ops.NullIf(ir.null(), ops.NullIfZero(0).to_expr()) ops.IsNan(np.nan) ops.IsInf(np.inf) ops.Ceil(4.5) ops.Floor(4.5) ops.Round(3.43456) ops.Round(3.43456, 2) ops.Round(3.43456, digits=1) ops.Clip(123, lower=30) ops.Clip(123, lower=30, upper=100) ops.BaseConvert('EEE', from_base=16, to_base=10) ops.Logarithm(100) ops.Log(100) ops.Log(100, base=2) ops.Ln(100) ops.Log2(100) ops.Log10(100) ops.Uppercase('asd') ops.Lowercase('asd') ops.Reverse('asd') ops.Strip('asd') ops.LStrip('asd') ops.RStrip('asd') ops.Capitalize('asd') ops.Substring('asd', start=1) ops.Substring('asd', 1) ops.Substring('asd', 1, length=2) ops.StrRight('asd', nchars=2) ops.Repeat('asd', times=4) ops.StringFind('asd', 'sd', start=1) ops.Translate('asd', from_str='bd', to_str='ce') ops.LPad('asd', length=2, pad='ss') ops.RPad('asd', length=2, pad='ss') ops.StringJoin(',', ['asd', 'bsdf']) ops.FuzzySearch('asd', pattern='n') ops.StringSQLLike('asd', pattern='as', escape='asd') ops.RegexExtract('asd', pattern='as', index=1) ops.RegexReplace('asd', 'as', 'a') ops.StringReplace('asd', 'as', 'a') ops.StringSplit('asd', 's') ops.StringConcat(['s', 'e']) ops.StartsWith('asd', 'as') ops.EndsWith('asd', 'xyz')
def cast(arg, target_type): # validate op = _ops.Cast(arg, target_type) if op.args[1] == arg.type(): # noop case if passed type is the same return arg else: result = op.to_expr() try: expr_name = ('cast({0}, {1!s})'.format(arg.get_name(), op.args[1])) result = result.name(expr_name) except: pass return result
def cast(arg, target_type): """Override ibis.expr.api's cast method. This allows for Timestamp-typed columns to be cast to Timestamp, since Ibis interprets some similar but non-equivalent types (eg. DateTime) to Timestamp (GitHub issue #451). """ # validate op = ops.Cast(arg, to=target_type) if op.to.equals(arg.type()) and not isinstance(arg, TimestampColumn): # noop case if passed type is the same return arg if isinstance(op.to, (dt.Geography, dt.Geometry)): from_geotype = arg.type().geotype or "geometry" to_geotype = op.to.geotype if from_geotype == to_geotype: return arg result = op.to_expr() if not arg.has_name(): return result expr_name = "cast({}, {})".format(arg.get_name(), op.to) return result.name(expr_name)