Skip to content

Select all array columns

Source code

Description

[Experimental]

Usage

cs__array(inner = NULL, ..., width = NULL)

Arguments

inner An optional inner selector to select columns having specific inner data types. If NULL, all inner types are selected.
These dots are for future extensions and must be empty.
width An optional integer specifying the width of the array columns to select. If NULL, all widths are selected.

Value

A Polars selector

See Also

  • cs for the documentation on operators supported by selectors.
  • cs$by_dtype(): Select all columns matching the given dtype(s).
  • cs$list(): Select all list columns.
  • cs$nested(): Select all nested columns.

Examples

library("polars")

df <- pl$DataFrame(
  foo = list(c("xx", "yy"), c("x", "y")),
  bar = list(123, 456),
  baz = c(2.0, 5.5),
  .schema_overrides = list(
    foo = pl$Array(pl$String, 2),
    bar = pl$Array(pl$Int64, 1)
  )
)

# Select all array columns:
df$select(cs$array())
#> shape: (2, 2)
#> ┌───────────────┬───────────────┐
#> │ foo           ┆ bar           │
#> │ ---           ┆ ---           │
#> │ array[str, 2] ┆ array[i64, 1] │
#> ╞═══════════════╪═══════════════╡
#> │ ["xx", "yy"]  ┆ [123]         │
#> │ ["x", "y"]    ┆ [456]         │
#> └───────────────┴───────────────┘
# Select all columns except for those that are array:
df$select(!cs$array())
#> shape: (2, 1)
#> ┌─────┐
#> │ baz │
#> │ --- │
#> │ f64 │
#> ╞═════╡
#> │ 2.0 │
#> │ 5.5 │
#> └─────┘
# If you want to select specific array columns,
# you can specify the inner data type and/or width:
df$select(cs$array(cs$string()))
#> shape: (2, 1)
#> ┌───────────────┐
#> │ foo           │
#> │ ---           │
#> │ array[str, 2] │
#> ╞═══════════════╡
#> │ ["xx", "yy"]  │
#> │ ["x", "y"]    │
#> └───────────────┘
df$select(cs$array(width = 1))
#> shape: (2, 1)
#> ┌───────────────┐
#> │ bar           │
#> │ ---           │
#> │ array[i64, 1] │
#> ╞═══════════════╡
#> │ [123]         │
#> │ [456]         │
#> └───────────────┘
df$select(cs$array(cs$string() | cs$numeric(), width = 2))
#> shape: (2, 1)
#> ┌───────────────┐
#> │ foo           │
#> │ ---           │
#> │ array[str, 2] │
#> ╞═══════════════╡
#> │ ["xx", "yy"]  │
#> │ ["x", "y"]    │
#> └───────────────┘