英文:
How to convert time durations to numeric in polars?
问题
在 polars
中有没有内置函数或更好的方法来将时间持续时间转换为数字,通过定义时间分辨率(例如:天、小时、分钟)?
# 创建一个数据框
df = pl.DataFrame(
{
"from": ["2023-01-01", "2023-01-02", "2023-01-03"],
"to": ["2023-01-04", "2023-01-05", "2023-01-06"],
}
)
# 转换为日期并计算时间差
df = df.with_columns(
[
pl.col("from").str.strptime(pl.Date, "%Y-%m-%d").alias("from_date"),
pl.col("to").str.strptime(pl.Date, "%Y-%m-%d").alias("to_date"),
]
).with_columns((pl.col("to_date") - pl.col("from_date")).alias("time_diff"))
# 将时间差转换为整数(以天为单位)
df = df.with_columns(
((pl.col("time_diff") / (24 * 60 * 60 * 1000)).cast(pl.Int8)).alias("time_diff_int")
)
希望这对你有帮助!
英文:
Is there any built-in function in polars
or a better way to convert time durations to numeric by defining the time resolution (e.g.: days, hours, minutes)?
# Create a dataframe
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("from").str.strptime(pl.Date, "%Y-%m-%d").alias("from_date"),
pl.col("to").str.strptime(pl.Date, "%Y-%m-%d").alias("to_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")
)
答案1
得分: 3
the dt
accessor lets you obtain individual components, is that what you're looking for?
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
]
docs: API reference, series/timeseries
英文:
the dt
accessor lets you obtain individual components, is that what you're looking for?
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
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论