Skip to content

Cast between DataType

Source code

Description

Cast between DataType

Usage

<Expr>$cast(dtype, ..., strict = TRUE, wrap_numerical = FALSE)

Arguments

dtype DataType or DataTypeExpr to cast to. A DataTypeExpr can refer to the dtype of another expression.
… These dots are for future extensions and must be empty.
strict If TRUE (default), an error will be thrown if cast failed at resolve time.
wrap_numerical If TRUE, numeric casts wrap overflowing values instead of marking the cast as invalid.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(a = 1:3, b = c(1, 2, 3))
df$with_columns(
  pl$col("a")$cast(pl$Float64),
  pl$col("b")$cast(pl$Int32)
)
#> shape: (3, 2)
#> ┌─────┬─────┐
#> │ a   ┆ b   │
#> │ --- ┆ --- │
#> │ f64 ┆ i32 │
#> ╞═════╪═════╡
#> │ 1.0 ┆ 1   │
#> │ 2.0 ┆ 2   │
#> │ 3.0 ┆ 3   │
#> └─────┴─────┘
# strict FALSE, inserts null for any cast failure
pl$select(
  pl$lit(c(100, 200, 300))$cast(pl$UInt8, strict = FALSE)
)$to_series()
#> shape: (3, 1)
#> ┌─────────┐
#> │ literal │
#> │ ---     │
#> │ u8      │
#> ╞═════════╡
#> │ 100     │
#> │ 200     │
#> │ null    │
#> └─────────┘
# strict TRUE, raise any failure as an error when query is executed.
tryCatch(
  {
    pl$select(
      pl$lit("a")$cast(pl$Float64, strict = TRUE)
    )$to_series()
  },
  error = function(e) e
)
#> <error/rlang_error>
#> Error:
#> ! Evaluation failed in `$select()`.
#> Caused by error:
#> ! Evaluation failed in `$collect()`.
#> Caused by error:
#> ! Invalid operation: conversion from `str` to `f64` failed in column 'scalar' for 1 out of 1 values: ["a"]
#> ---
#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   4. │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. └─pl$select(pl$lit("a")$cast(pl$Float64, strict = TRUE))
#>   6.   └─pl$DataFrame()$select(...) at r-polars/R/functions-lazy.R:12:3
#>   7.     ├─polars:::wrap(self$lazy()$select(...)$collect(optimizations = DEFAULT_EAGER_OPT_FLAGS)) at r-polars/R/dataframe-frame.R:372:3
#>   8.     │ └─rlang::try_fetch(...) at r-polars/R/utils-wrap.R:3:3
#>   9.     │   ├─base::tryCatch(...)
#>  10.     │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  11.     │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  12.     │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  13.     │   └─base::withCallingHandlers(...)
#>  14.     └─self$lazy()$select(...)$collect(optimizations = DEFAULT_EAGER_OPT_FLAGS) at r-polars/R/utils-wrap.R:3:3
#>  15.       ├─polars:::wrap(...) at r-polars/R/lazyframe-frame.R:253:3
#>  16.       │ └─rlang::try_fetch(...) at r-polars/R/utils-wrap.R:3:3
#>  17.       │   ├─base::tryCatch(...)
#>  18.       │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  19.       │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  20.       │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  21.       │   └─base::withCallingHandlers(...)
#>  22.       └─ldf$collect(engine) at r-polars/R/lazyframe-frame.R:260:5
#>  23.         └─polars:::.savvy_wrap_PlRDataFrame(...) at r-polars/R/000-wrappers.R:3987:5