简体   繁体   English

datetime.datetime对象的总和给出了错误TypeError:+:'datetime.datetime'和'datetime.datetime'的不支持的操作数类型

[英]sum of datetime.datetime object gave an error TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'datetime.datetime'

I'm trying to sum a list of datetime.datetime object using : 我正在尝试使用以下内容对datetime.datetime对象列表求和:

from datetime import datetime, timedelta
d= [datetime.datetime(2013, 5, 1, 9, 31, 24), datetime.datetime(2013, 6, 11, 17, 22, 18), datetime.datetime(2013, 4, 3, 16, 6, 59)]

sum_d = sum(d, timedelta())

I get the error : 我收到错误:

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

any idea what am I doing wrong or how I can sum up this list? 任何想法我做错了什么或如何总结这个列表?

I want to get the average of this list I was thinking about: 我想得到我正在考虑的这个列表的平均值:

 d_avg = sum(d, timedelta()) / len(d)

Thanks! 谢谢!

That's simply because you can't add times. 那只是因为你无法增加时间。 You can add time deltas , but not times themselves. 您可以添加时间增量 ,但不能添加时间增量

I mean, what is 3 rd Aug 1995 + 19 th June 454? 我的意思是,什么是第三 1995年8月+ 6月19 454? It doesn't make sense. 这没有意义。 You can add the distance from Year 0 but that's different to adding the dates themselves. 您可以添加第0年的距离,但这与添加日期本身不同。

What do you hope to get from this calculation? 你希望从这个计算得到什么?


If you hope to find the mean (why?), you'll have to use a less direct route 如果你希望找到平均值(为什么?),你将不得不使用较不直接的路线

import datetime

d = [datetime.datetime(2013, 5, 1, 9, 31, 24), datetime.datetime(2013, 6, 11, 17, 22, 18), datetime.datetime(2013, 4, 3, 16, 6, 59)]

d[0] + sum((d_i-d[0] for d_i in d), datetime.timedelta(0)) / len(d)
#>>> datetime.datetime(2013, 5, 5, 22, 20, 13, 666667)

This finds the sum of offsets from d[0] , means those and then adds that offset back on. 这将从d[0]找到偏移量的总和,表示这些偏移量,然后重新添加该偏移量。

You cannot sum this list: as the error message said, datetime.datetime objects cannot be added. 您无法对此列表求和:如错误消息所示,无法添加datetime.datetime对象。 What on Earth, for example, would you expect datetime.datetime(2013, 5, 1, 9, 31, 24), datetime.datetime(2013, 6, 11, 17, 22, 18) to return? 例如,您希望datetime.datetime(2013, 5, 1, 9, 31, 24), datetime.datetime(2013, 6, 11, 17, 22, 18)返回什么? Python generally won't do things that make no sense at all ;-) Python通常不会做任何根本没有意义的事情;-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:-:“ datetime.datetime”和“ int”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'int' TypeError:&:'datetime.datetime'和'bool'不支持的操作数类型 - TypeError: unsupported operand type(s) for &: 'datetime.datetime' and 'bool' TypeError:+不支持的操作数类型:“ datetime.datetime”和“ str” - TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'str' TypeError: 不支持的操作数类型 -: 'datetime.datetime' 和 'str' - TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' PySpark:TypeError:+不支持的操作数类型:'datetime.datetime'和'str' - PySpark: TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'str' - 不支持的操作数类型:“datetime.datetime”和“datetime.date” - unsupported operand type(s) for -: 'datetime.datetime' and 'datetime.date' - 不支持的操作数类型:“NoneType”和“datetime.datetime” - unsupported operand type(s) for -: 'NoneType' and 'datetime.datetime' - 不支持的操作数类型:“str”和“datetime.datetime” - unsupported operand type(s) for -: 'str' and 'datetime.datetime' 会计编码“(不支持的操作数类型-:'str'和'datetime.datetime')” - accounting coding "(unsupported operand type(s) for -: 'str' and 'datetime.datetime')" 使用datetime的Discord.py机器人会返回此错误(TypeError:-:“ datetime.datetime”和“ int”的不受支持的操作数类型) - Discord.py bot using datetime returns this (TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'int')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM