首页 > 解决方案 > 时间戳:无法将 NumPy 数组转换为张量

问题描述

我有看起来像这样的熊猫数据框:

time                                value
2019-05-24 04:15:35.742000+00:00    -0.085714

当我尝试执行此操作时,在我的代码的某一时刻:

hist = model.fit(
    X_train, y_train, 
...
)

其中 X_train 源自数据框,如下所示:

array([[[Timestamp('2019-05-21 14:16:37.091000'), -0.22857142857142856,          1.3553382233088835],

我收到以下错误:

Failed to convert a NumPy array to a Tensor (Unsupported object type Timestamp)

编辑:

tr['execution_time'] = pd.to_datetime(tr.execution_time).dt.tz_localize(None) 

这也没有帮助。

标签: pythonnumpydatetimetensorpython-datetime

解决方案


首先,我们必须将其转换为datetime对象。

df['execution_time'] = pd.to_datetime(df.execution_time).dt.tz_localize(None)

之后,我们必须使用timestamp()函数将datetime对象转换为浮点值

for i in range(len(df)):
  df['execution_time'][i]=df['execution_time'][i].timestamp()

之后,我们可以将值转换为float

df = df.astype('float32')

然后它可以很容易地转换为张量。


推荐阅读