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:
“day”
“dayofweek” | “dow”
“dayofyear” | “doy”
“decade”
“epoch”
“hour”
“isodow”
“isoweek” | “week”
“isoyear”
“microsecond(s)”
“millisecond(s)”
“nanosecond(s)”
“minute”
“month”
“quarter”
“second”
“time”
“year”
Example:
df = pl.DataFrame(
{
"dt": [
date(1969, 12, 31),
date(2026, 8, 22),
date(2077, 2, 10),
]
}
)
df.sql("""
SELECT
dt,
DATE_PART(dt, 'year') AS year,
DATE_PART(dt, 'month') AS month,
DATE_PART(dt, 'day') 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:
“day”
“dayofweek” | “dow”
“dayofyear” | “doy”
“decade”
“epoch”
“hour”
“isodow”
“isoweek” | “week”
“isoyear”
“microsecond(s)”
“millisecond(s)”
“nanosecond(s)”
“minute”
“month”
“quarter”
“second”
“time”
“year”
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 │
# └────────────┴────────┴──────┴─────────┘