Metadata-Version: 2.4
Name: polars-intervals
Version: 0.2.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Rust
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: polars>=1.44.1,<1.45
License-File: LICENSE
Summary: Interval algorithms for Polars
Keywords: polars,intervals,overlap,rust
License-Expression: MIT
Requires-Python: >=3.12
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Issues, https://github.com/jplauri/polars-intervals/issues
Project-URL: Repository, https://github.com/jplauri/polars-intervals

# polars-intervals

Interval algorithms for Polars, with support for eager and lazy queries.

[Usage](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md) ·
[Benchmarks](https://github.com/jplauri/polars-intervals/blob/master/docs/benchmarks.md)

## Install

Requires Python 3.12+.

```sh
pip install polars-intervals
```

Or with uv: `uv add polars-intervals`.

## Examples

### Count overlaps

Count how many other intervals overlap each row:

```python
import polars as pl
import polars_intervals as pi

df = pl.DataFrame(
    {
        "start": [0, 1, 2],
        "end": [2, 3, 4],
    }
)
result = (
    df.lazy()
    .with_columns(
        pi.overlap_count("start", "end").alias("overlaps"),
    )
    .collect()
)

print(result["overlaps"].to_list())  # [1, 2, 1]
```

Intervals include their start and exclude their end, so touching intervals do not overlap.

### Count contained intervals

```python
df = pl.DataFrame({"start": [0, 2, 4], "end": [10, 5, 12]})
df.with_columns(pi.containment_count("start", "end").alias("contained"))
# contained: [1, 0, 0]
```

A contains B when `A.start <= B.start` and `B.end <= A.end`, excluding
the same row. Duplicates count one another. Empty intervals follow these
same endpoint inequalities: `[0, 5)` contains `[5, 5)`.

### Measure nesting depth

```python
df = pl.DataFrame({"start": [0, 2, 2, 3], "end": [10, 8, 8, 7]})
df.with_columns(pi.nesting_depth("start", "end").alias("depth"))
# depth: [0, 1, 1, 2]
```

Return the length of the longest strict containment chain above each interval.
Outermost intervals have depth zero; identical intervals never add a level.
See [strict endpoint semantics and grouped examples](docs/usage.md#nesting-depth).

### Maximize coverage with a budget

```python
df = pl.DataFrame({"start": [0, -5, 6], "end": [10, 4, 15]})
selected = df.filter(pi.max_k_coverage("start", "end", k=2))
# Selects [-5, 4) and [6, 15): union measure 18.
```

Select at most `k` intervals whose union has maximum total measure. Among
maximum-coverage solutions, use the fewest intervals. The result is an exact,
deterministic Boolean mask in original row order; empty intervals are never
selected. See the [design and benchmarks](docs/coverage-benchmarks.md).

### Cover a target

Select the fewest intervals needed to cover `[0, 10)`:

```python
import polars as pl
import polars_intervals as pi

df = pl.DataFrame(
    {
        "start": [0, 0, 4, 6, 7],
        "end": [4, 6, 7, 10, 10],
    }
)
selected = df.filter(
    pi.minimum_cover(
        "start",
        "end",
        target_start=0,
        target_end=10,
    )
)

print(selected.rows())  # [(0, 6), (6, 10)]
```

## Algorithms

| Function | Description |
| --- | --- |
| [`overlap_count`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#count-overlaps) | Count the other intervals overlapping each interval. |
| [`containment_count`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#count-containment) | Count the other intervals contained by each interval. |
| [`nesting_depth`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#nesting-depth) | Length of the longest strict containment chain above each interval. |
| [`assign_lanes`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#assign-the-minimum-number-of-lanes) | Assign the fewest lanes with no overlaps within a lane. |
| [`max_weight_non_overlapping`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#select-a-globally-maximum-weight-schedule) | Select a non-overlapping subset with maximum total weight. |
| [`max_weight_with_capacity`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#select-with-a-simultaneous-capacity) | Maximize total weight under a simultaneous overlap limit. |
| [`max_weight_with_capacity_profile`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#select-with-a-capacity-profile) | Maximize total weight under a piecewise-constant capacity profile. |
| [`minimum_cover`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#cover-one-continuous-target) | Cover a target with the fewest intervals. |
| [`minimum_cost_cover`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#cover-at-minimum-cost) | Cover a target at minimum total cost. |
| [`minimum_stabbing_points`](https://github.com/jplauri/polars-intervals/blob/master/docs/usage.md#minimum-stabbing-points) | Find the fewest points that hit every interval. |

## Contributing

See the [contributing guide](https://github.com/jplauri/polars-intervals/blob/master/CONTRIBUTING.md)
for source builds and development checks. Licensed under [MIT](https://github.com/jplauri/polars-intervals/blob/master/LICENSE).

