eXtra stuff for DateTimes in Polars.
- β blazingly fast, written in Rust
- β custom business-day arithmetic
- β convert to and from multiple time zones
- β format datetime in different locales
- β convert to Julian Dates
- β time-based EWMA
First, you need to install Polars.
Then, you'll need to install polars-xdt
:
pip install polars-xdt
Read the documentation for a little tutorial and API reference.
Say we start with
from datetime import date
import polars as pl
import polars_xdt as xdt
df = pl.DataFrame(
{"date": [date(2023, 4, 3), date(2023, 9, 1), date(2024, 1, 4)]}
)
Let's shift Date
forwards by 5 days, excluding Saturday and Sunday:
result = df.with_columns(
date_shifted=xdt.offset_by(
'date',
'5bd',
weekend=('Sat', 'Sun'),
)
)
print(result)
shape: (3, 2)
ββββββββββββββ¬βββββββββββββββ
β date β date_shifted β
β --- β --- β
β date β date β
ββββββββββββββͺβββββββββββββββ‘
β 2023-04-03 β 2023-04-10 β
β 2023-09-01 β 2023-09-08 β
β 2024-01-04 β 2024-01-11 β
ββββββββββββββ΄βββββββββββββββ
Note that polars-xdt
also registers a xdt
namespace in the Expression
class, so you
could equivalently write the above using pl.col('date').xdt.offset_by('5bd')
(but note
that then type-checking would not recognise the xdt
attribute).
Read the documentation for more examples!
Thanks to Olha Urdeichuk for the illustration.