Implementation:Eventual Inc Daft Expression Cast
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, Data_Transformation |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for converting expression values between data types provided by the Daft library.
Description
The cast method on Daft's Expression class converts column values from their current data type to a specified target type. It delegates to daft.functions.cast internally. The cast is applied element-wise and supports conversions between numeric types, string to numeric parsing, timestamp to date extraction, and other compatible type pairs.
Usage
Use .cast() on any Daft expression when you need to convert its data type. This is a method on Expression instances and requires importing DataType from Daft to specify the target type.
Code Reference
Source Location
- Repository: Daft
- File:
daft/expressions/expressions.py - Lines: L490-498
Signature
def cast(self, dtype: DataTypeLike) -> Expression
Import
from daft import col, DataType
# Cast a column to a target type
col("x").cast(DataType.int64())
col("y").cast(DataType.string())
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| dtype | DataTypeLike | Yes | The target data type to cast the expression to (e.g., DataType.int64(), DataType.string()) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Expression | A new Expression with values cast to the specified data type |
Usage Examples
Basic Usage
import daft
from daft import col, DataType
# Create DataFrame with string numbers
df = daft.from_pydict({"x": ["1", "2", "3"]})
# Cast string column to integer
df = df.with_column("x_int", col("x").cast(DataType.int64()))
df.show()
# Output:
# x: ["1", "2", "3"]
# x_int: [1, 2, 3]
Float to Integer
import daft
from daft import col, DataType
df = daft.from_pydict({"price": [19.99, 24.50, 9.99]})
# Cast float to integer (truncates decimal places)
df = df.with_column("price_rounded", col("price").cast(DataType.int64()))
df.show()
# Output:
# price: [19.99, 24.50, 9.99]
# price_rounded: [19, 24, 9]