daft.Expression.apply#
- Expression.apply(func: Callable, return_dtype: DataType) Expression [source]#
Apply a function on each value in a given expression
Note
This is just syntactic sugar on top of a UDF and is convenient to use when your function only operates on a single column, and does not benefit from executing on batches. For either of those other use-cases, use a UDF instead.
Example
>>> import daft >>> df = daft.from_pydict({"x": ["1", "2", "tim"]}) >>> def f(x_val: str) -> int: ... if x_val.isnumeric(): ... return int(x_val) ... else: ... return 0 >>> df.with_column("num_x", df['x'].apply(f, return_dtype=daft.DataType.int64())).collect() ╭──────┬───────╮ │ x ┆ num_x │ │ --- ┆ --- │ │ Utf8 ┆ Int64 │ ╞══════╪═══════╡ │ 1 ┆ 1 │ ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤ │ 2 ┆ 2 │ ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤ │ tim ┆ 0 │ ╰──────┴───────╯ (Showing first 3 of 3 rows)
- Parameters:
func – Function to run per value of the expression
return_dtype – Return datatype of the function that was ran
- Returns:
New expression after having run the function on the expression
- Return type:
Expression