python – 为什么timedeltas不能乘以/除以NumPy?

我是NumPy的新手,我正在尝试计算一些简单的统计数据,如中位数或stddev.

我想要探索的“列”之一是时间差(其类型为timedelta64 NumPy类型),但我无法直接应用这些统计ufunc:

----> 1 age_request.std()

TypeError: ufunc 'divide' not supported for the input types, and the inputs could not be 
safely coerced to any supported types according to the casting rule 'safe'

为什么会这样?

我知道我应该看看熊猫,但首先我想熟悉NumPy.

最佳答案 查看
documentation for datetime.它列出了您可以对timedelta对象执行的操作.分区是这样完成的:

t1 = t2 // i

请注意,这会计算出地板并丢弃任何剩余部分.

据我所知,你只能对timedelta对象进行这些操作.虽然也许我错了,你可以使用numpy操作.

我建议将timedelta对象转换为微秒,然后进行除法,标准偏差,中位数等:

tdMicrosecs = td.microseconds + 1000.0 * td.seconds + 86400000000.0 * td.days
tdQuotient = tdMicrosecs / i

(我应该补充一点,我不完全确定python可以存储的最大整数值是什么,以及tdMicrosecs是否可以超过它.)

点赞