Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a time delta column in Polars from two datetime columns

How would I make a column with the delta (in days) of two date columns. I thought I could just subtract the date objects, but I'm obviously missing something

(pl.from_records([{'start': '2021-01-01', 'end': '2022-01-01'}])
 .with_columns(pl.col(['start', 'end']).str.to_date('%Y-%m-%d'))
 .with_columns(delta = pl.col('end') - pl.col('start'))
)
like image 594
Thomas Avatar asked Sep 06 '25 21:09

Thomas


1 Answers

You can try using the Expr.sub() function instead of the - operator:

 (pl.from_records([{'start': '2021-01-01', 'end': '2022-01-01'}])
 .with_columns(pl.col(['start', 'end']).str.to_date('%Y-%m-%d'))
 .with_columns(delta = pl.col('end').sub(pl.col('start'))))
like image 123
Fayçal benaissa Avatar answered Sep 09 '25 02:09

Fayçal benaissa