Temporal#
Function |
Description |
---|---|
Converts a formatted string date to an actual Date type. |
|
Extracts a part of a date (or datetime) such as ‘year’, ‘month’, etc. |
|
Offers the same functionality as |
DATE#
Converts a formatted string date to an actual Date type; ISO-8601 format is assumed unless a strftime-compatible formatting string is provided as the second parameter.
Example:
df = pl.DataFrame({"str_date": ["1969.10.30", "2024.07.05", "2077.02.28"]})
df.sql("""
SELECT str_date, DATE(str_date, '%Y.%m.%d') AS date FROM self
""")
# shape: (3, 2)
# ┌────────────┬────────────┐
# │ str_date ┆ date │
# │ --- ┆ --- │
# │ str ┆ date │
# ╞════════════╪════════════╡
# │ 1969.10.30 ┆ 1969-10-30 │
# │ 2024.07.05 ┆ 2024-07-05 │
# │ 2077.02.28 ┆ 2077-02-28 │
# └────────────┴────────────┘
DATE_PART#
Extracts a part of a date (or datetime) such as ‘year’, ‘month’, etc.
- Supported parts/fields:
“millennium” | “millennia”
“century” | “centuries”
“decade” | “decades”
“isoyear”
“year” | “years” | “y”
“quarter” | “quarters”
“month” | “months” | “mon” | “mons”
“dayofyear” | “doy”
“dayofweek” | “dow”
“isoweek” | “week”
“isodow”
“day” | “days” | “d”
“hour” | “hours” | “h”
“minute” | “minutes” | “mins” | “min” | “m”
“second” | “seconds” | “sec” | “secs” | “s”
“millisecond” | “milliseconds” | “ms”
“microsecond” | “microseconds” | “us”
“nanosecond” | “nanoseconds” | “ns”
“timezone”
“time”
“epoch”
Example:
df = pl.DataFrame(
{
"dt": [
date(1969, 12, 31),
date(2026, 8, 22),
date(2077, 2, 10),
]
}
)
df.sql("""
SELECT
dt,
DATE_PART('year', dt) AS year,
DATE_PART('month', dt) AS month,
DATE_PART('day', dt) AS day
FROM self
""")
# shape: (3, 4)
# ┌────────────┬──────┬───────┬─────┐
# │ dt ┆ year ┆ month ┆ day │
# │ --- ┆ --- ┆ --- ┆ --- │
# │ date ┆ i32 ┆ i8 ┆ i8 │
# ╞════════════╪══════╪═══════╪═════╡
# │ 1969-12-31 ┆ 1969 ┆ 12 ┆ 31 │
# │ 2026-08-22 ┆ 2026 ┆ 8 ┆ 22 │
# │ 2077-02-10 ┆ 2077 ┆ 2 ┆ 10 │
# └────────────┴──────┴───────┴─────┘
EXTRACT#
Extracts a part of a date (or datetime) such as ‘year’, ‘month’, etc.
- Supported parts/fields:
“millennium” | “millennia”
“century” | “centuries”
“decade” | “decades”
“isoyear”
“year” | “years” | “y”
“quarter” | “quarters”
“month” | “months” | “mon” | “mons”
“dayofyear” | “doy”
“dayofweek” | “dow”
“isoweek” | “week”
“isodow”
“day” | “days” | “d”
“hour” | “hours” | “h”
“minute” | “minutes” | “mins” | “min” | “m”
“second” | “seconds” | “sec” | “secs” | “s”
“millisecond” | “milliseconds” | “ms”
“microsecond” | “microseconds” | “us”
“nanosecond” | “nanoseconds” | “ns”
“timezone”
“time”
“epoch”
df = pl.DataFrame(
{
"dt": [
date(1969, 12, 31),
date(2026, 8, 22),
date(2077, 2, 10),
]
}
)
df.sql("""
SELECT
dt,
EXTRACT(decade FROM dt) AS decade,
EXTRACT(year FROM dt) AS year,
EXTRACT(quarter FROM dt) AS quarter,
FROM self
""")
# shape: (3, 4)
# ┌────────────┬────────┬──────┬─────────┐
# │ dt ┆ decade ┆ year ┆ quarter │
# │ --- ┆ --- ┆ --- ┆ --- │
# │ date ┆ i32 ┆ i32 ┆ i8 │
# ╞════════════╪════════╪══════╪═════════╡
# │ 1969-12-31 ┆ 196 ┆ 1969 ┆ 4 │
# │ 2026-08-22 ┆ 202 ┆ 2026 ┆ 3 │
# │ 2077-02-10 ┆ 207 ┆ 2077 ┆ 1 │
# └────────────┴────────┴──────┴─────────┘