python - 时间戳:无法将 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)
这也没有帮助。
解决方案
首先,我们必须将其转换为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')
然后它可以很容易地转换为张量。
推荐阅读
- oop - DDD:我可以将域服务作为聚合构造函数的参数传递吗
- javascript - Javascript 为什么不能在 eval() 中使用 let
- python - 如何在后台调度 Python Selenium 脚本?
- httpclient - RestSharp 到 HTTPClient RequestBody
- hibernate-tools - HibernateTools hbm2ddl ant 任务不保留顺序
- javascript - 使用条件语句过滤对象数组
- sql - Sybase SQL:分区上的行号
- javascript - 在 Javascript 中访问对象的对象
- database - 如何修复安装 mongoDB
- nginx - 如何在带有 nginx 的 debian 9 服务器上通过 https 从 phpmyadmin 发送数据?