python - 将 timedelta 转换为 int

标签 python

我想将 timedelta 转换为 int,以便我可以用它做数学运算,但我一直收到此错误:

numpy.core._exceptions._UFuncBinaryResolutionError: ufunc 'true_divide' cannot use operands with types dtype('O') and dtype('<m8[D]')

代码如下:

zeit = datetime(2023, 1, 1)
    today = datetime.now()

    new_date = zeit-today

days_int=(new_date / np.timedelta64(1, 'D')).astype(int)

最佳答案

如果您只想转换 int 中的天数,请使用 new_date.days

关于python - 将 timedelta 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67102352/

相关文章:

python - 我怎样才能以编程方式将笔记发布到 Google 阅读器?

Python 时间戳正则表达式

python - AttributeError: 'list' 对象在链表 LeetCode 挑战中没有属性 'val'

python - python中的复合排序

python - Tensorflow无法训练模型

javascript - 是否可以将跨浏览器扩展链接到 Python Web 应用程序后端?

python - 在 python Azure Functions 中自定义日志格式

Python3 最高效的 String 到 Float 转换

python - Tensorflow:更新不可训练模型层的权重

python - 神经网络只预测一类,所有用于测试的输出层激活变成0.999~