Casts an expression to the given datatype if possible.
See the casting matrix for supported casts.
Returns:
| Name | Type | Description |
Expression | Expression | Expression with the specified new datatype |
Note
- If a string is provided, it will use the sql engine to parse the string into a data type. See the SQL Reference for supported datatypes.
- a python
type can also be provided, in which case the corresponding Daft data type will be used.
See Also
Expression.cast
Examples:
| >>> import daft
>>> df = daft.from_pydict({"float": [1.0, 2.5, None]})
>>> df = df.select(df["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:
| >>> df = daft.from_pydict({"a": [1, 2, 3]})
>>> df = df.select(
... df["a"].cast(str).alias("str"),
... df["a"].cast(int).alias("int"),
... df["a"].cast(float).alias("float"),
... df["a"].cast("string").alias("sql_string"),
... df["a"].cast("int").alias("sql_int"),
... df["a"].cast("tinyint").alias("sql_tinyint"),
... )
>>> df.show()
|
╭────────┬───────┬─────────┬────────────┬─────────┬─────────────╮
│ str ┆ int ┆ float ┆ sql_string ┆ sql_int ┆ sql_tinyint │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ String ┆ Int64 ┆ Float64 ┆ String ┆ 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)
Source code in daft/functions/misc.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200 | def cast(expr: Expression, dtype: DataTypeLike) -> Expression:
"""Casts an expression to the given datatype if possible.
See the [casting matrix](https://docs.daft.ai/en/stable/api/datatypes/casting/) for supported casts.
Returns:
Expression: Expression with the specified new datatype
Note:
- If a string is provided, it will use the sql engine to parse the string into a data type. See the [SQL Reference](https://docs.daft.ai/en/stable/sql/datatypes/) for supported datatypes.
- a python `type` can also be provided, in which case the corresponding Daft data type will be used.
Tip: See Also
[`Expression.cast`](https://docs.daft.ai/en/stable/api/expressions/#daft.expressions.Expression.cast)
Examples:
>>> import daft
>>> df = daft.from_pydict({"float": [1.0, 2.5, None]})
>>> df = df.select(df["float"].cast(daft.DataType.int64()))
>>> df.show()
╭───────╮
│ float │
│ --- │
│ Int64 │
╞═══════╡
│ 1 │
├╌╌╌╌╌╌╌┤
│ 2 │
├╌╌╌╌╌╌╌┤
│ None │
╰───────╯
<BLANKLINE>
(Showing first 3 of 3 rows)
Example with python type and sql types:
>>> df = daft.from_pydict({"a": [1, 2, 3]})
>>> df = df.select(
... df["a"].cast(str).alias("str"),
... df["a"].cast(int).alias("int"),
... df["a"].cast(float).alias("float"),
... df["a"].cast("string").alias("sql_string"),
... df["a"].cast("int").alias("sql_int"),
... df["a"].cast("tinyint").alias("sql_tinyint"),
... )
>>> df.show()
╭────────┬───────┬─────────┬────────────┬─────────┬─────────────╮
│ str ┆ int ┆ float ┆ sql_string ┆ sql_int ┆ sql_tinyint │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ String ┆ Int64 ┆ Float64 ┆ String ┆ Int32 ┆ Int8 │
╞════════╪═══════╪═════════╪════════════╪═════════╪═════════════╡
│ 1 ┆ 1 ┆ 1 ┆ 1 ┆ 1 ┆ 1 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2 ┆ 2 ┆ 2 ┆ 2 ┆ 2 ┆ 2 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 3 ┆ 3 ┆ 3 ┆ 3 ┆ 3 ┆ 3 │
╰────────┴───────┴─────────┴────────────┴─────────┴─────────────╯
<BLANKLINE>
(Showing first 3 of 3 rows)
"""
dtype = DataType._infer(dtype)
expr = Expression._to_expression(expr)
return Expression._from_pyexpr(expr._expr.cast(dtype._dtype))
|