Skip to content

Arithmetic operators for Polars objects

Description

Arithmetic operators for Polars objects

Usage

## S3 method for class 'polars_expr'
e1 + e2

# S3 method for class 'polars_expr'
e1 - e2

# S3 method for class 'polars_expr'
e1 * e2

# S3 method for class 'polars_expr'
e1 / e2

# S3 method for class 'polars_expr'
e1 %% e2

# S3 method for class 'polars_expr'
e1 %/% e2

# S3 method for class 'polars_expr'
e1 ^ e2

# S3 method for class 'polars_expr'
e1 < e2

Arguments

e1, e2 Polars objects of numeric type or objects that can be coerced to a polars object of numeric type. Only + can work with two string inputs.

Value

A Polars object the same type as the input.

See Also

  • \$add()
  • \$sub()
  • \$mul()
  • \$true_div()
  • \$pow()
  • \$mod()
  • \$floor_div()

Examples

library("polars")

pl$lit(5) + 10
#> [(5.0) + (10.0)]
5 + pl$lit(10)
#> [(5.0) + (10.0)]
pl$lit(5) + pl$lit(10)
#> [(5.0) + (10.0)]
+pl$lit(1)
#> 1.0
# This will not raise an error as it is not actually evaluated.
expr = pl$lit(5) + "10"
expr
#> [(5.0) + ("10")]
# Will raise an error as it is evaluated.
tryCatch(
  pl$select(expr),
  error = function(e) e
)
#> <error/rlang_error>
#> Error:
#> ! Evaluation failed in `$select()`.
#> Caused by error:
#> ! Evaluation failed in `$collect()`.
#> Caused by error:
#> ! Invalid operation: arithmetic on string and numeric not allowed, try an explicit cast first
#> 
#> Resolved plan until failure:
#> 
#>  ---> FAILED HERE RESOLVING 'sink' <---
#> SELECT [[(5.0) + ("10")]]
#> FROM
#>   DF []; PROJECT */0 COLUMNS
#> ---
#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(pl$select(expr), error = function(e) e)
#>   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(expr)
#>   6.   └─pl$DataFrame()$select(...) at neo-r-polars/R/functions-lazy.R:12:3
#>   7.     ├─polars:::wrap(self$lazy()$select(...)$collect(`_eager` = TRUE)) at neo-r-polars/R/dataframe-frame.R:367:3
#>   8.     │ └─rlang::try_fetch(...) at neo-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(`_eager` = TRUE) at neo-r-polars/R/utils-wrap.R:3:3
#>  15.       ├─polars:::wrap(...) at neo-r-polars/R/lazyframe-frame.R:284:3
#>  16.       │ └─rlang::try_fetch(...) at neo-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 neo-r-polars/R/lazyframe-frame.R:331:5
#>  23.         └─polars:::.savvy_wrap_PlRDataFrame(...) at neo-r-polars/R/000-wrappers.R:3579:5
# `+` accepts two string inputs
pl$select(pl$lit("a") + "b")
#> shape: (1, 1)
#> ┌─────────┐
#> │ literal │
#> │ ---     │
#> │ str     │
#> ╞═════════╡
#> │ ab      │
#> └─────────┘
as_polars_series(5) + 10
#> shape: (1,)
#> Series: '' [f64]
#> [
#>  15.0
#> ]
+as_polars_series(5)
#> shape: (1,)
#> Series: '' [f64]
#> [
#>  5.0
#> ]
-as_polars_series(5)
#> shape: (1,)
#> Series: '' [f64]
#> [
#>  -5.0
#> ]