I'm struggling with following problem:
How can I sum a list of timedeltas like:
[datetime.timedelta(0, 3600), datetime.timedelta(0, 3600), datetime.timedelta(0, 7200)]
which should result in datetime.timedelta(0, 14400) => 4 hours
Try this,
import datetime
print sum(time_list,datetime.timedelta())
Result:
datetime.timedelta(0, 14400)
Pass datetime.timedelta(0,0)
to sum
as init value.
import datetime
arr = [datetime.timedelta(0, 3600), datetime.timedelta(0, 3600), datetime.timedelta(0, 7200)]
print(sum(arr, datetime.timedelta(0,0)))
Output:
4:00:00
You can use pandas
import pandas as pd
df=pd.DataFrame([datetime.timedelta(0, 3600), datetime.timedelta(0, 3600), datetime.timedelta(0, 7200)])
df[0].sum()
Timedelta('0 days 04:00:00')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With