Reputation: 79
I want to sum data1
, which is 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
total = sum(df.data1) But, I got an error like this:
---------------------------------------------------------------------------
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'
How can I correct it?
Upvotes: 2
Views: 64
Reputation: 164623
You can perform this calculation with pandas
:
df = pd.DataFrame({'Time': ['00:06:44', '00:00:34', '00:02:32' ,'00:00:18',
'00:03:42', '23:59:58']})
res = pd.to_timedelta(df['Time']).dt.seconds.sum() # 87228
Upvotes: 0
Reputation: 862511
There is problem sum
of datetime.time
s is not implemented in python, so possible solution are convert values to_timedelta
s first:
total = pd.to_timedelta(df.data1.astype(str)).sum()
print (total)
1 days 00:13:48
print (int(total.total_seconds()))
87228
Or use generator
with sum
components of times:
total = sum(x.hour *3600 + x.minute * 60 + x.second for x in df.data1)
print (total)
87228
Upvotes: 1