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

如何将pandas的DatetimeIndex转换为Timestamp数组?

解决DatetimeIndex转Timestamp数组及儒略日计算问题

问题原因

DatetimeIndex的底层存储元素是numpy.datetime64类型,而pd.Timestamp.to_julian_date方法仅支持pandas.Timestamp对象,直接传入DatetimeIndex会触发'numpy.datetime64' object has no attribute 'year'错误。

最简解决方法(推荐)

DatetimeIndex本身内置了to_julian_date方法,无需额外用np.vectorize包装,直接调用即可完成儒略日计算:

# 直接通过索引计算儒略日
julianDay = Weather.index.to_julian_date()

转换为Timestamp数组的方法

如果确实需要将DatetimeIndex转换为Timestamp数组,可通过以下方式实现:

# 将DatetimeIndex转换为Timestamp数组
timestamp_array = Weather.index.to_series().apply(pd.Timestamp).values
# 再使用你原来的vectorize方法计算儒略日
get_julianDate = np.vectorize(pd.Timestamp.to_julian_date)
julianDay = get_julianDate(timestamp_array)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 04:45:29