gpt4 book ai didi

python - 如何将整数(秒)添加到 hh :mm:ss format in Python?

转载 作者:行者123 更新时间:2023-11-30 22:32:23 27 4
gpt4 key购买 nike

我在 python 中有以下数据框,其中尝试通过将“持续时间”(以秒为单位)添加到“start_time”来计算“新时间”列

Serial  start_date     start_time     Duration(seconds)  New time
A 5/22/2017 10:37:24 216
A 5/22/2017 10:37:26 213
A 5/22/2017 10:37:29 3
A 5/22/2017 10:39:55 60
A 5/22/2017 10:51:50 380
A 5/22/2017 10:51:57 339

我想将持续时间添加到 start_time 中。持续时间以秒为单位。“新时间”应采用 hh:mm:ss 格式。

我尝试在论坛中寻找类似的查询,但无法解决这个问题。

以下是信息

data.info()

start_date 13661 non-null object
start_time 13661 non-null object
Duration 13661 non-null int64

我尝试使用日期时间从论坛中的类似问题中获取线索

data.newtime = data.start_time + datetime.timedelta(data.Duration)

当我执行此操作时,出现以下错误:TypeError:timedelta 天组件不支持的类型:系列

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-95-fdfac1490ba5> in <module>()
----> 1 data.newtime = data.start_time + datetime.timedelta(data.Duration)

TypeError: unsupported type for timedelta days component: Series

不知道该怎么做。 Python 新手。

感谢帮助TIA

最佳答案

您可以使用to_timedelta输出也是timedelta:

df['New time'] = pd.to_timedelta(df['start_time']) + 
pd.to_timedelta(df['Duration(seconds)'], unit='s')
print (df)
Serial start_date start_time Duration(seconds) New time
0 A 5/22/2017 10:37:24 216 10:41:00
1 A 5/22/2017 10:37:26 213 10:40:59
2 A 5/22/2017 10:37:29 3 10:37:32
3 A 5/22/2017 10:39:55 60 10:40:55
4 A 5/22/2017 10:51:50 380 10:58:10
5 A 5/22/2017 10:51:57 339 10:57:36

但是如果秒数更多,输出就会改变,因为还有天:

print (df)
Serial start_date start_time Duration(seconds)
0 A 5/22/2017 10:37:24 216
1 A 5/22/2017 10:37:26 213000
2 A 5/22/2017 10:37:29 3
3 A 5/22/2017 10:39:55 60
4 A 5/22/2017 10:51:50 380
5 A 5/22/2017 10:51:57 339

df['New time'] = pd.to_timedelta(df['start_time']) +
pd.to_timedelta(df['Duration(seconds)'], unit='s')
print (df)
Serial start_date start_time Duration(seconds) New time
0 A 5/22/2017 10:37:24 216 0 days 10:41:00
1 A 5/22/2017 10:37:26 213000 2 days 21:47:26
2 A 5/22/2017 10:37:29 3 0 days 10:37:32
3 A 5/22/2017 10:39:55 60 0 days 10:40:55
4 A 5/22/2017 10:51:50 380 0 days 10:58:10
5 A 5/22/2017 10:51:57 339 0 days 10:57:36
<小时/>

也可以添加日期时间:

df['New date'] = pd.to_datetime(df['start_date']) + \
pd.to_timedelta(df['start_time']) + \
pd.to_timedelta(df['Duration(seconds)'], unit='s')
print (df)
Serial start_date start_time Duration(seconds) New date
0 A 5/22/2017 10:37:24 216 2017-05-22 10:41:00
1 A 5/22/2017 10:37:26 213 2017-05-22 10:40:59
2 A 5/22/2017 10:37:29 3 2017-05-22 10:37:32
3 A 5/22/2017 10:39:55 60 2017-05-22 10:40:55
4 A 5/22/2017 10:51:50 380 2017-05-22 10:58:10
5 A 5/22/2017 10:51:57 339 2017-05-22 10:57:36
<小时/>
df['New date'] = pd.to_datetime(df['start_date']) + \
pd.to_timedelta(df['start_time']) + \
pd.to_timedelta(df['Duration(seconds)'], unit='s')
print (df)
Serial start_date start_time Duration(seconds) New date
0 A 5/22/2017 10:37:24 216 2017-05-22 10:41:00
1 A 5/22/2017 10:37:26 213000 2017-05-24 21:47:26
2 A 5/22/2017 10:37:29 3 2017-05-22 10:37:32
3 A 5/22/2017 10:39:55 60 2017-05-22 10:40:55
4 A 5/22/2017 10:51:50 380 2017-05-22 10:58:10
5 A 5/22/2017 10:51:57 339 2017-05-22 10:57:36

---

如果需要将timedelta转换为字符串,格式为HH:MM:SS和丢失的(如果存在):

df['New time'] = pd.to_timedelta(df['start_time']) + 
pd.to_timedelta(df['Duration(seconds)'], unit='s')
df['New time'] = pd.to_datetime(df['New time']).dt.strftime('%H:%M:%S')
print (df)
Serial start_date start_time Duration(seconds) New time
0 A 5/22/2017 10:37:24 216 10:41:00
1 A 5/22/2017 10:37:26 213000 21:47:26
2 A 5/22/2017 10:37:29 3 10:37:32
3 A 5/22/2017 10:39:55 60 10:40:55
4 A 5/22/2017 10:51:50 380 10:58:10
5 A 5/22/2017 10:51:57 339 10:57:36

关于python - 如何将整数(秒)添加到 hh :mm:ss format in Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45481953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com