polars
The polars package for R gives users access to a lightning fast Data Frame library written in Rust. Polarsβ embarrassingly parallel execution, cache efficient algorithms and expressive API makes it perfect for efficient data wrangling, data pipelines, snappy APIs, and much more besides. Polars also supports βstreaming modeβ for out-of-memory operations. This allows users to analyze datasets many times larger than RAM.
Documentation can be found on the r-polars homepage.
The primary developer of the upstream Polars project is Ritchie Vink (@ritchie46). This R port is maintained by SΓΈren Welling (@sorhawell) and contributors. Consider joining our Discord (subchannel) for additional help and discussion.
Install
The package can be installed from R-universe, or GitHub.
Some platforms can install pre-compiled binaries, and others will need to build from source.
R-universe
R-universe provides pre-compiled polars binaries for Windows (x86_64), macOS (x86_64) and Ubuntu 22.04 (x86_64) with source builds for other platforms.
Binary packages on R-universe are compiled by stable Rust, with nightly features disabled.
install.packages("polars", repos = "https://rpolars.r-universe.dev")
# For Ubuntu binary installation
install.packages("polars", repos = "https://rpolars.r-universe.dev/bin/linux/jammy/4.3")
Special thanks to Jeroen Ooms (@jeroen) for the excellent R-universe support.
GitHub releases
We also provide pre-compiled binaries for various operating systems on our GitHub releases page. You can download and install these files manually, or install directly from R. Simply match the URL for your operating system and the desired release. For example, to install the latest release of polars on one can use:
Linux (x86_64)
install.packages(
"https://github.com/pola-rs/r-polars/releases/latest/download/polars__x86_64-pc-linux-gnu.gz",
repos = NULL
)
Windows
install.packages(
"https://github.com/pola-rs/r-polars/releases/latest/download/polars.zip",
repos = NULL
)
macOS(x86_64)
install.packages(
"https://github.com/pola-rs/r-polars/releases/latest/download/polars__x86_64-apple-darwin20.tgz",
repos = NULL
)
Just remember to invoke the repos = NULL
argument if you are
installing these binary builds directly from within R.
Binary packages on GitHub releases are compiled by nightly Rust, with nightly features enabled.
Build from source
For source installation, the Rust toolchain (Rust 1.70 or later) must be configured.
Currently you should install rust >=1.70 or nightly-2023-07-27 (for full features (simd)).
Please check the https://github.com/r-rust/hellorust repository for about Rust code in R packages.
During source installation, some environment variables can be set to enable Rust features and profile changes.
RPOLARS_FULL_FEATURES="true"
(Build with nightly feature enabled, requires Rust toolchain nightly-2023-07-27)RPOLARS_PROFILE="release-optimized"
(Build with more optimization)
Quickstart example
The Get Started vignette
(vignette("polars")
) contains a series of detailed examples, but here
is a quick illustration.
polars is a very powerful package with many functions. To avoid
conflicts with other packages and base R function names, polarsβs
top level functions are hosted in the pl
namespace, and accessible via
the pl$
prefix. To convert an R data frame to a Polars DataFrame
, we
call:
library(polars)
dat = pl$DataFrame(mtcars)
dat
#> shape: (32, 11)
#> ββββββββ¬ββββββ¬ββββββββ¬ββββββββ¬ββββ¬ββββββ¬ββββββ¬βββββββ¬βββββββ
#> β mpg β cyl β disp β hp β β¦ β vs β am β gear β carb β
#> β --- β --- β --- β --- β β --- β --- β --- β --- β
#> β f64 β f64 β f64 β f64 β β f64 β f64 β f64 β f64 β
#> ββββββββͺββββββͺββββββββͺββββββββͺββββͺββββββͺββββββͺβββββββͺβββββββ‘
#> β 21.0 β 6.0 β 160.0 β 110.0 β β¦ β 0.0 β 1.0 β 4.0 β 4.0 β
#> β 21.0 β 6.0 β 160.0 β 110.0 β β¦ β 0.0 β 1.0 β 4.0 β 4.0 β
#> β 22.8 β 4.0 β 108.0 β 93.0 β β¦ β 1.0 β 1.0 β 4.0 β 1.0 β
#> β 21.4 β 6.0 β 258.0 β 110.0 β β¦ β 1.0 β 0.0 β 3.0 β 1.0 β
#> β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β β¦ β
#> β 15.8 β 8.0 β 351.0 β 264.0 β β¦ β 0.0 β 1.0 β 5.0 β 4.0 β
#> β 19.7 β 6.0 β 145.0 β 175.0 β β¦ β 0.0 β 1.0 β 5.0 β 6.0 β
#> β 15.0 β 8.0 β 301.0 β 335.0 β β¦ β 0.0 β 1.0 β 5.0 β 8.0 β
#> β 21.4 β 4.0 β 121.0 β 109.0 β β¦ β 1.0 β 1.0 β 4.0 β 2.0 β
#> ββββββββ΄ββββββ΄ββββββββ΄ββββββββ΄ββββ΄ββββββ΄ββββββ΄βββββββ΄βββββββ
This DataFrame
object can be manipulated using many of the usual R
functions and accessors, e.g.:
dat[1:4, c("mpg", "qsec", "hp")]
#> shape: (4, 3)
#> ββββββββ¬ββββββββ¬ββββββββ
#> β mpg β qsec β hp β
#> β --- β --- β --- β
#> β f64 β f64 β f64 β
#> ββββββββͺββββββββͺββββββββ‘
#> β 21.0 β 16.46 β 110.0 β
#> β 21.0 β 17.02 β 110.0 β
#> β 22.8 β 18.61 β 93.0 β
#> β 21.4 β 19.44 β 110.0 β
#> ββββββββ΄ββββββββ΄ββββββββ
However, the true power of Polars is unlocked by using methods, which
are encapsulated in the DataFrame
object itself. For example, we can
chain the $groupby()
and the $mean()
methods to compute group-wise
means for each column of the dataset:
dat$groupby("cyl", maintain_order = TRUE)$mean()
#> shape: (3, 11)
#> βββββββ¬ββββββββββββ¬βββββββββββββ¬βββββββββββββ¬ββββ¬βββββββββββ¬βββββββββββ¬βββββββββββ¬βββββββββββ
#> β cyl β mpg β disp β hp β β¦ β vs β am β gear β carb β
#> β --- β --- β --- β --- β β --- β --- β --- β --- β
#> β f64 β f64 β f64 β f64 β β f64 β f64 β f64 β f64 β
#> βββββββͺββββββββββββͺβββββββββββββͺβββββββββββββͺββββͺβββββββββββͺβββββββββββͺβββββββββββͺβββββββββββ‘
#> β 6.0 β 19.742857 β 183.314286 β 122.285714 β β¦ β 0.571429 β 0.428571 β 3.857143 β 3.428571 β
#> β 4.0 β 26.663636 β 105.136364 β 82.636364 β β¦ β 0.909091 β 0.727273 β 4.090909 β 1.545455 β
#> β 8.0 β 15.1 β 353.1 β 209.214286 β β¦ β 0.0 β 0.142857 β 3.285714 β 3.5 β
#> βββββββ΄ββββββββββββ΄βββββββββββββ΄βββββββββββββ΄ββββ΄βββββββββββ΄βββββββββββ΄βββββββββββ΄βββββββββββ
Note that we use maintain_order = TRUE
so that polars
always keeps
the groups in the same order as they are in the original data.
The polars vignette contains many more examples of how to use the package to:
- Read CSV, JSON, Parquet, and other file formats.
- Filter rows and select columns.
- Modify and create new columns.
- Group by and aggregate.
- Reshape data.
- Join and concatenate different datasets.
- Sort data.
- Work with dates and times.
- Handle missing values.
- Use the lazy execution engine for maximum performance and memory-efficient operations.
- Etc.
Development and Contributions
Contributions are very welcome!
As of March 2023, polars has now reached nearly 100% coverage of the
underlying βlazyβ Expr syntax. While translation of the βeagerβ syntax
is still a little further behind, you should be able to do just about
everything using $select()
+ $with_columns()
. Most of the methods
associated with DataFrame
and LazyFrame
classes have been
implemented, but not all. There is still much to do, and your help would
be much appreciated!
If you spot missing functionalityβimplemented in Python but not Rβplease let us know on GitHub.
System dependencies
To install the development version of Polars or develop new features, you will to install the Rust toolchain:
-
Install
rustup
, the cross-platform Rust installer. Then:rustup toolchain install nightly-2023-07-27 rustup default nightly-2023-07-27
-
Windows: Make sure the latest version of Rtools is installed and on your PATH.
-
macOS: Make sure
Xcode
is installed. -
Install CMake and add it to your PATH.
Implementing new features
Here are the steps required for an example contribution, where we are implementing the cosine expression:
- Look up the polars.Expr.cos method in py-polars documentation.
- Press the
[source]
button to see the Python implementation - Find the cos py-polars rust implementation (likely just a simple call to the Rust-Polars API)
- Adapt the Rust part and place it here.
- Adapt the Python frontend syntax to R and place it here. Add the roxygen docs + examples above.
- Notice we use
Expr_cos = "use_extendr_wrapper"
, it means weβre just using unmodified the extendr auto-generated wrapper - Write a test here.
- Run
renv::restore()
and resolve all R packages - Run
rextendr::document()
to recompile and confirm the added method functions as intended, e.g.Βpl$DataFrame(a=c(0,pi/2,pi,NA_real_))$select(pl$col("a")$cos())
- Run
devtools::test()
. See below for how to set up your development environment correctly.
Note that PRs to polars will be automatically be built and tested on all platforms as part of our GitHub Actions workflow. A more detailed description of the development environment and workflow for local builds is provided below.
Development workflow
Assuming the system dependencies have been met (above), the typical polars development workflow is as follows:
Step 1: Fork the polars repo on GitHub and then clone it locally.
git clone [email protected]:<YOUR-GITHUB-ACCOUNT>/r-polars.git
cd r-polars
Step 2: Build the package and install the suggested package dependencies.
-
Option A: Using devtools.
Rscript -e 'devtools::install(pkg = ".", dependencies = TRUE)'
-
Option B: Using renv.
# Rscript -e 'install.packages("renv")' Rscript -e 'renv::activate(); renv::restore()'
Step 3: Make your proposed changes to the R and/or Rust code. Donβt forget to run:
rextendr::document() # compile Rust code + update wrappers & docs
devtools::test() # run all unit tests
Step 4 (optional): Build the package locally.
R CMD INSTALL --no-multiarch --with-keep.source .
Step 5: Commit your changes and submit a PR to the main polars repo.
- As aside, notice that
./renv.lock
sets all R packages during the server build.
Tip: To speed up the local rextendr::document() or R CMD check, run the following:
source("inst/misc/develop_polars.R")
#to rextendr:document() + not_cran + load packages + all_features
load_polars()
#to check package + reuses previous compilation in check, protects against deletion
check_polars() #assumes rust target at `paste0(getwd(),"/src/rust")`
- The
RPOLARS_RUST_SOURCE
environment variable allows polars to recover the Cargo cache even if source files have been moved. Replace with your own absolute path to your local clone! filter_rcmdcheck.R
removes known warnings from final check report.unlink("check")
cleans up.
Misc
If you experience unexpected sluggish performance, when using polars in
a given IDE, weβd like to hear about it. You can try to activate
pl$set_polars_options(debug_polars = TRUE)
to profile what methods are
being touched (not necessarily run) and how fast. Below is an example of
good behavior.
#run e.g. an eager query after setting debug_polars = TRUE
pl$DataFrame(iris)$select("Species")
[TIME? ms]
pl$DataFrame() -> [0.73ms]
.pr$DataFrame$new_with_capacity() -> [0.56ms]
.pr$DataFrame$set_column_from_robj() -> [11.04ms]
.pr$DataFrame$set_column_from_robj() -> [0.3309ms]
.pr$DataFrame$set_column_from_robj() -> [0.283ms]
.pr$DataFrame$set_column_from_robj() -> [0.2761ms]
.pr$DataFrame$set_column_from_robj() -> [12.54ms]
DataFrame$select() -> [0.3681ms]
ProtoExprArray$push_back_rexpr() -> [0.21ms]
pl$col() -> [0.1669ms]
.pr$Expr$col() -> [0.212ms]
.pr$DataFrame$select() -> [1.229ms]
DataFrame$print() -> [0.1781ms]
.pr$DataFrame$print() -> shape: (150, 1)
βββββββββββββ
β Species β
β --- β
β cat β
βββββββββββββ‘
β setosa β
β setosa β
β setosa β
β setosa β
β β¦ β
β virginica β
β virginica β
β virginica β
β virginica β
βββββββββββββ