如何在polars中将时间持续时间转换为数值?

huangapple go评论74阅读模式
英文:

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")
)

如何在polars中将时间持续时间转换为数值?

答案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
]

docs: API reference, series/timeseries

huangapple
  • 本文由 发表于 2023年2月13日 23:46:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定