Skip to content

pola-rs/polars-xdt

Repository files navigation

polars-xdt

eXtra stuff for DateTimes

polars-xdt

PyPI version Read the docs!

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

Installation

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.

Basic Example

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!

Logo

Thanks to Olha Urdeichuk for the illustration.