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

如何在Pandas DataFrame的列上调用timestamp()方法?

解决Pandas datetime列批量转时间戳的问题

你的问题出在旧版本Pandas的dt访问器没有timestamp()方法,所以直接调用rows.time.dt.timestamp()会触发AttributeError。以下是几种无需逐行循环的整列处理方案:

  • 方案一:用apply批量调用timestamp
    直接对整列的datetime对象应用timestamp()方法,写法简洁:

    rows['time'].apply(lambda x: x.timestamp())
    
  • 方案二:手动计算时间戳(兼容所有版本)
    通过计算datetime列与Unix纪元时间(1970-01-01)的秒数差得到时间戳,不受Pandas版本限制:

    (rows['time'] - pd.Timestamp('1970-01-01')) // pd.Timedelta('1s')
    
  • 方案三:升级Pandas版本
    若你的Pandas版本低于1.1.0,升级到新版本后,dt.timestamp()方法即可直接使用,执行你原本的代码就行:

    rows.time.dt.timestamp()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 23:15:57