polars.Expr.dt.second#

Expr.dt.second(*, fractional: bool = False) Expr[source]#

Extract seconds from underlying DateTime representation.

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.

Parameters:
fractional

Whether to include the fractional component of the second.

Returns:
Expr

Expression of data type UInt32 or Float64.

Examples

>>> from datetime import timedelta, datetime
>>> df = pl.DataFrame(
...     data={
...         "date": pl.date_range(
...             start=datetime(2001, 1, 1, 0, 0, 0, 456789),
...             end=datetime(2001, 1, 1, 0, 0, 6),
...             interval=timedelta(seconds=2, microseconds=654321),
...             eager=True,
...         )
...     }
... )
>>> df
shape: (3, 1)
┌────────────────────────────┐
│ date                       │
│ ---                        │
│ datetime[μs]               │
╞════════════════════════════╡
│ 2001-01-01 00:00:00.456789 │
│ 2001-01-01 00:00:03.111110 │
│ 2001-01-01 00:00:05.765431 │
└────────────────────────────┘
>>> df.select(pl.col("date").dt.second().alias("secs"))
shape: (3, 1)
┌──────┐
│ secs │
│ ---  │
│ u32  │
╞══════╡
│ 0    │
│ 3    │
│ 5    │
└──────┘
>>> df.select(pl.col("date").dt.second(fractional=True).alias("secs"))
shape: (3, 1)
┌──────────┐
│ secs     │
│ ---      │
│ f64      │
╞══════════╡
│ 0.456789 │
│ 3.11111  │
│ 5.765431 │
└──────────┘
>>> from datetime import timedelta, datetime
>>> start = datetime(2001, 1, 1)
>>> stop = datetime(2001, 1, 1, 0, 0, 4)
>>> df = pl.DataFrame(
...     {"date": pl.date_range(start, stop, timedelta(seconds=2), eager=True)}
... )
>>> df
shape: (3, 1)
┌─────────────────────┐
│ date                │
│ ---                 │
│ datetime[μs]        │
╞═════════════════════╡
│ 2001-01-01 00:00:00 │
│ 2001-01-01 00:00:02 │
│ 2001-01-01 00:00:04 │
└─────────────────────┘
>>> df.select(pl.col("date").dt.second())
shape: (3, 1)
┌──────┐
│ date │
│ ---  │
│ u32  │
╞══════╡
│ 0    │
│ 2    │
│ 4    │
└──────┘