如何解决熊猫将整数转换为时间
我有这样的以秒为单位的时间列
100
100000
235900
我想转换为时间格式,即
00:01
01:00
23:59
我尝试过
time = pd.to_datetime(temp['time'],format='%H%M%S').dt.time
但是会扔
ValueError: time data '0' does not match format '%H%M%S' (match)
解决方法
使用Series.str.zfill
将整数转换为字符串:
time = pd.to_datetime(temp['time'].astype(str).str.zfill(6),format='%H%M%S').dt.time
print (time)
0 00:01:00
1 10:00:00
2 23:59:00
Name: time,dtype: object
如果需要时间增量:
s = temp['time'].astype(str).str.zfill(6)
td = pd.to_timedelta(s.str[:2] + ':' + s.str[2:4] + ':' + s.str[4:])
print (td)
0 00:01:00
1 10:00:00
2 23:59:00
Name: time,dtype: timedelta64[ns]
或者:
td= pd.to_timedelta(time.astype(str))
print (td)
0 00:01:00
1 10:00:00
2 23:59:00
Name: time,dtype: timedelta64[ns]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。