Skip to content

Select all datetime columns

Description

Select all datetime columns

Usage

cs__datetime(time_unit = c("ms", "us", "ns"), time_zone = list("*", NULL))

Arguments

time_unit One (or more) of the allowed time unit precision strings, “ms”, “us”, and “ns”. Default is to select columns with any valid timeunit.
time_zone One of the followings. The value or each element of the vector will be passed to the time_zone argument of the pl$Datetime() function:
  • A character vector of one or more timezone strings, as defined in OlsonNames().
  • NULL to select Datetime columns that do not have a timezone.
  • “\*“ to select Datetime columns that have any timezone.
  • A list of single timezone strings , “*”, and NULL to select Datetime columns that do not have a timezone or have the (specific) timezone. For example, the default value list(”*”, NULL) selects all Datetime columns.

Value

A Polars selector

See Also

cs for the documentation on operators supported by Polars selectors.

Examples

library("polars")

chr_vec <- c("1999-07-21 05:20:16.987654", "2000-05-16 06:21:21.123456")
df <- pl$DataFrame(
  tstamp_tokyo = as.POSIXlt(chr_vec, tz = "Asia/Tokyo"),
  tstamp_utc = as.POSIXct(chr_vec, tz = "UTC"),
  tstamp = as.POSIXct(chr_vec),
  dt = as.Date(chr_vec),
)

# Select all datetime columns:
df$select(cs$datetime())
#> shape: (2, 3)
#> ┌─────────────────────────────────┬─────────────────────────────┬─────────────────────────┐
#> │ tstamp_tokyo                    ┆ tstamp_utc                  ┆ tstamp                  │
#> │ ---                             ┆ ---                         ┆ ---                     │
#> │ datetime[ns, Asia/Tokyo]        ┆ datetime[ms, UTC]           ┆ datetime[ms]            │
#> ╞═════════════════════════════════╪═════════════════════════════╪═════════════════════════╡
#> │ 1999-07-21 05:20:16.987653999 … ┆ 1999-07-21 05:20:16.987 UTC ┆ 1999-07-21 05:20:16.987 │
#> │ 2000-05-16 06:21:21.123456 JST  ┆ 2000-05-16 06:21:21.123 UTC ┆ 2000-05-16 06:21:21.123 │
#> └─────────────────────────────────┴─────────────────────────────┴─────────────────────────┘
# Select all datetime columns that have "ms" precision:
df$select(cs$datetime("ms"))
#> shape: (2, 2)
#> ┌─────────────────────────────┬─────────────────────────┐
#> │ tstamp_utc                  ┆ tstamp                  │
#> │ ---                         ┆ ---                     │
#> │ datetime[ms, UTC]           ┆ datetime[ms]            │
#> ╞═════════════════════════════╪═════════════════════════╡
#> │ 1999-07-21 05:20:16.987 UTC ┆ 1999-07-21 05:20:16.987 │
#> │ 2000-05-16 06:21:21.123 UTC ┆ 2000-05-16 06:21:21.123 │
#> └─────────────────────────────┴─────────────────────────┘
# Select all datetime columns that have any timezone:
df$select(cs$datetime(time_zone = "*"))
#> shape: (2, 2)
#> ┌─────────────────────────────────┬─────────────────────────────┐
#> │ tstamp_tokyo                    ┆ tstamp_utc                  │
#> │ ---                             ┆ ---                         │
#> │ datetime[ns, Asia/Tokyo]        ┆ datetime[ms, UTC]           │
#> ╞═════════════════════════════════╪═════════════════════════════╡
#> │ 1999-07-21 05:20:16.987653999 … ┆ 1999-07-21 05:20:16.987 UTC │
#> │ 2000-05-16 06:21:21.123456 JST  ┆ 2000-05-16 06:21:21.123 UTC │
#> └─────────────────────────────────┴─────────────────────────────┘
# Select all datetime columns that have a specific timezone:
df$select(cs$datetime(time_zone = "UTC"))
#> shape: (2, 1)
#> ┌─────────────────────────────┐
#> │ tstamp_utc                  │
#> │ ---                         │
#> │ datetime[ms, UTC]           │
#> ╞═════════════════════════════╡
#> │ 1999-07-21 05:20:16.987 UTC │
#> │ 2000-05-16 06:21:21.123 UTC │
#> └─────────────────────────────┘
# Select all datetime columns that have NO timezone:
df$select(cs$datetime(time_zone = NULL))
#> shape: (2, 1)
#> ┌─────────────────────────┐
#> │ tstamp                  │
#> │ ---                     │
#> │ datetime[ms]            │
#> ╞═════════════════════════╡
#> │ 1999-07-21 05:20:16.987 │
#> │ 2000-05-16 06:21:21.123 │
#> └─────────────────────────┘
# Select all columns except for datetime columns:
df$select(!cs$datetime())
#> shape: (2, 1)
#> ┌────────────┐
#> │ dt         │
#> │ ---        │
#> │ date       │
#> ╞════════════╡
#> │ 1999-07-21 │
#> │ 2000-05-16 │
#> └────────────┘