Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retrieve date from datetime column in polars

Currently when I try to retrieve date from a polars datetime column, I have to write sth. similar to:

df = pl.DataFrame({
    'time': [dt.datetime.now()]
})


df = df.select([
    pl.col("*"),
    pl.col("time").apply(lambda x: x.date()).alias("date")
])

Is there a different way, something closer to:

pl.col("time").dt.date().alias("date")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can cast a Datetime column to a Date column:


import datetime
import polars as pl

df = pl.DataFrame({
    'time': [datetime.datetime.now()]
})

df.with_column(
    pl.col("time").cast(pl.Date)
)
shape: (1, 1)
┌────────────┐
│ time       │
│ ---        │
│ date       │
╞════════════╡
│ 2022-08-02 │
└────────────┘

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading