Skip to content

Select all columns matching the given dtypes

Description

Select all columns matching the given dtypes

Usage

cs__by_dtype(...)

Arguments

\<dynamic-dots\> Data types to select.

Value

A Polars selector

See Also

cs for the documentation on operators supported by Polars selectors.

Examples

library("polars")

df <- pl$DataFrame(
  dt = as.Date(c("1999-12-31", "2024-1-1", "2010-7-5")),
  value = c(1234500, 5000555, -4500000),
  other = c("foo", "bar", "foo")
)

# Select all columns with date or string dtypes:
df$select(cs$by_dtype(pl$Date, pl$String))
#> shape: (3, 2)
#> ┌────────────┬───────┐
#> │ dt         ┆ other │
#> │ ---        ┆ ---   │
#> │ date       ┆ str   │
#> ╞════════════╪═══════╡
#> │ 1999-12-31 ┆ foo   │
#> │ 2024-01-01 ┆ bar   │
#> │ 2010-07-05 ┆ foo   │
#> └────────────┴───────┘
# Select all columns that are not of date or string dtype:
df$select(!cs$by_dtype(pl$Date, pl$String))
#> shape: (3, 1)
#> ┌────────────┐
#> │ value      │
#> │ ---        │
#> │ f64        │
#> ╞════════════╡
#> │ 1.2345e6   │
#> │ 5.000555e6 │
#> │ -4.5e6     │
#> └────────────┘
# Group by string columns and sum the numeric columns:
df$group_by(cs$string())$agg(cs$numeric()$sum())$sort("other")
#> shape: (2, 2)
#> ┌───────┬────────────┐
#> │ other ┆ value      │
#> │ ---   ┆ ---        │
#> │ str   ┆ f64        │
#> ╞═══════╪════════════╡
#> │ bar   ┆ 5.000555e6 │
#> │ foo   ┆ -3.2655e6  │
#> └───────┴────────────┘