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

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

How to convert time durations to numeric in polars?

问题

polars 中有没有内置函数或更好的方法来将时间持续时间转换为数字,通过定义时间分辨率(例如:天、小时、分钟)?

  1. # 创建一个数据框
  2. df = pl.DataFrame(
  3. {
  4. "from": ["2023-01-01", "2023-01-02", "2023-01-03"],
  5. "to": ["2023-01-04", "2023-01-05", "2023-01-06"],
  6. }
  7. )
  8. # 转换为日期并计算时间差
  9. df = df.with_columns(
  10. [
  11. pl.col("from").str.strptime(pl.Date, "%Y-%m-%d").alias("from_date"),
  12. pl.col("to").str.strptime(pl.Date, "%Y-%m-%d").alias("to_date"),
  13. ]
  14. ).with_columns((pl.col("to_date") - pl.col("from_date")).alias("time_diff"))
  15. # 将时间差转换为整数(以天为单位)
  16. df = df.with_columns(
  17. ((pl.col("time_diff") / (24 * 60 * 60 * 1000)).cast(pl.Int8)).alias("time_diff_int")
  18. )

希望这对你有帮助!

英文:

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

  1. # Create a dataframe
  2. df = pl.DataFrame(
  3. {
  4. "from": ["2023-01-01", "2023-01-02", "2023-01-03"],
  5. "to": ["2023-01-04", "2023-01-05", "2023-01-06"],
  6. }
  7. )
  8. # Convert to date and calculate the time difference
  9. df = df.with_columns(
  10. [
  11. pl.col("from").str.strptime(pl.Date, "%Y-%m-%d").alias("from_date"),
  12. pl.col("to").str.strptime(pl.Date, "%Y-%m-%d").alias("to_date"),
  13. ]
  14. ).with_columns((pl.col("to_date") - pl.col("from_date")).alias("time_diff"))
  15. # Convert the time difference to int (in days)
  16. df = df.with_columns(
  17. ((pl.col("time_diff") / (24 * 60 * 60 * 1000)).cast(pl.Int8)).alias("time_diff_int")
  18. )

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

答案1

得分: 3

the dt accessor lets you obtain individual components, is that what you're looking for?

  1. df["time_diff"].dt.days()
  2. Series: 'time_diff' [i64]
  3. [
  4. 3
  5. 3
  6. 3
  7. ]
  8. df["time_diff"].dt.hours()
  9. Series: 'time_diff' [i64]
  10. [
  11. 72
  12. 72
  13. 72
  14. ]
  15. df["time_diff"].dt.minutes()
  16. Series: 'time_diff' [i64]
  17. [
  18. 4320
  19. 4320
  20. 4320
  21. ]

docs: API reference, series/timeseries

英文:

the dt accessor lets you obtain individual components, is that what you're looking for?

  1. df["time_diff"].dt.days()
  2. Series: 'time_diff' [i64]
  3. [
  4. 3
  5. 3
  6. 3
  7. ]
  8. df["time_diff"].dt.hours()
  9. Series: 'time_diff' [i64]
  10. [
  11. 72
  12. 72
  13. 72
  14. ]
  15. df["time_diff"].dt.minutes()
  16. Series: 'time_diff' [i64]
  17. [
  18. 4320
  19. 4320
  20. 4320
  21. ]

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:

确定