首页 > 解决方案 > Pandas / SQL 数据类型的困难?

问题描述

我有一些看起来像这样的数据

    Open    High    Low Close   Volume  Instrument
Date                        
2018-09-02 07:00:00-04:00   7269.0  7274.0  7213.0  7240.5  2321665 XBTZ18
2018-09-02 08:00:00-04:00   7240.5  7270.0  7240.5  7259.0  781280  XBTZ18
2018-09-02 09:00:00-04:00   7259.0  7259.5  7161.0  7194.5  2959099 XBTZ18
2018-09-02 10:00:00-04:00   7194.5  7238.0  7189.5  7232.0  1799117 XBTZ18
2018-09-02 11:00:00-04:00   7232.0  7245.0  7231.0  7235.0  237230  XBTZ18

数据类型在哪里:

Open          float64
High          float64
Low           float64
Close         float64
Volume          int64
Instrument     object
dtype: object

现在,我只是想将这个 pandas 数据框写入 Amazon AWS 上的 SQL 数据。

我的桌子的结构是这样的

'Date' -- > DATETIME (Primary Key, Not Null, Unique)
'Open' -- > DECIMAL
'High' -- > Decimal
'Low' -- > Decimal
'Close' -- > Decimal
'Volume' -- > INT
'INSTRUMENT' -- > VarChar(45) (Primary Key, Not Null)

现在我想将我的数据框写入 SQL 数据库:

df_for_db = data_dump[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Instrument']]
#df_for_db['Date'] = df['Date'].astype(pd.Timestamp)

#write the dataframe
df_for_db.to_sql(name='hourlyData', con=engine, if_exists = 'replace', index=False)

但我得到这个错误。

TypeError: Cannot cast DatetimeIndex to dtype datetime64[us]

上面看到的数据类型应该是兼容的,对吧?我错过了什么?谢谢你

标签: pythonmysqlstock-data

解决方案


推荐阅读