Skip to content
Advertisement

Add timedelta to a date column above weeks

How would I add 1 year to a column?

I’ve tried using map and apply but I failed miserably.

I also wonder why pl.date() accepts integers while it advertises that it only accepts str or pli.Expr.

A small hack workaround is:

JavaScript

but this won’t work for months or days. I can’t just add a number or I’ll get a:

JavaScript

Most likely because day and month cycle while year goes to infinity.

I could also do this:

JavaScript

Advertisement

Answer

Polars allows to do addition and subtraction with python’s timedelta objects. However above week units things get a bit more complicated as we have to take different days of the month and leap years into account.

For this polars has offset_by under the dt namespace.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement