Код: Выделить всё
df_a = pl.from_repr("""
┌─────────────────────┬───────┐
│ arrival_time ┆ Train │
│ --- ┆ --- │
│ datetime[ns] ┆ i64 │
╞═════════════════════╪═══════╡
│ 2024-10-04 08:40:10 ┆ 112 │
│ 2024-10-04 19:31:26 ┆ 134 │
└─────────────────────┴───────┘
""")
Код: Выделить всё
df_b = pl.from_repr("""
┌─────────────────────┬───────────┐
│ Time ┆ Period │
│ --- ┆ --- │
│ datetime[ns] ┆ str │
╞═════════════════════╪═══════════╡
│ 2024-10-04 08:00:00 ┆ Early │
│ 2024-10-04 16:00:00 ┆ Afternoon │
└─────────────────────┴───────────┘
""")
Код: Выделить всё
┌─────────────────────┬───────┬───────────┐
│ arrival_time ┆ Train ┆ Period │
│ --- ┆ --- ┆ --- │
│ datetime[ns] ┆ i64 ┆ str │
╞═════════════════════╪═══════╪═══════════╡
│ 2024-10-04 08:40:10 ┆ 112 ┆ Early │
│ 2024-10-04 19:31:26 ┆ 134 ┆ Afternoon │
└─────────────────────┴───────┴───────────┘
Код: Выделить всё
min(dict(zip( df.Period,df.Time)).items(), key=lambda x: abs(pl.col('arrival_time') - x[1]))[0])
Подробнее здесь: https://stackoverflow.com/questions/726 ... comparison