错误“'Timestamp' 对象没有属性 'translate'”
Error "'Timestamp' object has no attribute 'translate'"
最近,我开始研究股票价格分析以优化我的投资组合。我从一个 Excel 文件和几个 VBA 宏开始。它工作得很好,但速度很慢。所以,我现在正尝试在我的服务器 (based on this post) 上建立一个合适的 "stock prices" 数据库。
在 "stock_prices" 数据库中,有一个 "daily_price" table 存储了一些代码的每日股票价格。为了更新 "daily price" table,每天都会启动一个 python 脚本,它包含以下 Python / SQL 语句。
df = pdr.get_data_yahoo(ticker, start_date)
for row in df.itertuples():
values = [YAHOO_VENDOR_ID, ticker_index[ticker]] + list(row)
cursor.execute("INSERT INTO daily_price (data_vendor_id, ticker_id, price_date, open_price, high_price, low_price, close_price, adj_close_price, volume) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", tuple(values))
不幸的是,"cursor.execute..." 行 returns 出现以下错误:
"AttributeError: 'Timestamp' object has no attribute 'translate'"
"values" 元组的输出是:
[1, 2, 时间戳('2004-08-19 00:00:00'), 49.81328582763672, 51.83570861816406, 47.80083084106445, 49.9826545715332, 49.982654571533=11360][7=44]8
根据我在另一个类似 post 中可以读到的内容,我检查了日期索引的类型以确保它不是对象:
Print(df.index.dtype)
这个returns"datetime64[ns]"好像不错
最后,在数据库中,我尝试将数据类型从"Date"更改为"Datetime",但这并没有解决错误。
有人可以分享一些有关如何解决此错误的提示吗?
此致,
2020 年 4 月 25 日编辑:最终解决方案
df = pdr.get_data_yahoo(ticker, start_date)
df = df.reset_index()
df.columns = ['price_date', 'open_price', 'high_price', 'low_price', 'close_price', 'adj_close_price', 'volume']
df['data_vendor_id'] = YAHOO_VENDOR_ID
df['ticker_id'] = ticker_index[ticker]
df = df[['data_vendor_id','ticker_id','price_date', 'open_price', 'high_price', 'low_price', 'close_price', 'adj_close_price', 'volume']]
df['price_date'] = df['price_date'].dt.strftime('%Y-%m-%d %H:%M:%S')
print(df)
cursor.executemany("INSERT INTO daily_price (data_vendor_id, ticker_id, price_date, open_price, high_price, low_price, close_price, adj_close_price, volume) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", df.to_numpy().tolist())
考虑将日期时间列转换为时间的字符串表示,并使用 DataFrame.to_numpy()
而不是 iterrows
方法:
df = pdr.get_data_yahoo(ticker, start_date)
# ADD NEW COLUMNS
df["data_vendor_id"] = YAHOO_VENDOR_ID
df["ticker_id"] = ticker_index[ticker]]
# CONVERT DATE TO STRING TIME
df["DATE"] = df["DATE"].dt.strftime('%Y-%m-%d %H:%M:%S')
sql = '''INSERT INTO daily_price (data_vendor_id, ticker_id, price_date,
open_price, high_price, low_price,
close_price, adj_close_price, volume)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
'''
# LIST
cursor.executemany(sql, df.to_numpy().tolist())
conn.commit()
最近,我开始研究股票价格分析以优化我的投资组合。我从一个 Excel 文件和几个 VBA 宏开始。它工作得很好,但速度很慢。所以,我现在正尝试在我的服务器 (based on this post) 上建立一个合适的 "stock prices" 数据库。
在 "stock_prices" 数据库中,有一个 "daily_price" table 存储了一些代码的每日股票价格。为了更新 "daily price" table,每天都会启动一个 python 脚本,它包含以下 Python / SQL 语句。
df = pdr.get_data_yahoo(ticker, start_date)
for row in df.itertuples():
values = [YAHOO_VENDOR_ID, ticker_index[ticker]] + list(row)
cursor.execute("INSERT INTO daily_price (data_vendor_id, ticker_id, price_date, open_price, high_price, low_price, close_price, adj_close_price, volume) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", tuple(values))
不幸的是,"cursor.execute..." 行 returns 出现以下错误: "AttributeError: 'Timestamp' object has no attribute 'translate'"
"values" 元组的输出是: [1, 2, 时间戳('2004-08-19 00:00:00'), 49.81328582763672, 51.83570861816406, 47.80083084106445, 49.9826545715332, 49.982654571533=11360][7=44]8
根据我在另一个类似 post 中可以读到的内容,我检查了日期索引的类型以确保它不是对象:
Print(df.index.dtype)
这个returns"datetime64[ns]"好像不错
最后,在数据库中,我尝试将数据类型从"Date"更改为"Datetime",但这并没有解决错误。
有人可以分享一些有关如何解决此错误的提示吗?
此致,
2020 年 4 月 25 日编辑:最终解决方案
df = pdr.get_data_yahoo(ticker, start_date)
df = df.reset_index()
df.columns = ['price_date', 'open_price', 'high_price', 'low_price', 'close_price', 'adj_close_price', 'volume']
df['data_vendor_id'] = YAHOO_VENDOR_ID
df['ticker_id'] = ticker_index[ticker]
df = df[['data_vendor_id','ticker_id','price_date', 'open_price', 'high_price', 'low_price', 'close_price', 'adj_close_price', 'volume']]
df['price_date'] = df['price_date'].dt.strftime('%Y-%m-%d %H:%M:%S')
print(df)
cursor.executemany("INSERT INTO daily_price (data_vendor_id, ticker_id, price_date, open_price, high_price, low_price, close_price, adj_close_price, volume) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", df.to_numpy().tolist())
考虑将日期时间列转换为时间的字符串表示,并使用 DataFrame.to_numpy()
而不是 iterrows
方法:
df = pdr.get_data_yahoo(ticker, start_date)
# ADD NEW COLUMNS
df["data_vendor_id"] = YAHOO_VENDOR_ID
df["ticker_id"] = ticker_index[ticker]]
# CONVERT DATE TO STRING TIME
df["DATE"] = df["DATE"].dt.strftime('%Y-%m-%d %H:%M:%S')
sql = '''INSERT INTO daily_price (data_vendor_id, ticker_id, price_date,
open_price, high_price, low_price,
close_price, adj_close_price, volume)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
'''
# LIST
cursor.executemany(sql, df.to_numpy().tolist())
conn.commit()