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

LSTM多变量时间序列预测报错:float()参数不能为Timestamp类型

解决LSTM归一化报错与时间转Unix格式方法

一、解决TypeError: float() argument must be a string or a number, not 'Timestamp'

报错核心原因:归一化工具(比如MinMaxScaler)仅支持数值型数据,而你的数据集里包含date_time这个Timestamp类型列,工具尝试将时间戳转为浮点型时失败。

解决步骤:

  • 先把时间列从待归一化数据中拆分,只对数值列(比如Close)执行归一化
  • 归一化完成后,再将时间列与处理后的数值列合并

示例代码:

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

# 假设数据存储在df中,包含date_time和Close列
# 分离时间列与数值列
time_data = df['date_time'].copy()
numeric_cols = df[['Close']]

# 初始化归一化器并处理数值列
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_numeric = scaler.fit_transform(numeric_cols)

# 转回数据框并合并时间列
scaled_df = pd.DataFrame(scaled_numeric, columns=['Scaled_Close'])
scaled_df['date_time'] = time_data
# 调整列顺序(可选)
scaled_df = scaled_df[['date_time', 'Scaled_Close']]

二、将时间转为Unix格式的操作

Unix时间戳是从1970-01-01 UTC到目标时间的秒数/毫秒数,用Pandas可快速实现转换:

1. 转换为秒级Unix时间戳

# 简洁写法
df['unix_seconds'] = df['date_time'].apply(lambda x: x.timestamp())

# 更高效的方式(利用Timestamp的纳秒存储特性)
df['unix_seconds'] = df['date_time'].view('int64') // 10**9

2. 转换为毫秒级Unix时间戳

如果模型需要毫秒级精度:

df['unix_milliseconds'] = df['date_time'].view('int64') // 10**6

注意:若date_time还不是Timestamp类型,需先转换:df['date_time'] = pd.to_datetime(df['date_time'])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 19:15:48