polars.Expr.dt.is_business_day#

Expr.dt.is_business_day(
*,
week_mask: Iterable[bool] = (True, True, True, True, True, False, False),
holidays: Iterable[dt.date] = (),
) Expr[source]#

Determine whether each day lands on a business day.

Warning

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Parameters:
week_mask

Which days of the week to count. The default is Monday to Friday. If you wanted to count only Monday to Thursday, you would pass (True, True, True, True, False, False, False).

holidays

Holidays to exclude from the count. The Python package python-holidays may come in handy here. You can install it with pip install holidays, and then, to get all Dutch holidays for years 2020-2024:

import holidays

my_holidays = holidays.country_holidays("NL", years=range(2020, 2025))

and pass holidays=my_holidays when you call is_business_day.

Returns:
Expr

Expression of data type Boolean.

Examples

>>> from datetime import date
>>> df = pl.DataFrame({"start": [date(2020, 1, 3), date(2020, 1, 5)]})
>>> df.with_columns(is_business_day=pl.col("start").dt.is_business_day())
shape: (2, 2)
┌────────────┬─────────────────┐
│ start      ┆ is_business_day │
│ ---        ┆ ---             │
│ date       ┆ bool            │
╞════════════╪═════════════════╡
│ 2020-01-03 ┆ true            │
│ 2020-01-05 ┆ false           │
└────────────┴─────────────────┘

You can pass a custom weekend - for example, if you only take Sunday off:

>>> week_mask = (True, True, True, True, True, True, False)
>>> df.with_columns(
...     is_business_day=pl.col("start").dt.is_business_day(week_mask=week_mask)
... )
shape: (2, 2)
┌────────────┬─────────────────┐
│ start      ┆ is_business_day │
│ ---        ┆ ---             │
│ date       ┆ bool            │
╞════════════╪═════════════════╡
│ 2020-01-03 ┆ true            │
│ 2020-01-05 ┆ false           │
└────────────┴─────────────────┘

You can also pass a list of holidays:

>>> from datetime import date
>>> holidays = [date(2020, 1, 3), date(2020, 1, 6)]
>>> df.with_columns(
...     is_business_day=pl.col("start").dt.is_business_day(holidays=holidays)
... )
shape: (2, 2)
┌────────────┬─────────────────┐
│ start      ┆ is_business_day │
│ ---        ┆ ---             │
│ date       ┆ bool            │
╞════════════╪═════════════════╡
│ 2020-01-03 ┆ false           │
│ 2020-01-05 ┆ false           │
└────────────┴─────────────────┘