我需要在单独的函数中从 pandas 帧值转换日期:
def myfunc(lat, lon, when):
ts = (when - np.datetime64('1970-01-01T00:00:00Z','s')) / np.timedelta64(1, 's')
date = datetime.datetime.utcfromtimestamp(ts)
print("Numpy date= ", when, " Python date= ", date)
return float(90) - next_func(lat, lon, date)
调用这个函数:
new_df['new_column'] = np.vectorize(my_func)(lat, lon, new_df['datetime(LT)'])
但它引发错误:
ufunc subtract cannot use operands with types dtype('int64') and dtype('<M8[s]')
如何将 numpy datetime64 [ns] 转换为 python datetime?
原文由 Boris Salimov 发布,翻译遵循 CC BY-SA 4.0 许可协议
我想知道您是否需要所有这些转换工作。使用正确的时间单位 a
datetime64
可以直接生成datetime
对象。我不确定你的
when
变量,但我们假设它来自pandas
,并且类似于DatetimeIndex
:70–等效的 numpy 数组
使用
[ns]
结果是一个大整数,某种“时间戳”。但是,如果我将时间单位转换为秒甚至微秒(us)之类的东西:结果是
datetime
对象的列表。