datetime.time数据集的总和

时间:2018-04-15 14:06:16

标签: python pandas datetime

我想总结data1,即pandas.core.series.Series。

data1:

0        00:06:44
1        00:00:34
2        00:02:32
3        00:00:18
4        00:03:42
5        23:59:58

总计=总和(df.data1) 但是,我收到了这样的错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-128-9cc06aed3448> in <module>()
      4 
      5 #print(df.data1)
----> 6 total = sum(df.data1)

TypeError: unsupported operand type(s) for +: 'int' and 'datetime.time'

我该如何纠正?

2 个答案:

答案 0 :(得分:1)

sum的问题datetime.time未在python中实现,因此可能的解决方案是首先转换值to_timedelta

total = pd.to_timedelta(df.data1.astype(str)).sum()
print (total)
1 days 00:13:48

print (int(total.total_seconds()))
87228

或者将generatorsum次成分一起使用:

total = sum(x.hour *3600 + x.minute * 60 + x.second for x in df.data1)
print (total)
87228

答案 1 :(得分:0)

您可以使用 <a id="show_login_modal" href="javascript:void(0)">Login</a> 执行此计算:

pandas