如何将 timedelta 转换为小时 [英] How to convert timedelta to hours
本文介绍了如何将 timedelta 转换为小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 timedelta 数据帧
I have a timedelta Dataframe
JC time
1 3days 21:02:05
2 1days 23:50:07
3 6days 19:28:36
但是我想要
1 93:02:05
2 47:50:07
3 163:28:36
如何转换?
推荐答案
您可以按照以下方式将 timedelta
转换为所需格式的小时数、分钟数和秒数:>
You could do as follows to convert a timedelta
to the number of hours, minutes and seconds in the format you want:
def convert_to_hours(delta):
total_seconds = delta.total_seconds()
hours = str(int(total_seconds // 3600)).zfill(2)
minutes = str(int((total_seconds % 3600) // 60)).zfill(2)
seconds = str(int(total_seconds % 60)).zfill(2)
return f"{hours}:{minutes}:{seconds}"
delta = timedelta(days=3, hours=21, minutes=2, seconds=5)
# 3 days, 21:02:05
convert_to_hours(delta)
# 93:02:05
要转换您的数据框,您可以执行以下操作:
And to convert your dataframe, you can do something like this:
df["time"] = df["time"].apply(convert_to_hours)
这篇关于如何将 timedelta 转换为小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文