cast function

The cast function casts the type of the selected attribute to the specified data type.

This function can also be used when the attribute is empty, to ensure the correct data type is assigned to the column, as an empty column will be otherwise be assigned the nominal data type.


Parameters

cast(column, newtype, forced)

Parameter

Description

column

The attribute whose values we want to cast. The column parameter is mandatory.

newtype

The datatype the attribute will be cast to. The newtype parameter is mandatory.

Possible values for this parameter are: “integer”, "nominal", “continuous”, "long", "double", "binary", "ordinal", "percentage", "currency", "date", "week", "month", "quarter", "datetime", "time", "index", "boolean", "int", "float", "short", "string”.

forced

If set to False, any cast operations will would result in the loss of data precision (e.g. converting a continuous value to an integer) will not be performed, and an error message will be displayed. This parameter is set to True by default.


Example

The following example uses the car_sales dataset.

Description

Screenshot

In the car_sales dataset, the Sales_in_thousands attribute contains continuous values with detailed numbers

If we cast this continuous attribute to an integer we will round off the number into the nearest thousand.

The required formula is cast($"Sales_in_thousands", "integer").

If we entered the value False for the forced parameter, the cast operation would not be performed, as it results in a loss of data precision of the continuous attribute values.

The formula used in this case is cast($"Sales_in_thousands", "integer", False).

An error message will be displayed at the bottom of the screen.