polars
中是否有任何内置函数或更好的方法通过定义时间分辨率(例如:天、小时、分钟)将持续时间转换为数字?
import polars as pl
df = pl.DataFrame({
"from": ["2023-01-01", "2023-01-02", "2023-01-03"],
"to": ["2023-01-04", "2023-01-05", "2023-01-06"],
})
我目前的做法:
# Convert to date and calculate the time difference
df = (
df.with_columns(
pl.col("to", "from").str.to_date().name.suffix("_date")
)
.with_columns((pl.col("to_date") - pl.col("from_date")).alias("time_diff"))
)
# Convert the time difference to int (in days)
df = df.with_columns(
((pl.col("time_diff") / (24 * 60 * 60 * 1000)).cast(pl.Int8)).alias("time_diff_int")
)
输出:
shape: (3, 6)
┌────────────┬────────────┬────────────┬────────────┬──────────────┬───────────────┐
│ from ┆ to ┆ to_date ┆ from_date ┆ time_diff ┆ time_diff_int │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ date ┆ date ┆ duration[ms] ┆ i8 │
╞════════════╪════════════╪════════════╪════════════╪══════════════╪═══════════════╡
│ 2023-01-01 ┆ 2023-01-04 ┆ 2023-01-04 ┆ 2023-01-01 ┆ 3d ┆ 3 │
│ 2023-01-02 ┆ 2023-01-05 ┆ 2023-01-05 ┆ 2023-01-02 ┆ 3d ┆ 3 │
│ 2023-01-03 ┆ 2023-01-06 ┆ 2023-01-06 ┆ 2023-01-03 ┆ 3d ┆ 3 │
└────────────┴────────────┴────────────┴────────────┴──────────────┴───────────────┘
dt
访问器可让您获取单独的组件,这就是您正在寻找的吗?
df["time_diff"].dt.days()
Series: 'time_diff' [i64]
[
3
3
3
]
df["time_diff"].dt.hours()
Series: 'time_diff' [i64]
[
72
72
72
]
df["time_diff"].dt.minutes()
Series: 'time_diff' [i64]
[
4320
4320
4320
]