我是 NumPy
的新手,我正在尝试计算一些简单的统计数据,例如 median
或 stddev
。
我想探索的“列”之一是时差(它的类型是 timedelta64
NumPy
类型),但我不能应用那些统计 ufuncs
直接:
----> 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'
为什么会这样?
我知道我应该研究一下 Pandas
,但首先我想熟悉一下 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
是否可以超过它。)
关于python - 为什么 timedeltas 不能在 NumPy 中进行乘法/除法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11593382/