Skip to content

Extract seconds from underlying Datetime representation

Source code

Description

Applies to Datetime columns. Returns the integer second number from 0 to 59, or a floating point number from 0 < 60 if fractional=TRUE that includes any milli/micro/nanosecond component.

Usage

<Expr>$dt$second(fractional = FALSE)

Arguments

fractional A logical. Whether to include the fractional component of the second.

Value

Expr of data type Int8 or Float64

Examples

library(polars)

df = pl$DataFrame(
  datetime = as.POSIXct(
    c(
      "1978-01-01 01:01:01",
      "2024-10-13 05:30:14.500",
      "2065-01-01 10:20:30.06"
    ),
    "UTC"
  )
)

df$with_columns(
  second = pl$col("datetime")$dt$second(),
  second_fractional = pl$col("datetime")$dt$second(fractional = TRUE)
)
#> shape: (3, 3)
#> ┌─────────────────────────────┬────────┬───────────────────┐
#> │ datetime                    ┆ second ┆ second_fractional │
#> │ ---                         ┆ ---    ┆ ---               │
#> │ datetime[ms, UTC]           ┆ i8     ┆ f64               │
#> ╞═════════════════════════════╪════════╪═══════════════════╡
#> │ 1978-01-01 01:01:01 UTC     ┆ 1      ┆ 1.0               │
#> │ 2024-10-13 05:30:14.500 UTC ┆ 14     ┆ 14.5              │
#> │ 2065-01-01 10:20:30.060 UTC ┆ 30     ┆ 30.06             │
#> └─────────────────────────────┴────────┴───────────────────┘