如何在Python中将整数(秒)添加到hh:mm:ss格式?
我在python中有以下数据帧,我试图通过在“开始时间”中添加“持续时间”(以秒为单位)来计算列“新时间”如何在Python中将整数(秒)添加到hh:mm:ss格式?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我在python中有以下数据帧,我试图通过在“开始时间”中添加“持续时间”(以秒为单位)来计算列“新时间” 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
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
我想在开始时间中添加持续时间。持续时间以秒为单位。
“新时间”应为hh:mm:ss格式
我试图在论坛上寻找类似的问题,但无法回避
以下是信息
data.info()
start_date 13661 non-null object
start_time 13661 non-null object
Duration 13661 non-null int64
我试着从论坛上的一个类似问题中得到提示,使用datetime
data.newtime = data.start_time + datetime.timedelta(data.Duration)
当我执行此命令时,我得到以下错误:TypeError:timedelta-days组件的不支持类型:Series
---------------------------------------------------------------------------
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
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 data.newtime=data.start\u time+datetime.timedelta(data.Duration)
TypeError:timedelta days组件的类型不受支持:Series
我不知道该怎么办。python新手
感谢您的帮助
TIA以下是一个片段,可以帮助您自己解决问题:
from datetime import datetime, timedelta
my_date = datetime.strptime('5/22/2017 10:37:24', '%m/%d/%Y %H:%M:%S')
my_time_diff = timedelta(seconds=216)
my_new_date = my_date + my_time_diff
print(my_new_date.strftime('%m/%d/%Y %H:%M:%S'))
有用资源:
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
--- 如果需要将
timedelta
转换为string
,格式为HH:MM:SS
和lostdays
(如果存在):
@马克西米利安:我试过引用这个。在这里,我想为数据帧中的每个行时间条目计算这个值。我已经编辑了这个错误,我得到了。谢谢!现在问题很清楚了。请注意,如果您对日志数据执行此操作,则闰秒(Python忽略)的存在可能会影响您的计算。通常只在物理实验和金融交易中重要的时间戳。很高兴能帮上忙!天气真好!我对各种选择投了赞成票。又是一个漂亮的!
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
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