daft.Expression.cast#
- Expression.cast(dtype: Union[DataType, type, str]) Expression [source]#
Casts an expression to the given datatype if possible.
Note
Overflowing values will be wrapped, e.g. 256 will be cast to 0 for an unsigned 8-bit integer.
If a string is provided, it will use the sql engine to parse the string into a data type. See the SQL documentation for more information.
a python
type
can also be provided, in which case the corresponding Daft data type will be used.
The following combinations of datatype casting is valid:
Target → Source ↓
Null
Boolean
Integers
Floats
Decimal128
String
Binary
Fixed-size Binary
Image
Fixed-shape Image
Embedding
Tensor
Fixed-shape Tensor
Python
List
Fixed-size List
Struct
Map
Timestamp
Date
Time
Duration
Null
Y
Y
Y
Y
Y
Y
Y
Y
N
N
Y
N
N
Y
Y
Y
Y
Y
Y
Y
Y
Y
Boolean
Y
Y
Y
Y
N
Y
Y
N
N
N
N
N
N
Y
N
N
N
N
N
N
N
N
Integers
Y
Y
Y
Y
Y
Y
Y
N
N
N
N
N
N
Y
N
N
N
N
Y
Y
Y
Y
Floats
Y
Y
Y
Y
Y
Y
Y
N
N
N
N
N
N
Y
N
M
N
N
Y
Y
Y
Y
Decimal128
Y
N
Y
Y
Y
N
N
N
N
N
N
N
N
Y
N
N
N
N
N
N
N
N
String
Y
N
Y
Y
N
Y
Y
N
N
N
N
N
N
Y
N
N
N
N
Y
Y
N
N
Binary
Y
N
Y
Y
N
Y
Y
Y
N
N
N
N
N
Y
N
N
N
N
N
N
N
N
Fixed-size Binary
Y
N
N
N
N
N
Y
N
N
N
N
N
N
Y
N
N
N
N
N
N
N
N
Image
N
N
N
N
N
N
N
N
Y
Y
N
Y
Y
Y
N
N
Y
N
N
N
N
N
Fixed-size Image
N
N
N
N
N
N
N
N
Y
Y
N
Y
Y
Y
Y
Y
N
N
N
N
N
N
Embedding
Y
N
N
N
N
n
N
N
N
Y
N
Y
Y
Y
Y
Y
N
N
N
N
N
N
Tensor
Y
N
N
N
N
N
N
N
Y
Y
N
Y
Y
Y
N
N
Y
N
N
N
N
N
Fixed-shape Tensor
N
N
N
N
N
N
N
N
N
Y
N
Y
Y
Y
Y
Y
N
N
N
N
N
N
Python
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
N
N
N
N
N
List
N
N
N
N
N
N
N
N
N
N
Y
N
N
N
Y
Y
N
Y
N
N
N
N
Fixed-size List
N
N
N
N
N
N
N
N
N
Y
N
N
Y
N
Y
Y
N
N
N
N
N
N
Struct
N
N
N
N
N
N
N
N
Y
N
N
Y
N
N
N
N
Y
N
N
N
N
N
Map
N
N
N
N
N
N
N
N
N
N
Y
N
N
N
Y
Y
N
Y
N
N
N
N
Timestamp
Y
N
Y
Y
N
Y
N
N
N
N
N
N
N
Y
N
N
N
N
Y
Y
Y
N
Date
Y
N
Y
Y
N
Y
N
N
N
N
N
N
N
Y
N
N
N
N
Y
Y
N
N
Time
Y
N
Y
Y
N
Y
N
N
N
N
N
N
N
Y
N
N
N
N
N
N
Y
N
Duration
Y
N
Y
Y
N
N
N
N
N
N
N
N
N
Y
N
N
N
N
N
N
N
N
Example
>>> import daft >>> df = daft.from_pydict({"float": [1.0, 2.5, None]}) >>> df = df.select(daft.col("float").cast(daft.DataType.int64())) >>> df.show() ╭───────╮ │ float │ │ --- │ │ Int64 │ ╞═══════╡ │ 1 │ ├╌╌╌╌╌╌╌┤ │ 2 │ ├╌╌╌╌╌╌╌┤ │ None │ ╰───────╯ (Showing first 3 of 3 rows)
- Example with python type and sql types:
>>> import daft >>> df = daft.from_pydict({"a": [1, 2, 3]}) >>> df = df.select( ... daft.col("a").cast(str).alias("str"), ... daft.col("a").cast(int).alias("int"), ... daft.col("a").cast(float).alias("float"), ... daft.col("a").cast("string").alias("sql_string"), ... daft.col("a").cast("int").alias("sql_int"), ... daft.col("a").cast("tinyint").alias("sql_tinyint"), ... ) >>> df.show() ╭──────┬───────┬─────────┬────────────┬─────────┬─────────────╮ │ str ┆ int ┆ float ┆ sql_string ┆ sql_int ┆ sql_tinyint │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ Utf8 ┆ Int64 ┆ Float64 ┆ Utf8 ┆ Int32 ┆ Int8 │ ╞══════╪═══════╪═════════╪════════════╪═════════╪═════════════╡ │ 1 ┆ 1 ┆ 1 ┆ 1 ┆ 1 ┆ 1 │ ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤ │ 2 ┆ 2 ┆ 2 ┆ 2 ┆ 2 ┆ 2 │ ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤ │ 3 ┆ 3 ┆ 3 ┆ 3 ┆ 3 ┆ 3 │ ╰──────┴───────┴─────────┴────────────┴─────────┴─────────────╯ (Showing first 3 of 3 rows)
- Returns:
Expression with the specified new datatype
- Return type:
Expression