[Python] How to count work days between date columns with Polars

Discussão em 'Python' iniciado por Stack, Agosto 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following DataFrame.

    df = pl.from_repr("""
    ┌────────────┬───────────────┐
    │ date ┆ maturity_date │
    │ --- ┆ --- │
    │ date ┆ date │
    ╞════════════╪═══════════════╡
    │ 2000-01-04 ┆ 2000-01-17 │
    │ 2000-01-04 ┆ 2000-02-15 │
    │ 2000-01-04 ┆ 2000-03-15 │
    │ 2000-01-04 ┆ 2000-04-17 │
    │ 2000-01-04 ┆ 2000-05-15 │
    └────────────┴───────────────┘
    """)


    I'm trying to get the number of the workdays between date and maturity_date (not counting saturday and sunday)

    I'd also like to calculate diff days that use a given calendar like a trade date calendar of stock market which is different from a normal calendar.

    I use this date_ranges to count workdays, but it seems only a little faster than map_elements

    df.with_columns(
    pl.date_ranges("date", "maturity_date")
    .list.eval(pl.element().dt.weekday() <= 5)
    .list.count_matches(True)
    .alias("workdays_diff")
    # pl.concat_list("date", "maturity_date").map_elements(lambda x: get_work_days(x[0], x[1]))
    # .alias("workdays_diff")
    )


    shape: (5, 3)
    ┌────────────┬───────────────┬───────────────┐ # ┌────────────────┐
    │ date ┆ maturity_date ┆ workdays_diff │ # │ tradedate_diff │
    │ --- ┆ --- ┆ --- │ # │ --- │
    │ date ┆ date ┆ u32 │ # │ i64 │
    ╞════════════╪═══════════════╪═══════════════╡ # ╞════════════════╡
    │ 2000-01-04 ┆ 2000-01-17 ┆ 10 │ # │ 10 │
    │ 2000-01-04 ┆ 2000-02-15 ┆ 31 │ # │ 21 │
    │ 2000-01-04 ┆ 2000-03-15 ┆ 52 │ # │ 42 │
    │ 2000-01-04 ┆ 2000-04-17 ┆ 75 │ # │ 65 │
    │ 2000-01-04 ┆ 2000-05-15 ┆ 95 │ # │ 80 │
    └────────────┴───────────────┴───────────────┘ # └────────────────┘


    Is there a faster way?

    Is there also a way to calculate tradedate_diff?

    Continue reading...

Compartilhe esta Página