我想把数据写入mysql数据库。我首先从数据库中读取当前数据并计算新值。新值应按与数据库中数据相同的顺序写入,如下所示。我不想覆盖现有数据。我不想使用-proc:none
。
我收到以下错误消息:
(mysql.connector.errors.DatabaseError)1265(01000):数据被截断 对于列' log_return'在第1行[SQL:' INSERT INTO
完整的代码如下。
to_sql
数据库如下所示。
import sqlalchemy as sqlal
import pandas as pd
import numpy as np
mysql_engine = sqlal.create_engine(xxx)
mysql_engine.raw_connection()
metadata = sqlal.MetaData()
product = sqlal.Table('product', metadata,
sqlal.Column('ticker', sqlal.String(10), primary_key=True, nullable=False, unique=True),
sqlal.Column('isin', sqlal.String(12), nullable=True),
sqlal.Column('product_name', sqlal.String(80), nullable=True),
sqlal.Column('currency', sqlal.String(3), nullable=True),
sqlal.Column('market_data_source', sqlal.String(20), nullable=True),
sqlal.Column('trading_location', sqlal.String(20), nullable=True),
sqlal.Column('country', sqlal.String(20), nullable=True),
sqlal.Column('sector', sqlal.String(80), nullable=True)
)
market_price_data = sqlal.Table('market_price_data', metadata,
sqlal.Column('Date', sqlal.DateTime, nullable=True),
sqlal.Column('ticker', sqlal.String(10), sqlal.ForeignKey('product.ticker'), nullable=True),
sqlal.Column('adj_close', sqlal.Float, nullable=True),
sqlal.Column('log_return', sqlal.Float, nullable=True)
)
metadata.create_all(mysql_engine)
GetTimeSeriesLevels = pd.read_sql_query('SELECT Date, ticker, adj_close FROM market_price_data Order BY ticker ASC', mysql_engine)
GetTimeSeriesLevels['log_return'] = np.log(GetTimeSeriesLevels.groupby('ticker')['adj_close'].apply(lambda x: x.div(x.shift(1)))).dropna()
GetTimeSeriesLevels['log_return'].fillna('NULL', inplace=True)
insert_yahoo_data = market_price_data.insert().values(GetTimeSeriesLevels [['log_return']].to_dict('records'))
mysql_engine.execute(insert_yahoo_data)
它应该是这样的:
Date ticker adj_close log_return
2016-11-21 00:00:00 AAPL 111.73 NULL
2016-11-22 00:00:00 AAPL 111.8 NULL
2016-11-23 00:00:00 AAPL 111.23 NULL
2016-11-25 00:00:00 AAPL 111.79 NULL
2016-11-28 00:00:00 AAPL 111.57 NULL
2016-11-23 00:00:00 ACN 119.82 NULL
2016-11-25 00:00:00 ACN 120.74 NULL
2016-11-28 00:00:00 ACN 120.76 NULL
2016-11-29 00:00:00 ACN 120.94 NULL
2016-11-30 00:00:00 ACN 119.43 NULL
...
答案 0 :(得分:1)
虽然可耻,我不知道sqlalchemy只有原始SQL,考虑将pandas数据框转储到临时表中,然后将其与最终表连接:
# DUMP TO TEMP TABLE (REPLACING EACH TIME)
GetTimeSeriesLevels.to_sql(name='log_return_temp', con=mysql_engine, if_exists='replace',
index=False)
# SQL UPDATE (USING TRANSACTION)
with engine.begin() as conn:
conn.execute("UPDATE market_price_data f" +
" INNER JOIN log_return_temp t" +
" ON f.Date = t.Date" +
" AND f.ticker = t.ticker" +
" SET f.log_return = t.log_return;")
engine.dispose()
或者,考虑直接在MySQL中进行日志转换!从我所看到的,在你的pandas / numpy代码中,你记录了当前行adj_close
与最后一行adj_close
的商的变换。 MySQL可以运行自联接来排列当前和最后一行。并且MySQL在其mathematical operators中维护自然日志。
下面是可以使用CREATE AS ...
转储到临时表的select语句,或者转换为带有嵌套UPDATE
语句的复杂SELECT
查询:
SELECT t1.*, LOG(t1.adj_close / t2.adj_close) As log_return
FROM
(SELECT m.Date, m.ticker, m.adj_close,
(SELECT Count(*) FROM market_price_data sub
WHERE sub.Date <= m.Date AND sub.ticker = m.ticker) AS rank
FROM market_price_data m) As t1
INNER JOIN
(SELECT m.Date, m.ticker, m.adj_close,
(SELECT Count(*) FROM market_price_data sub
WHERE sub.Date <= m.Date AND sub.ticker = m.ticker) AS rank
FROM market_price_data m) As t1
ON t1.rank = (t2.rank - 1) AND t1.ticker = t2.ticker AND t1.Date = t2.Date