python - 为什么 timedeltas 不能在 NumPy 中进行乘法/除法?

标签 python datetime numpy

我是 NumPy 的新手,我正在尝试计算一些简单的统计数据,例如 medianstddev

我想探索的“列”之一是时差(它的类型是 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/

相关文章:

php - Symfony3 Doctrine 使用时区

python - 在 NumPy 中将逐元素乘法和矩阵乘法与多维数组相结合

python - 字符串可以变成变量吗?

Python 2.7 getfullargspec 等效项

python - 每次 Spark 窗口函数

python - 什么决定了 Pandas 的最小和最大时间戳?

c# - 应用程序查询时 Azure 移动服务字符串/日期列出现问题

python - 在 pytables CArray 中查找最大非无穷大元素

python - 强力字符串匹配算法的运行时

python - - vs -= 运算符与 numpy