Skip to content

Datetime

  • https://www.confessionsofadataguy.com/date-and-datetime-manipulation-in-polar

  • https://medium.com/@riellygriffiths/working-with-datetime-data-in-polars-9bb57e7f6304

df = pl.DataFrame({'dt': ['2024-10-01', '2024-10-02']})

string to date

df.with_columns(
    dt = pl.col('dt').str.to_datetime().cast(pl.Date)
)
df.with_columns(
    dt = pl.col('dt').str.strptime(pl.Date)
)

ctx = pl.SQLContext(data=df)
ctx.execute('SELECT *, CAST(dt as DATE) as date FROM data', eager=True)
ctx.execute('SELECT *, DATE(dt) as dt FROM data', eager=True)

string to datetime

df.with_columns(
    dt = pl.col('dt').str.to_datetime().cast(pl.Datetime)
)

date to string

df.with_columns(
    dt = pl.col('dt').dt.strftime('%Y-%m-%d')
)

date to yera/month/day

df.with_columns([
    pl.col('dt').dt.year().alias('year'),
    pl.col('dt').dt.month().alias('month'),
    pl.col('dt').dt.day().alias('day'),
])

add days to date column

df.with_columns(
    dt = pl.col('dt') + pl.duration(days=1)
)