Skip to content

Concat polars objects

Source code

Description

Concat polars objects

Usage

pl$concat(
  ...,
  how = c("vertical", "vertical_relaxed", "horizontal", "diagonal", "diagonal_relaxed"),
  rechunk = FALSE,
  parallel = TRUE
)

Arguments

Either individual unpacked args or args wrapped in list(). Args can be eager as DataFrame, Series and R vectors, or lazy as LazyFrame and Expr. The first element determines the output of $concat(): if the first element is lazy, a LazyFrame is returned; otherwise, a DataFrame is returned (note that if the first element is eager, all other elements have to be eager to avoid implicit collect).
how Bind direction. Can be "vertical" (like rbind()), "horizontal" (like cbind()), or "diagonal". For “vertical” and “diagonal”, adding the suffix “\_relaxed” will cast columns to their shared supertypes. For example, if we try to vertically concatenate two columns of types i32 and f64, using how = “vertical_relaxed” will cast the column of type i32 to f64 beforehand.
rechunk Perform a rechunk at last.
parallel Only used for LazyFrames. If TRUE (default), lazy computations may be executed in parallel.

Details

Categorical columns/Series must have been constructed while global string cache enabled. See pl$enable_string_cache().

Value

DataFrame, Series, LazyFrame or Expr

Examples

library("polars")

# vertical
l_ver = lapply(1:10, function(i) {
  l_internal = list(
    a = 1:5,
    b = letters[1:5]
  )
  pl$DataFrame(l_internal)
})
pl$concat(l_ver, how = "vertical")
#> shape: (50, 2)
#> ┌─────┬─────┐
#> │ a   ┆ b   │
#> │ --- ┆ --- │
#> │ i32 ┆ str │
#> ╞═════╪═════╡
#> │ 1   ┆ a   │
#> │ 2   ┆ b   │
#> │ 3   ┆ c   │
#> │ 4   ┆ d   │
#> │ 5   ┆ e   │
#> │ …   ┆ …   │
#> │ 1   ┆ a   │
#> │ 2   ┆ b   │
#> │ 3   ┆ c   │
#> │ 4   ┆ d   │
#> │ 5   ┆ e   │
#> └─────┴─────┘
# horizontal
l_hor = lapply(1:10, function(i) {
  l_internal = list(
    1:5,
    letters[1:5]
  )
  names(l_internal) = paste0(c("a", "b"), i)
  pl$DataFrame(l_internal)
})
pl$concat(l_hor, how = "horizontal")
#> shape: (5, 20)
#> ┌─────┬─────┬─────┬─────┬───┬─────┬─────┬─────┬─────┐
#> │ a1  ┆ b1  ┆ a2  ┆ b2  ┆ … ┆ a9  ┆ b9  ┆ a10 ┆ b10 │
#> │ --- ┆ --- ┆ --- ┆ --- ┆   ┆ --- ┆ --- ┆ --- ┆ --- │
#> │ i32 ┆ str ┆ i32 ┆ str ┆   ┆ i32 ┆ str ┆ i32 ┆ str │
#> ╞═════╪═════╪═════╪═════╪═══╪═════╪═════╪═════╪═════╡
#> │ 1   ┆ a   ┆ 1   ┆ a   ┆ … ┆ 1   ┆ a   ┆ 1   ┆ a   │
#> │ 2   ┆ b   ┆ 2   ┆ b   ┆ … ┆ 2   ┆ b   ┆ 2   ┆ b   │
#> │ 3   ┆ c   ┆ 3   ┆ c   ┆ … ┆ 3   ┆ c   ┆ 3   ┆ c   │
#> │ 4   ┆ d   ┆ 4   ┆ d   ┆ … ┆ 4   ┆ d   ┆ 4   ┆ d   │
#> │ 5   ┆ e   ┆ 5   ┆ e   ┆ … ┆ 5   ┆ e   ┆ 5   ┆ e   │
#> └─────┴─────┴─────┴─────┴───┴─────┴─────┴─────┴─────┘
# diagonal
pl$concat(l_hor, how = "diagonal")
#> shape: (50, 20)
#> ┌──────┬──────┬──────┬──────┬───┬──────┬──────┬──────┬──────┐
#> │ a1   ┆ b1   ┆ a2   ┆ b2   ┆ … ┆ a9   ┆ b9   ┆ a10  ┆ b10  │
#> │ ---  ┆ ---  ┆ ---  ┆ ---  ┆   ┆ ---  ┆ ---  ┆ ---  ┆ ---  │
#> │ i32  ┆ str  ┆ i32  ┆ str  ┆   ┆ i32  ┆ str  ┆ i32  ┆ str  │
#> ╞══════╪══════╪══════╪══════╪═══╪══════╪══════╪══════╪══════╡
#> │ 1    ┆ a    ┆ null ┆ null ┆ … ┆ null ┆ null ┆ null ┆ null │
#> │ 2    ┆ b    ┆ null ┆ null ┆ … ┆ null ┆ null ┆ null ┆ null │
#> │ 3    ┆ c    ┆ null ┆ null ┆ … ┆ null ┆ null ┆ null ┆ null │
#> │ 4    ┆ d    ┆ null ┆ null ┆ … ┆ null ┆ null ┆ null ┆ null │
#> │ 5    ┆ e    ┆ null ┆ null ┆ … ┆ null ┆ null ┆ null ┆ null │
#> │ …    ┆ …    ┆ …    ┆ …    ┆ … ┆ …    ┆ …    ┆ …    ┆ …    │
#> │ null ┆ null ┆ null ┆ null ┆ … ┆ null ┆ null ┆ 1    ┆ a    │
#> │ null ┆ null ┆ null ┆ null ┆ … ┆ null ┆ null ┆ 2    ┆ b    │
#> │ null ┆ null ┆ null ┆ null ┆ … ┆ null ┆ null ┆ 3    ┆ c    │
#> │ null ┆ null ┆ null ┆ null ┆ … ┆ null ┆ null ┆ 4    ┆ d    │
#> │ null ┆ null ┆ null ┆ null ┆ … ┆ null ┆ null ┆ 5    ┆ e    │
#> └──────┴──────┴──────┴──────┴───┴──────┴──────┴──────┴──────┘
# if two columns don't share the same type, concat() will error unless we use
# `how = "vertical_relaxed"`:
test = pl$DataFrame(x = 1L) # i32
test2 = pl$DataFrame(x = 1.0) # f64

pl$concat(test, test2, how = "vertical_relaxed")
#> shape: (2, 1)
#> ┌─────┐
#> │ x   │
#> │ --- │
#> │ f64 │
#> ╞═════╡
#> │ 1.0 │
#> │ 1.0 │
#> └─────┘