Код: Выделить всё
import polars as pl
df = pl.from_repr("""
┌─────┬────────────┐
│ uid ┆ dates │
│ --- ┆ --- │
│ i64 ┆ date │
╞═════╪════════════╡
│ 1 ┆ 2025-11-21 │
│ 2 ┆ 2025-11-22 │
│ 3 ┆ 2025-11-23 │
│ 4 ┆ 2025-11-24 │
│ 5 ┆ 2025-11-25 │
└─────┴────────────┘
""")
# Self join_asof on date
self_join = df.join_asof(
df,
on="dates",
strategy="nearest",
allow_exact_matches=False,
coalesce=False,
)
Код: Выделить всё
┌─────┬────────────┬───────────┬─────────────┐
│ uid ┆ dates ┆ uid_right ┆ dates_right │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ date ┆ i64 ┆ date │
╞═════╪════════════╪═══════════╪═════════════╡
│ 1 ┆ 2025-11-21 ┆ 1 ┆ 2025-11-21 │
│ 2 ┆ 2025-11-22 ┆ 2 ┆ 2025-11-22 │
│ 3 ┆ 2025-11-23 ┆ 3 ┆ 2025-11-23 │
│ 4 ┆ 2025-11-24 ┆ 4 ┆ 2025-11-24 │
│ 5 ┆ 2025-11-25 ┆ 5 ┆ 2025-11-25 │
└─────┴────────────┴───────────┴─────────────┘
Подробнее здесь: https://stackoverflow.com/questions/798 ... ct-matches
Мобильная версия