You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中list对象无strftime属性报错原因排查问询

问题解答

报错原因

这个报错既不是格式错误也不是缺少模块导致的,核心是代码逻辑错误:

  • costs_for_roi.index.values是numpy的datetime64[ns]类型数组,你通过list()把它转成Python列表后,列表对象本身没有strftime方法——strftime是单个datetime类对象的专属方法,不能直接调用在列表上。

修复方案

方案1:直接利用pandas索引的内置方法(推荐)

因为costs_for_roi看起来是pandas的DataFrame/Series对象,直接调用索引的strftime方法再转列表即可:

datelist = costs_for_roi.index.strftime("%Y-%m-%d %H:%M:%S").tolist()

方案2:处理numpy datetime数组

如果要直接操作numpy数组,有两种方式:

  1. 使用numpy自带的日期格式化方法:
import numpy as np
datelist = np.datetime_as_string(costs_for_roi.index.values, unit='s').tolist()
  1. 循环遍历每个datetime元素调用strftime:
datelist = [dt.strftime("%Y-%m-%d %H:%M:%S") for dt in costs_for_roi.index.values]

内容的提问来源于stack exchange,提问作者Nneka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:45:11