polars_core::export::cast

Function cast

Source
pub fn cast(
    array: &(dyn Array + 'static),
    to_type: &ArrowDataType,
    options: CastOptionsImpl,
) -> Result<Box<dyn Array>, PolarsError>
Expand description

Cast array to the provided data type and return a new Array with type to_type, if possible.

Behavior:

  • PrimitiveArray to PrimitiveArray: overflowing cast will be None
  • Boolean to Utf8: true => ‘1’, false => 0
  • Utf8 to numeric: strings that can’t be parsed to numbers return null, float strings in integer casts return null
  • Numeric to boolean: 0 returns false, any other value returns true
  • List to List: the underlying data type is cast
  • Fixed Size List to List: the underlying data type is cast
  • List to Fixed Size List: the offsets are checked for valid order, then the underlying type is cast.
  • Struct to Struct: the underlying fields are cast.
  • PrimitiveArray to List: a list array with 1 value per slot is created
  • Date32 and Date64: precision lost when going to higher interval
  • Time32 and Time64: precision lost when going to higher interval
  • Timestamp and Date{32|64}: precision lost when going to higher interval
  • Temporal to/from backing primitive: zero-copy with data type change

Unsupported Casts

  • non-StructArray to StructArray or StructArray to non-StructArray
  • List to primitive
  • Utf8 to boolean
  • Interval and duration